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-16 07:31:19 +02:00
|
|
|
use sqlx::PgPool;
|
|
|
|
|
2022-04-18 22:07:52 +02:00
|
|
|
use super::Result;
|
2022-05-06 16:02:38 +02:00
|
|
|
use crate::db_async_handler;
|
2022-04-16 07:31:19 +02:00
|
|
|
|
2022-06-04 16:55:29 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, serde::Serialize, thiserror::Error)]
|
2022-04-16 07:31:19 +02:00
|
|
|
pub enum Error {
|
|
|
|
#[error("Can't create order item")]
|
|
|
|
CantCreate,
|
2022-04-18 08:52:09 +02:00
|
|
|
#[error("Can't find order item doe to lack of identity")]
|
2022-04-16 07:31:19 +02:00
|
|
|
NoIdentity,
|
|
|
|
#[error("Order item does not exists")]
|
|
|
|
NotExists,
|
|
|
|
#[error("Failed to load all order items")]
|
|
|
|
All,
|
2022-05-04 07:26:29 +02:00
|
|
|
#[error("Failed to load order items")]
|
|
|
|
OrderItems,
|
2022-04-16 07:31:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<Vec<OrderItem>>")]
|
|
|
|
pub struct AllOrderItems;
|
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
db_async_handler!(AllOrderItems, all_order_items, Vec<OrderItem>);
|
2022-04-16 07:31:19 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn all_order_items(_msg: AllOrderItems, pool: PgPool) -> Result<Vec<OrderItem>> {
|
2022-04-16 07:31:19 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-04-20 16:09:09 +02:00
|
|
|
SELECT id, product_id, order_id, quantity, quantity_unit
|
2022-04-16 07:31:19 +02:00
|
|
|
FROM order_items
|
2022-05-01 18:25:27 +02:00
|
|
|
ORDER BY id DESC
|
2022-04-16 07:31:19 +02:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.fetch_all(&pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e:?}");
|
2022-06-06 15:09:13 +02:00
|
|
|
Error::All.into()
|
2022-04-16 07:31:19 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:02:38 +02:00
|
|
|
#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
|
2022-04-16 07:31:19 +02:00
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<OrderItem>")]
|
|
|
|
pub struct CreateOrderItem {
|
2022-04-20 16:09:09 +02:00
|
|
|
pub product_id: ProductId,
|
2022-05-23 14:11:56 +02:00
|
|
|
pub order_id: OrderId,
|
2022-04-20 16:09:09 +02:00
|
|
|
pub quantity: Quantity,
|
|
|
|
pub quantity_unit: QuantityUnit,
|
2022-04-16 07:31:19 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 16:09:09 +02:00
|
|
|
db_async_handler!(CreateOrderItem, inner_create_order_item, OrderItem);
|
2022-04-16 07:31:19 +02:00
|
|
|
|
2022-04-20 16:09:09 +02:00
|
|
|
async fn inner_create_order_item(msg: CreateOrderItem, db: PgPool) -> Result<OrderItem> {
|
|
|
|
create_order_item(msg, &db).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn create_order_item<'e, E>(msg: CreateOrderItem, db: E) -> Result<OrderItem>
|
|
|
|
where
|
|
|
|
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
|
|
|
{
|
2022-04-16 07:31:19 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-04-20 16:09:09 +02:00
|
|
|
INSERT INTO order_items (product_id, order_id, quantity, quantity_unit)
|
|
|
|
VALUES ($1, $2, $3, $4)
|
|
|
|
RETURNING id, product_id, order_id, quantity, quantity_unit
|
2022-04-16 07:31:19 +02:00
|
|
|
"#,
|
|
|
|
)
|
2022-04-20 16:09:09 +02:00
|
|
|
.bind(msg.product_id)
|
|
|
|
.bind(msg.order_id)
|
|
|
|
.bind(msg.quantity)
|
|
|
|
.bind(msg.quantity_unit)
|
|
|
|
.fetch_one(db)
|
2022-04-16 07:31:19 +02:00
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e:?}");
|
2022-04-16 07:31:19 +02:00
|
|
|
super::Error::OrderItem(Error::CantCreate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<OrderItem>")]
|
|
|
|
pub struct FindOrderItem {
|
|
|
|
pub id: OrderItemId,
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
db_async_handler!(FindOrderItem, find_order_item, OrderItem);
|
2022-04-16 07:31:19 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn find_order_item(msg: FindOrderItem, db: PgPool) -> Result<OrderItem> {
|
2022-04-16 07:31:19 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-04-20 16:09:09 +02:00
|
|
|
SELECT id, product_id, order_id, quantity, quantity_unit
|
2022-04-16 07:31:19 +02:00
|
|
|
FROM order_items
|
|
|
|
WHERE id = $1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.id)
|
|
|
|
.fetch_one(&db)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e:?}");
|
2022-06-06 15:09:13 +02:00
|
|
|
Error::NotExists.into()
|
2022-04-16 07:31:19 +02:00
|
|
|
})
|
|
|
|
}
|
2022-05-04 07:26:29 +02:00
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<Vec<OrderItem>>")]
|
|
|
|
pub struct OrderItems {
|
2022-06-06 16:07:49 +02:00
|
|
|
pub order_id: OrderId,
|
2022-05-04 07:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(OrderItems, order_items, Vec<OrderItem>);
|
|
|
|
|
|
|
|
pub(crate) async fn order_items(msg: OrderItems, pool: PgPool) -> Result<Vec<OrderItem>> {
|
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
SELECT id, product_id, order_id, quantity, quantity_unit
|
|
|
|
FROM order_items
|
|
|
|
WHERE order_id = $1
|
|
|
|
ORDER BY id DESC
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.order_id)
|
|
|
|
.fetch_all(&pool)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e:?}");
|
2022-06-06 15:09:13 +02:00
|
|
|
Error::OrderItems.into()
|
2022-05-04 07:26:29 +02:00
|
|
|
})
|
|
|
|
}
|