Add product class

This commit is contained in:
~erin 2022-04-01 12:59:52 -04:00
parent 4d0fb59e33
commit 233cc25371
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
3 changed files with 16 additions and 8 deletions

View File

@ -23,3 +23,4 @@ captcha = "0.0.8"
#env_logger = "0.9.0"
paris = { version = "1.5", features = ["macros"] }
tera = "1"
toml = "0.5"

View File

@ -8,14 +8,12 @@ use crate::authenticate::*;
use crate::database::*;
use crate::structures::*;
use crate::html::render_pages;
use argon2::{password_hash::SaltString, Argon2};
// Endpoint to add a new product
#[post("/api/new", data = "<request>")]
pub fn new_product(
argon2: &State<Argon2>,
request: Form<ProductRequest>,
) -> Json<Status> {
pub fn new_product(argon2: &State<Argon2>, request: Form<ProductRequest>) -> Json<Status> {
/* Need to rework the password mechanism so it's not stored in plaintext serverside */
if check_password(&request.password, &argon2) {
@ -26,6 +24,7 @@ pub fn new_product(
full_name: request.full_name.clone(),
price_usd: request.price_usd,
stock: request.stock,
class: request.class,
};
match register_product(&new_product.short_name, &new_product) {
@ -39,6 +38,8 @@ pub fn new_product(
}
}
render_pages();
return Json(Status {
// Return a JSON status
status: "success".to_string(),
@ -225,10 +226,7 @@ pub fn delete_order(auth: Form<Authenticate>, order: &str, argon2: &State<Argon2
// Update fields of a product
#[post("/api/update", data = "<update>")]
pub fn update_product(
argon2: &State<Argon2>,
update: Form<Update<'_>>,
) -> Json<Status> {
pub fn update_product(argon2: &State<Argon2>, update: Form<Update<'_>>) -> Json<Status> {
if check_password(&update.password, &argon2) {
match update.field {
// Check what field is being updated

View File

@ -36,9 +36,17 @@ pub struct ProductRequest {
pub short_name: String,
pub price_usd: f64,
pub stock: u32,
pub class: ProductClass,
pub password: String,
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, FromFormField)]
pub enum ProductClass {
Estrogens,
AntiAndrogens,
Progestogens,
}
// Struct for a stored product
#[derive(Clone, Serialize, Deserialize, Debug, FromForm)]
pub struct Product {
@ -46,6 +54,7 @@ pub struct Product {
pub short_name: String,
pub price_usd: f64,
pub stock: u32,
pub class: ProductClass,
}
// User order request