bazzar/crates/database_manager/src/products.rs

446 lines
11 KiB
Rust
Raw Normal View History

2022-04-18 22:07:52 +02:00
use actix::Message;
2022-05-06 16:02:38 +02:00
#[cfg(feature = "dummy")]
use fake::Fake;
2022-05-06 11:47:18 +02:00
use model::{
2022-04-19 16:49:30 +02:00
Days, Price, Product, ProductCategory, ProductId, ProductLongDesc, ProductName,
ProductShortDesc,
2022-04-14 21:40:26 +02:00
};
2022-05-06 11:47:18 +02:00
use super::Result;
2022-05-13 15:24:11 +02:00
use crate::MultiLoad;
2022-04-14 21:40:26 +02:00
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, thiserror::Error)]
2022-04-14 21:40:26 +02:00
pub enum Error {
#[error("Unable to load all products")]
All,
#[error("Unable to create product")]
Create,
#[error("Unable to update product")]
Update,
#[error("Unable to delete product")]
Delete,
2022-05-04 07:26:29 +02:00
#[error("Unable to find products for shopping cart")]
ShoppingCartProducts,
2022-05-11 15:56:41 +02:00
#[error("Product with id {0} can't be found")]
2022-06-04 16:55:29 +02:00
Single(ProductId),
2022-05-13 15:24:11 +02:00
#[error("Failed to load products for given ids")]
FindProducts,
2022-04-14 21:40:26 +02:00
}
#[derive(Message)]
#[rtype(result = "Result<Vec<model::Product>>")]
pub struct AllProducts;
2022-05-04 22:26:10 +02:00
crate::db_async_handler!(AllProducts, all, Vec<Product>, inner_all);
2022-04-14 21:40:26 +02:00
2022-05-04 22:26:10 +02:00
pub(crate) async fn all<'e, E>(_msg: AllProducts, pool: E) -> Result<Vec<model::Product>>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
2022-04-14 21:40:26 +02:00
sqlx::query_as(
r#"
SELECT id,
name,
short_description,
long_description,
category,
2022-04-19 16:49:30 +02:00
price,
2022-04-17 23:11:58 +02:00
deliver_days_flag
2022-04-14 21:40:26 +02:00
FROM products
2022-05-13 15:24:11 +02:00
ORDER BY id
2022-04-14 21:40:26 +02:00
"#,
)
2022-05-04 22:26:10 +02:00
.fetch_all(pool)
2022-04-14 21:40:26 +02:00
.await
.map_err(|e| {
tracing::error!("{e:?}");
2022-05-06 11:47:18 +02:00
crate::Error::Product(Error::All)
2022-04-14 21:40:26 +02:00
})
}
2022-05-11 15:56:41 +02:00
#[derive(Message)]
#[rtype(result = "Result<model::Product>")]
pub struct FindProduct {
pub product_id: model::ProductId,
}
crate::db_async_handler!(FindProduct, find_product, Product, inner_find_product);
pub(crate) async fn find_product<'e, E>(msg: FindProduct, pool: E) -> Result<model::Product>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
sqlx::query_as(
r#"
SELECT id,
name,
short_description,
long_description,
category,
price,
deliver_days_flag
FROM products
WHERE id = $1
"#,
)
.bind(msg.product_id)
.fetch_one(pool)
.await
.map_err(|e| {
tracing::error!("{e:?}");
2022-05-11 15:56:41 +02:00
crate::Error::Product(Error::Single(msg.product_id))
})
}
2022-05-08 09:47:05 +02:00
#[derive(Message, Debug)]
2022-04-14 21:40:26 +02:00
#[rtype(result = "Result<model::Product>")]
pub struct CreateProduct {
pub name: ProductName,
pub short_description: ProductShortDesc,
pub long_description: ProductLongDesc,
pub category: Option<ProductCategory>,
2022-04-19 16:49:30 +02:00
pub price: Price,
2022-04-17 23:15:00 +02:00
pub deliver_days_flag: Days,
2022-04-14 21:40:26 +02:00
}
2022-05-04 22:26:10 +02:00
crate::db_async_handler!(CreateProduct, create_product, Product, inner_create_product);
2022-04-14 21:40:26 +02:00
2022-05-04 22:26:10 +02:00
pub(crate) async fn create_product<'e, E>(msg: CreateProduct, pool: E) -> Result<model::Product>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
2022-04-14 21:40:26 +02:00
sqlx::query_as(
r#"
2022-04-19 16:49:30 +02:00
INSERT INTO products (name, short_description, long_description, category, price, deliver_days_flag)
VALUES ($1, $2, $3, $4, $5, $6)
2022-04-14 21:40:26 +02:00
RETURNING id,
name,
short_description,
long_description,
category,
2022-04-19 16:49:30 +02:00
price,
2022-04-17 23:15:00 +02:00
deliver_days_flag
2022-04-14 21:40:26 +02:00
"#,
)
.bind(msg.name)
.bind(msg.short_description)
.bind(msg.long_description)
.bind(msg.category)
2022-04-19 16:49:30 +02:00
.bind(msg.price)
2022-04-17 23:15:00 +02:00
.bind(msg.deliver_days_flag)
2022-05-04 22:26:10 +02:00
.fetch_one(pool)
2022-04-14 21:40:26 +02:00
.await
.map_err(|e| {
tracing::error!("{e:?}");
2022-06-06 15:09:13 +02:00
dbg!(e);
2022-05-06 11:47:18 +02:00
crate::Error::Product(Error::Create)
2022-04-14 21:40:26 +02:00
})
}
#[derive(Message)]
#[rtype(result = "Result<model::Product>")]
pub struct UpdateProduct {
pub id: ProductId,
pub name: ProductName,
pub short_description: ProductShortDesc,
pub long_description: ProductLongDesc,
pub category: Option<ProductCategory>,
2022-04-19 16:49:30 +02:00
pub price: Price,
2022-04-17 23:15:00 +02:00
pub deliver_days_flag: Days,
2022-04-14 21:40:26 +02:00
}
2022-05-04 22:26:10 +02:00
crate::db_async_handler!(UpdateProduct, update_product, Product, inner_update_product);
2022-04-14 21:40:26 +02:00
2022-06-06 15:09:13 +02:00
pub(crate) async fn update_product<'e, E>(msg: UpdateProduct, pool: E) -> Result<Product>
2022-05-04 22:26:10 +02:00
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
2022-04-14 21:40:26 +02:00
sqlx::query_as(
r#"
UPDATE products
SET name = $2,
short_description = $3,
long_description = $4,
category = $5,
price = $6,
2022-04-19 16:49:30 +02:00
deliver_days_flag = $7
2022-04-17 23:15:00 +02:00
WHERE id = $1
2022-04-14 21:40:26 +02:00
RETURNING id,
name,
short_description,
long_description,
category,
2022-04-19 16:49:30 +02:00
price,
2022-04-17 23:15:00 +02:00
deliver_days_flag
2022-04-14 21:40:26 +02:00
"#,
)
2022-04-17 23:15:00 +02:00
.bind(msg.id)
2022-04-14 21:40:26 +02:00
.bind(msg.name)
.bind(msg.short_description)
.bind(msg.long_description)
.bind(msg.category)
2022-04-19 16:49:30 +02:00
.bind(msg.price)
2022-04-17 23:15:00 +02:00
.bind(msg.deliver_days_flag)
2022-05-04 22:26:10 +02:00
.fetch_one(pool)
2022-04-14 21:40:26 +02:00
.await
.map_err(|e| {
tracing::error!("{e:?}");
dbg!(e);
2022-05-06 11:47:18 +02:00
crate::Error::Product(Error::Update)
2022-04-14 21:40:26 +02:00
})
}
#[derive(Message)]
#[rtype(result = "Result<Option<model::Product>>")]
pub struct DeleteProduct {
pub product_id: ProductId,
}
2022-05-04 22:26:10 +02:00
crate::db_async_handler!(
DeleteProduct,
delete_product,
Option<model::Product>,
inner_delete_product
);
2022-04-14 21:40:26 +02:00
2022-05-04 22:26:10 +02:00
pub(crate) async fn delete_product<'e, E>(msg: DeleteProduct, pool: E) -> Result<Option<Product>>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
2022-04-14 21:40:26 +02:00
sqlx::query_as(
r#"
DELETE FROM products
WHERE id = $1
2022-04-17 23:15:00 +02:00
RETURNING id,
name,
short_description,
long_description,
category,
2022-04-19 16:49:30 +02:00
price,
2022-04-17 23:15:00 +02:00
deliver_days_flag
2022-04-14 21:40:26 +02:00
"#,
)
.bind(msg.product_id)
2022-05-04 22:26:10 +02:00
.fetch_optional(pool)
2022-04-14 21:40:26 +02:00
.await
.map_err(|e| {
tracing::error!("{e:?}");
2022-05-06 11:47:18 +02:00
crate::Error::Product(Error::Delete)
2022-04-14 21:40:26 +02:00
})
}
2022-05-04 07:26:29 +02:00
#[derive(Message)]
#[rtype(result = "Result<Vec<model::Product>>")]
pub struct ShoppingCartProducts {
pub shopping_cart_id: model::ShoppingCartId,
}
crate::db_async_handler!(
ShoppingCartProducts,
shopping_cart_products,
2022-05-04 22:26:10 +02:00
Vec<model::Product>,
inner_shopping_cart_products
2022-05-04 07:26:29 +02:00
);
2022-05-04 22:26:10 +02:00
pub(crate) async fn shopping_cart_products<'e, E>(
2022-05-04 07:26:29 +02:00
msg: ShoppingCartProducts,
2022-05-04 22:26:10 +02:00
pool: E,
) -> Result<Vec<Product>>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
2022-05-04 07:26:29 +02:00
sqlx::query_as(
r#"
SELECT products.id,
products.name,
products.short_description,
products.long_description,
products.category,
products.price,
products.deliver_days_flag
FROM products
INNER JOIN shopping_cart_items ON shopping_cart_items.product_id = products.id
WHERE shopping_cart_id = $1
2022-05-13 15:24:11 +02:00
ORDER BY products.id
2022-05-04 07:26:29 +02:00
"#,
)
.bind(msg.shopping_cart_id)
2022-05-04 22:26:10 +02:00
.fetch_all(pool)
2022-05-04 07:26:29 +02:00
.await
.map_err(|e| {
tracing::error!("{e:?}");
2022-05-06 11:47:18 +02:00
crate::Error::Product(Error::ShoppingCartProducts)
2022-05-04 07:26:29 +02:00
})
}
2022-05-13 15:24:11 +02:00
#[derive(Message)]
#[rtype(result = "Result<Vec<model::Product>>")]
pub struct FindProducts {
pub product_ids: Vec<ProductId>,
2022-05-13 15:24:11 +02:00
}
crate::db_async_handler!(
FindProducts,
find_products,
Vec<Product>,
inner_find_products
);
pub(crate) async fn find_products(
msg: FindProducts,
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Vec<Product>> {
2022-05-13 15:24:11 +02:00
MultiLoad::new(
pool,
r#"
SELECT id,
name,
short_description,
long_description,
category,
price,
deliver_days_flag
FROM products
WHERE
"#,
"products.id =",
)
.load(
msg.product_ids.len(),
msg.product_ids.into_iter().map(|id| *id),
|e| {
tracing::error!("{e:?}");
2022-05-13 15:24:11 +02:00
crate::Error::Product(Error::FindProducts)
},
)
.await
}
2022-06-06 15:09:13 +02:00
#[cfg(test)]
mod tests {
use config::UpdateConfig;
use model::*;
use uuid::Uuid;
pub struct NoOpts;
impl UpdateConfig for NoOpts {}
use crate::*;
async fn test_product(
t: &mut sqlx::Transaction<'_, sqlx::Postgres>,
name: Option<String>,
short_description: Option<String>,
long_description: Option<String>,
category: Option<ProductCategory>,
price: Option<u32>,
deliver_days_flag: Option<Days>,
) -> Product {
super::create_product(
CreateProduct {
name: ProductName::new(name.unwrap_or_else(|| format!("{}", Uuid::new_v4()))),
short_description: ProductShortDesc::new(
short_description.unwrap_or_else(|| format!("{}", Uuid::new_v4())),
),
long_description: ProductLongDesc::new(
long_description.unwrap_or_else(|| format!("{}", Uuid::new_v4())),
),
category,
price: Price::from_u32(price.unwrap_or(4687)),
deliver_days_flag: deliver_days_flag
.unwrap_or_else(|| Days(vec![Day::Friday, Day::Sunday])),
},
t,
)
.await
.unwrap()
}
#[actix::test]
async fn create() {
testx::db_t_ref!(t);
2022-06-06 15:09:13 +02:00
test_product(&mut t, None, None, None, None, None, None).await;
testx::db_rollback!(t);
}
#[actix::test]
async fn all() {
testx::db_t_ref!(t);
2022-06-06 15:09:13 +02:00
let p1 = test_product(&mut t, None, None, None, None, None, None).await;
let p2 = test_product(&mut t, None, None, None, None, None, None).await;
let p3 = test_product(&mut t, None, None, None, None, None, None).await;
let products = super::all(AllProducts, &mut t).await.unwrap();
testx::db_rollback!(t);
assert_eq!(products, vec![p1, p2, p3]);
}
#[actix::test]
async fn find() {
testx::db_t_ref!(t);
2022-06-06 15:09:13 +02:00
let p1 = test_product(&mut t, None, None, None, None, None, None).await;
let p2 = test_product(&mut t, None, None, None, None, None, None).await;
let p3 = test_product(&mut t, None, None, None, None, None, None).await;
let product = find_product(FindProduct { product_id: p2.id }, &mut t)
.await
.unwrap();
testx::db_rollback!(t);
assert_ne!(product, p1);
assert_eq!(product, p2);
assert_ne!(product, p3);
}
#[actix::test]
async fn update() {
testx::db_t_ref!(t);
let original = test_product(&mut t, None, None, None, None, None, None).await;
let updated = update_product(
UpdateProduct {
id: original.id,
name: ProductName::new("a9s0dja0sjd0jas09dj"),
short_description: ProductShortDesc::new("ajs9d8ua9sdu9ahsd98has"),
long_description: ProductLongDesc::new("hja89sdy9yha9sdy98ayusd9hya9sy8dh"),
category: None,
price: Price::from_u32(823794),
deliver_days_flag: Day::Tuesday | Day::Saturday,
},
&mut t,
)
.await
.unwrap();
let reloaded = find_product(
FindProduct {
product_id: original.id,
},
&mut t,
)
.await
.unwrap();
testx::db_rollback!(t);
assert_ne!(updated, original);
assert_eq!(updated, reloaded);
assert_eq!(
updated,
Product {
id: original.id,
name: ProductName::new("a9s0dja0sjd0jas09dj"),
short_description: ProductShortDesc::new("ajs9d8ua9sdu9ahsd98has"),
long_description: ProductLongDesc::new("hja89sdy9yha9sdy98ayusd9hya9sy8dh"),
category: None,
price: Price::from_u32(823794),
deliver_days_flag: Day::Tuesday | Day::Saturday,
}
);
}
2022-06-06 15:09:13 +02:00
}