Add deliver days

This commit is contained in:
eraden 2022-04-17 23:15:00 +02:00
parent 05584144d6
commit 8871f46738
2 changed files with 33 additions and 14 deletions

View File

@ -4,8 +4,8 @@ use sqlx::PgPool;
use super::Result;
use crate::database::Database;
use crate::model::{
PriceMajor, PriceMinor, Product, ProductCategory, ProductId, ProductLongDesc, ProductName,
ProductShortDesc,
Days, PriceMajor, PriceMinor, Product, ProductCategory, ProductId, ProductLongDesc,
ProductName, ProductShortDesc,
};
use crate::{database, model};
@ -58,6 +58,7 @@ pub struct CreateProduct {
pub category: Option<ProductCategory>,
pub price_major: PriceMajor,
pub price_minor: PriceMinor,
pub deliver_days_flag: Days,
}
crate::db_async_handler!(CreateProduct, create_product, Product);
@ -66,14 +67,15 @@ pub(crate) async fn create_product(msg: CreateProduct, pool: PgPool) -> Result<m
sqlx::query_as(
r#"
INSERT INTO products (name, short_description, long_description, category, price_major, price_minor)
VALUES ($1, $2, $3, $4, $5, $6)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id,
name,
short_description,
long_description,
category,
price_major,
price_minor
price_minor,
deliver_days_flag
"#,
)
.bind(msg.name)
@ -82,6 +84,7 @@ RETURNING id,
.bind(msg.category)
.bind(msg.price_major)
.bind(msg.price_minor)
.bind(msg.deliver_days_flag)
.fetch_one(&pool)
.await
.map_err(|e| {
@ -100,6 +103,7 @@ pub struct UpdateProduct {
pub category: Option<ProductCategory>,
pub price_major: PriceMajor,
pub price_minor: PriceMinor,
pub deliver_days_flag: Days,
}
crate::db_async_handler!(UpdateProduct, update_product, Product);
@ -108,29 +112,32 @@ pub(crate) async fn update_product(msg: UpdateProduct, pool: PgPool) -> Result<m
sqlx::query_as(
r#"
UPDATE products
SET name = $1 AND
short_description = $2 AND
long_description = $3 AND
category = $4 AND
price_major = $5 AND
price_minor = $6
WHERE id = $7
SET name = $2 AND
short_description = $3 AND
long_description = $4 AND
category = $5 AND
price_major = $6 AND
price_minor = $7 AND
deliver_days_flag = $8
WHERE id = $1
RETURNING id,
name,
short_description,
long_description,
category,
price_major,
price_minor
price_minor,
deliver_days_flag
"#,
)
.bind(msg.id)
.bind(msg.name)
.bind(msg.short_description)
.bind(msg.long_description)
.bind(msg.category)
.bind(msg.price_major)
.bind(msg.price_minor)
.bind(msg.id)
.bind(msg.deliver_days_flag)
.fetch_one(&pool)
.await
.map_err(|e| {
@ -152,6 +159,14 @@ pub(crate) async fn delete_product(msg: DeleteProduct, pool: PgPool) -> Result<O
r#"
DELETE FROM products
WHERE id = $1
RETURNING id,
name,
short_description,
long_description,
category,
price_major,
price_minor,
deliver_days_flag
"#,
)
.bind(msg.product_id)

View File

@ -1,7 +1,7 @@
use crate::database;
use crate::database::Database;
use crate::model::{
PriceMajor, PriceMinor, ProductCategory, ProductId, ProductLongDesc, ProductName,
Days, PriceMajor, PriceMinor, ProductCategory, ProductId, ProductLongDesc, ProductName,
ProductShortDesc,
};
use crate::routes::admin::Error;
@ -30,6 +30,7 @@ pub struct UpdateProduct {
pub category: Option<ProductCategory>,
pub price_major: PriceMajor,
pub price_minor: PriceMinor,
pub deliver_days_flag: Days,
}
#[patch("/product")]
@ -50,6 +51,7 @@ async fn update_product(
category: payload.category,
price_major: payload.price_major,
price_minor: payload.price_minor,
deliver_days_flag: payload.deliver_days_flag,
}
);
}
@ -62,6 +64,7 @@ pub struct CreateProduct {
pub category: Option<ProductCategory>,
pub price_major: PriceMajor,
pub price_minor: PriceMinor,
pub deliver_days_flag: Days,
}
#[post("/product")]
@ -81,6 +84,7 @@ async fn create_product(
category: payload.category,
price_major: payload.price_major,
price_minor: payload.price_minor,
deliver_days_flag: payload.deliver_days_flag,
}
);
}