bazzar/api/src/actors/database/order_items.rs
2022-04-20 16:09:09 +02:00

103 lines
2.5 KiB
Rust

use sqlx::PgPool;
use super::Result;
use crate::database::Database;
use crate::db_async_handler;
use crate::model::*;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Can't create order item")]
CantCreate,
#[error("Can't find order item doe to lack of identity")]
NoIdentity,
#[error("Order item does not exists")]
NotExists,
#[error("Failed to load all order items")]
All,
}
#[derive(actix::Message)]
#[rtype(result = "Result<Vec<OrderItem>>")]
pub struct AllOrderItems;
db_async_handler!(AllOrderItems, all_order_items, Vec<OrderItem>);
pub(crate) async fn all_order_items(_msg: AllOrderItems, pool: PgPool) -> Result<Vec<OrderItem>> {
sqlx::query_as(
r#"
SELECT id, product_id, order_id, quantity, quantity_unit
FROM order_items
"#,
)
.fetch_all(&pool)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::OrderItem(Error::All)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<OrderItem>")]
pub struct CreateOrderItem {
pub product_id: ProductId,
pub order_id: AccountOrderId,
pub quantity: Quantity,
pub quantity_unit: QuantityUnit,
}
db_async_handler!(CreateOrderItem, inner_create_order_item, OrderItem);
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>,
{
sqlx::query_as(
r#"
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
"#,
)
.bind(msg.product_id)
.bind(msg.order_id)
.bind(msg.quantity)
.bind(msg.quantity_unit)
.fetch_one(db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::OrderItem(Error::CantCreate)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<OrderItem>")]
pub struct FindOrderItem {
pub id: OrderItemId,
}
db_async_handler!(FindOrderItem, find_order_item, OrderItem);
pub(crate) async fn find_order_item(msg: FindOrderItem, db: PgPool) -> Result<OrderItem> {
sqlx::query_as(
r#"
SELECT id, product_id, order_id, quantity, quantity_unit
FROM order_items
WHERE id = $1
"#,
)
.bind(msg.id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::OrderItem(Error::NotExists)
})
}