2022-04-16 18:57:37 +02:00
|
|
|
use crate::db_async_handler;
|
2022-04-16 07:31:19 +02:00
|
|
|
use actix::{Handler, ResponseActFuture, WrapFuture};
|
|
|
|
use sqlx::PgPool;
|
|
|
|
|
|
|
|
use crate::database::Database;
|
|
|
|
use crate::model::*;
|
|
|
|
|
|
|
|
use super::Result;
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("Can't create order item")]
|
|
|
|
CantCreate,
|
|
|
|
#[error("Can't find order item does 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;
|
|
|
|
|
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#"
|
|
|
|
SELECT id, buyer_id, status
|
|
|
|
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 buyer_id: AccountId,
|
|
|
|
pub status: OrderStatus,
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
db_async_handler!(CreateOrderItem, create_order_item, OrderItem);
|
2022-04-16 07:31:19 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn create_order_item(msg: CreateOrderItem, db: PgPool) -> Result<OrderItem> {
|
2022-04-16 07:31:19 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
INSERT INTO order_items (buyer_id, status)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
RETURNING id, buyer_id, status
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.buyer_id)
|
|
|
|
.bind(msg.status)
|
|
|
|
.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,
|
|
|
|
}
|
|
|
|
|
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#"
|
|
|
|
SELECT id, buyer_id, status
|
|
|
|
FROM order_items
|
|
|
|
WHERE id = $1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.id)
|
|
|
|
.fetch_one(&db)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
log::error!("{e:?}");
|
|
|
|
super::Error::OrderItem(Error::NotExists)
|
|
|
|
})
|
|
|
|
}
|