From e40d1bd3d84eefc797dd5d0bad063f69a72f7f5d Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Fri, 1 Apr 2022 15:40:16 -0400 Subject: [PATCH] Add description to Product --- README.md | 2 +- src/api.rs | 41 +++++++++++++++++++++++++++++---- src/html.rs | 5 +--- src/structures.rs | 14 +++++++++-- templates/products/product.html | 11 +++++---- 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 22884ca..e76cfae 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,4 @@ A simple server for dealing with products & orders for the Catgirl Pharmacy. - [x] Add comments - [x] Add logging - [x] Format properly -- [/] Generate static web assets via code +- [x] Generate static web assets via code diff --git a/src/api.rs b/src/api.rs index ad061b2..375641d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -22,6 +22,7 @@ pub fn new_product(argon2: &State, request: Form) -> Jso // Create a new Product object from the supplied info short_name: request.short_name.clone(), full_name: request.full_name.clone(), + description: request.description.clone(), price_usd: request.price_usd, stock: request.stock, class: request.class, @@ -231,7 +232,7 @@ pub fn update_product(argon2: &State, update: Form>) -> Json< if check_password(&update.password, &argon2) { match update.field { // Check what field is being updated - "price_usd" => { + UpdateField::Price => { let mut new_product = match read_product(&update.product) { // Read the product from the database Ok(product) => product, @@ -243,7 +244,7 @@ pub fn update_product(argon2: &State, update: Form>) -> Json< } }; - new_product.price_usd = update.value; // Change the price_usd value to the provided value + new_product.price_usd = update.value.unwrap(); // Change the price_usd value to the provided value match register_product(&update.product, &new_product) { // Update the product in the database Ok(s) => info!("{}", s), @@ -263,7 +264,7 @@ pub fn update_product(argon2: &State, update: Form>) -> Json< reason: "price changed".to_string(), }); } - "stock" => { + UpdateField::Stock => { // Same as before, but with stock let mut new_product = match read_product(&update.product) { Ok(product) => product, @@ -275,7 +276,7 @@ pub fn update_product(argon2: &State, update: Form>) -> Json< } }; - new_product.stock = update.value as u32; + new_product.stock = update.value.unwrap() as u32; match register_product(&update.product, &new_product) { Ok(s) => info!("{}", s), Err(error) => { @@ -293,6 +294,38 @@ pub fn update_product(argon2: &State, update: Form>) -> Json< reason: "stock changed".to_string(), }); } + UpdateField::Description => { + // Update the description + let mut new_product = match read_product(&update.product) { + Ok(product) => product, + Err(error) => { + return Json(Status { + status: "fail".to_string(), + reason: error.to_string(), + }); + } + }; + + new_product.description = update.text.unwrap().to_string(); + println!("Description: {}", &new_product.description); + match register_product(&update.product, &new_product) { + Ok(s) => info!("{}", s), + Err(error) => { + return Json(Status { + status: "fail".to_string(), + reason: error.to_string(), + }) + } + } + + render_product(new_product); + + return Json(Status { + status: "success".to_string(), + reason: "description changed".to_string(), + }); + } + _ => { // Fail because provided field wasn't found, return json return Json(Status { diff --git a/src/html.rs b/src/html.rs index 0fe7e0b..13bc375 100644 --- a/src/html.rs +++ b/src/html.rs @@ -248,10 +248,7 @@ pub fn render_product(product: Product) { context.insert("description", &base.config.description); // Fill out product information - context.insert("full_name", &obj.product.full_name); - context.insert("short_name", &obj.product.short_name); - context.insert("price_usd", &obj.product.price_usd); - context.insert("stock", &obj.product.stock); + context.insert("product", &obj.product); // Insert navigation elements for i in &base.nav_elements { diff --git a/src/structures.rs b/src/structures.rs index 90ea135..a4837b2 100644 --- a/src/structures.rs +++ b/src/structures.rs @@ -34,6 +34,7 @@ pub struct Authenticate { pub struct ProductRequest { pub full_name: String, pub short_name: String, + pub description: String, pub price_usd: f64, pub stock: u32, pub class: ProductClass, @@ -52,6 +53,7 @@ pub enum ProductClass { pub struct Product { pub full_name: String, pub short_name: String, + pub description: String, pub price_usd: f64, pub stock: u32, pub class: ProductClass, @@ -80,12 +82,20 @@ pub struct Order { pub status: OrderStatus, } +#[derive(FromFormField)] +pub enum UpdateField { + Price, + Stock, + Description, +} + // Struct for a request to update a product #[derive(FromForm)] pub struct Update<'r> { pub r#product: &'r str, - pub r#field: &'r str, - pub value: f64, + pub field: UpdateField, + pub value: Option, + pub text: Option<&'r str>, pub r#password: &'r str, } diff --git a/templates/products/product.html b/templates/products/product.html index 26604bd..bdaac9f 100644 --- a/templates/products/product.html +++ b/templates/products/product.html @@ -43,13 +43,16 @@ -

{{ full_name }}

+

{{ product.full_name }}

- {{full_name}} + {{product.full_name}}
- ${{price_usd}} USD + ${{product.price_usd}} USD
- {{stock}} in stock + {{product.stock}} in stock + +

Description

+

{{ product.description | linebreaksbr | safe }}