Implement the customer handlers using the model

This commit is contained in:
Paul van Tilburg 2019-06-20 20:05:12 +02:00
parent f4d72b68d2
commit 6841a3b026
2 changed files with 117 additions and 21 deletions

View File

@ -1,4 +1,4 @@
//! The root handlers
//! The application handlers
use rocket::get;

View File

@ -1,44 +1,140 @@
//! The customer handlers
//!
//! Handlers for viewing a list of existing customers or creating a new one.
//! Handlers for viewing a list of existing customers or creating a new one.
use diesel::prelude::*;
use diesel::result::Error as DieselError;
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::DbConn;
pub mod invoices;
pub mod tasks;
/// Shows the list of customers.
#[get("/")]
pub fn index() {
unimplemented!()
fn error_status(error: DieselError) -> Status {
match error {
DieselError::NotFound => Status::NotFound,
_ => Status::InternalServerError,
}
}
/// Creates a new customer and redirects to the show handler.
#[post("/")]
pub fn create() {
unimplemented!()
/// 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() {
unimplemented!()
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: u32) {
unimplemented!()
#[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>")]
pub fn update(_id: u32) {
unimplemented!()
#[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: u32) {
unimplemented!()
#[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");
}
}