134 lines
4.1 KiB
Rust
134 lines
4.1 KiB
Rust
//! The customer handlers
|
|
//!
|
|
//! Handlers for viewing a list of existing customers or creating a new one.
|
|
|
|
use diesel::prelude::*;
|
|
use rocket::http::Status;
|
|
use rocket::response::status::Created;
|
|
use rocket::uri;
|
|
use rocket::{delete, get, post, put};
|
|
use rocket_contrib::json::Json;
|
|
|
|
use crate::models::{Customer, NewCustomer};
|
|
use crate::rest_helpers::error_status;
|
|
use crate::DbConn;
|
|
|
|
pub mod invoices;
|
|
pub mod tasks;
|
|
|
|
/// Shows the list of customers.
|
|
#[get("/")]
|
|
pub fn index(conn: DbConn) -> Result<Json<Vec<Customer>>, Status> {
|
|
all!(Customer, *conn)
|
|
.map(|customers| Json(customers))
|
|
.map_err(|e| error_status(e))
|
|
}
|
|
|
|
/// Creates a customer from the new customer
|
|
#[post("/", format = "json", data = "<new_customer>")]
|
|
pub fn create(
|
|
new_customer: Json<NewCustomer>,
|
|
conn: DbConn,
|
|
) -> Result<Created<Json<Customer>>, Status> {
|
|
create!(Customer, new_customer.into_inner(), *conn)
|
|
.map(|customer: Customer| {
|
|
Created(
|
|
uri!("/customers", show: customer.id).to_string(),
|
|
Some(Json(customer)),
|
|
)
|
|
})
|
|
.map_err(|e| error_status(e))
|
|
}
|
|
|
|
/// Provides the form for the data required to create a new customer.
|
|
#[get("/new")]
|
|
pub fn new() -> Json<NewCustomer> {
|
|
Json(NewCustomer::default())
|
|
}
|
|
|
|
/// Shows a form for viewing and updating information of the customer with the given ID.
|
|
#[get("/<id>")]
|
|
pub fn show(id: i32, conn: DbConn) -> Result<Json<Customer>, Status> {
|
|
get!(Customer, id, *conn)
|
|
.map(|customer| Json(customer))
|
|
.map_err(|e| error_status(e))
|
|
}
|
|
|
|
/// Updates the customer with the given ID.
|
|
#[put("/<id>", format = "json", data = "<customer>")]
|
|
pub fn update(id: i32, customer: Json<Customer>, conn: DbConn) -> Result<Json<Customer>, Status> {
|
|
update!(Customer, id, customer.into_inner(), *conn)
|
|
.map(|customer| Json(customer))
|
|
.map_err(|e| error_status(e))
|
|
}
|
|
|
|
/// Destroys the customer with the given ID and redirects to the index handler.
|
|
#[delete("/<id>")]
|
|
pub fn destroy(id: i32, conn: DbConn) -> Result<Status, Status> {
|
|
delete!(Customer, id, *conn)
|
|
.map(|_| Status::NoContent)
|
|
.map_err(|e| error_status(e))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use rocket::http::{Accept, ContentType, Status};
|
|
use rocket::local::Client;
|
|
use serde_json::{self, json};
|
|
|
|
use super::*;
|
|
use crate::rocket;
|
|
|
|
#[test]
|
|
fn index() {
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
let mut res = client.get("/customers").header(Accept::JSON).dispatch();
|
|
assert_eq!(res.status(), Status::Ok);
|
|
|
|
let body = res.body_string().unwrap();
|
|
let customers = serde_json::from_str::<Vec<Customer>>(body.as_str()).unwrap();
|
|
assert_eq!(customers, vec![]);
|
|
// FIXME: Actually have something in de the index!
|
|
}
|
|
|
|
#[test]
|
|
fn create() {
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
let new_customer_json = json!({
|
|
"name": "Test Company"
|
|
});
|
|
let res = client
|
|
.post("/customers")
|
|
.header(ContentType::JSON)
|
|
.header(Accept::JSON)
|
|
.body(new_customer_json.to_string())
|
|
.dispatch();
|
|
assert_eq!(res.status(), Status::UnprocessableEntity);
|
|
|
|
let new_customer_json = json!({
|
|
"address_city": "Some City",
|
|
"address_postal_code": "123456",
|
|
"address_street": "Somestreet",
|
|
"email": "info@testcompany.tld",
|
|
"financial_contact": "Financial Office",
|
|
"hourly_rate": 100.0,
|
|
"name": "Test Company",
|
|
"phone": "0123456789",
|
|
"short_name": "Test Co.",
|
|
"time_specification": true
|
|
});
|
|
let mut res = client
|
|
.post("/customers")
|
|
.header(ContentType::JSON)
|
|
.header(Accept::JSON)
|
|
.body(new_customer_json.to_string())
|
|
.dispatch();
|
|
assert_eq!(res.status(), Status::Created);
|
|
let body = res.body_string().unwrap();
|
|
let new_customer = serde_json::from_str::<Customer>(body.as_str()).unwrap();
|
|
assert_eq!(new_customer.name, "Test Company");
|
|
}
|
|
}
|