Add description to Product

This commit is contained in:
~erin 2022-04-01 15:40:16 -04:00
parent 9615f13199
commit e40d1bd3d8
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
5 changed files with 58 additions and 15 deletions

View File

@ -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

View File

@ -22,6 +22,7 @@ pub fn new_product(argon2: &State<Argon2>, request: Form<ProductRequest>) -> 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<Argon2>, update: Form<Update<'_>>) -> 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<Argon2>, update: Form<Update<'_>>) -> 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<Argon2>, update: Form<Update<'_>>) -> 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<Argon2>, update: Form<Update<'_>>) -> 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<Argon2>, update: Form<Update<'_>>) -> 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 {

View File

@ -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 {

View File

@ -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<f64>,
pub text: Option<&'r str>,
pub r#password: &'r str,
}

View File

@ -43,13 +43,16 @@
</ul>
</nav>
<h2 class="product">{{ full_name }}</h2>
<h2 class="product">{{ product.full_name }}</h2>
<img class="product" alt="{{full_name}}" src="/assets/products/{{short_name}}.webp">
<img class="product" alt="{{product.full_name}}" src="/assets/products/{{product.short_name}}.webp">
<div class="product">
<code class="price" id="price">${{price_usd}} USD</code>
<code class="price" id="price">${{product.price_usd}} USD</code>
<br>
<code class="price" id="stock">{{stock}} in stock</code>
<code class="price" id="stock">{{product.stock}} in stock</code>
<h3>Description</h3>
<p>{{ product.description | linebreaksbr | safe }}</p>
</html>