Add description to Product
This commit is contained in:
parent
9615f13199
commit
e40d1bd3d8
|
@ -20,4 +20,4 @@ A simple server for dealing with products & orders for the Catgirl Pharmacy.
|
||||||
- [x] Add comments
|
- [x] Add comments
|
||||||
- [x] Add logging
|
- [x] Add logging
|
||||||
- [x] Format properly
|
- [x] Format properly
|
||||||
- [/] Generate static web assets via code
|
- [x] Generate static web assets via code
|
||||||
|
|
41
src/api.rs
41
src/api.rs
|
@ -22,6 +22,7 @@ pub fn new_product(argon2: &State<Argon2>, request: Form<ProductRequest>) -> Jso
|
||||||
// Create a new Product object from the supplied info
|
// Create a new Product object from the supplied info
|
||||||
short_name: request.short_name.clone(),
|
short_name: request.short_name.clone(),
|
||||||
full_name: request.full_name.clone(),
|
full_name: request.full_name.clone(),
|
||||||
|
description: request.description.clone(),
|
||||||
price_usd: request.price_usd,
|
price_usd: request.price_usd,
|
||||||
stock: request.stock,
|
stock: request.stock,
|
||||||
class: request.class,
|
class: request.class,
|
||||||
|
@ -231,7 +232,7 @@ pub fn update_product(argon2: &State<Argon2>, update: Form<Update<'_>>) -> Json<
|
||||||
if check_password(&update.password, &argon2) {
|
if check_password(&update.password, &argon2) {
|
||||||
match update.field {
|
match update.field {
|
||||||
// Check what field is being updated
|
// Check what field is being updated
|
||||||
"price_usd" => {
|
UpdateField::Price => {
|
||||||
let mut new_product = match read_product(&update.product) {
|
let mut new_product = match read_product(&update.product) {
|
||||||
// Read the product from the database
|
// Read the product from the database
|
||||||
Ok(product) => product,
|
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) {
|
match register_product(&update.product, &new_product) {
|
||||||
// Update the product in the database
|
// Update the product in the database
|
||||||
Ok(s) => info!("{}", s),
|
Ok(s) => info!("{}", s),
|
||||||
|
@ -263,7 +264,7 @@ pub fn update_product(argon2: &State<Argon2>, update: Form<Update<'_>>) -> Json<
|
||||||
reason: "price changed".to_string(),
|
reason: "price changed".to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
"stock" => {
|
UpdateField::Stock => {
|
||||||
// Same as before, but with stock
|
// Same as before, but with stock
|
||||||
let mut new_product = match read_product(&update.product) {
|
let mut new_product = match read_product(&update.product) {
|
||||||
Ok(product) => 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) {
|
match register_product(&update.product, &new_product) {
|
||||||
Ok(s) => info!("{}", s),
|
Ok(s) => info!("{}", s),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
@ -293,6 +294,38 @@ pub fn update_product(argon2: &State<Argon2>, update: Form<Update<'_>>) -> Json<
|
||||||
reason: "stock changed".to_string(),
|
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
|
// Fail because provided field wasn't found, return json
|
||||||
return Json(Status {
|
return Json(Status {
|
||||||
|
|
|
@ -248,10 +248,7 @@ pub fn render_product(product: Product) {
|
||||||
context.insert("description", &base.config.description);
|
context.insert("description", &base.config.description);
|
||||||
|
|
||||||
// Fill out product information
|
// Fill out product information
|
||||||
context.insert("full_name", &obj.product.full_name);
|
context.insert("product", &obj.product);
|
||||||
context.insert("short_name", &obj.product.short_name);
|
|
||||||
context.insert("price_usd", &obj.product.price_usd);
|
|
||||||
context.insert("stock", &obj.product.stock);
|
|
||||||
|
|
||||||
// Insert navigation elements
|
// Insert navigation elements
|
||||||
for i in &base.nav_elements {
|
for i in &base.nav_elements {
|
||||||
|
|
|
@ -34,6 +34,7 @@ pub struct Authenticate {
|
||||||
pub struct ProductRequest {
|
pub struct ProductRequest {
|
||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
pub short_name: String,
|
pub short_name: String,
|
||||||
|
pub description: String,
|
||||||
pub price_usd: f64,
|
pub price_usd: f64,
|
||||||
pub stock: u32,
|
pub stock: u32,
|
||||||
pub class: ProductClass,
|
pub class: ProductClass,
|
||||||
|
@ -52,6 +53,7 @@ pub enum ProductClass {
|
||||||
pub struct Product {
|
pub struct Product {
|
||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
pub short_name: String,
|
pub short_name: String,
|
||||||
|
pub description: String,
|
||||||
pub price_usd: f64,
|
pub price_usd: f64,
|
||||||
pub stock: u32,
|
pub stock: u32,
|
||||||
pub class: ProductClass,
|
pub class: ProductClass,
|
||||||
|
@ -80,12 +82,20 @@ pub struct Order {
|
||||||
pub status: OrderStatus,
|
pub status: OrderStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(FromFormField)]
|
||||||
|
pub enum UpdateField {
|
||||||
|
Price,
|
||||||
|
Stock,
|
||||||
|
Description,
|
||||||
|
}
|
||||||
|
|
||||||
// Struct for a request to update a product
|
// Struct for a request to update a product
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
pub struct Update<'r> {
|
pub struct Update<'r> {
|
||||||
pub r#product: &'r str,
|
pub r#product: &'r str,
|
||||||
pub r#field: &'r str,
|
pub field: UpdateField,
|
||||||
pub value: f64,
|
pub value: Option<f64>,
|
||||||
|
pub text: Option<&'r str>,
|
||||||
pub r#password: &'r str,
|
pub r#password: &'r str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,13 +43,16 @@
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</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">
|
<div class="product">
|
||||||
<code class="price" id="price">${{price_usd}} USD</code>
|
<code class="price" id="price">${{product.price_usd}} USD</code>
|
||||||
<br>
|
<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>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue