bazzar/api/src/actors/database/account_orders.rs

135 lines
3.1 KiB
Rust
Raw Normal View History

use sqlx::PgPool;
2022-04-18 22:07:52 +02:00
use super::Result;
use crate::database::Database;
2022-04-18 22:07:52 +02:00
use crate::db_async_handler;
use crate::model::*;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Can't create account order")]
CantCreate,
#[error("Can't find account order does to lack of identity")]
NoIdentity,
#[error("Account order does not exists")]
NotExists,
#[error("Failed to load all account orders")]
All,
}
#[derive(actix::Message)]
#[rtype(result = "Result<Vec<AccountOrder>>")]
pub struct AllAccountOrders;
2022-04-16 18:57:37 +02:00
db_async_handler!(AllAccountOrders, all_account_orders, Vec<AccountOrder>);
2022-04-16 18:57:37 +02:00
pub(crate) async fn all_account_orders(
_msg: AllAccountOrders,
pool: PgPool,
) -> Result<Vec<AccountOrder>> {
sqlx::query_as(
r#"
2022-04-18 08:22:51 +02:00
SELECT id, buyer_id, status, order_id
FROM account_orders
"#,
)
.fetch_all(&pool)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::AccountOrder(Error::All)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<AccountOrder>")]
pub struct CreateAccountOrder {
pub buyer_id: AccountId,
pub status: OrderStatus,
2022-04-18 08:22:51 +02:00
pub order_id: Option<OrderId>,
}
2022-04-16 18:57:37 +02:00
db_async_handler!(CreateAccountOrder, create_account_order, AccountOrder);
2022-04-16 18:57:37 +02:00
pub(crate) async fn create_account_order(
msg: CreateAccountOrder,
db: PgPool,
) -> Result<AccountOrder> {
sqlx::query_as(
r#"
2022-04-18 08:22:51 +02:00
INSERT INTO account_orders (buyer_id, status, order_id)
VALUES ($1, $2, $3)
RETURNING id, buyer_id, status, order_id
"#,
)
.bind(msg.buyer_id)
.bind(msg.status)
2022-04-18 08:22:51 +02:00
.bind(msg.order_id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::AccountOrder(Error::CantCreate)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<AccountOrder>")]
pub struct UpdateAccountOrder {
pub id: AccountOrderId,
pub buyer_id: AccountId,
pub status: OrderStatus,
pub order_id: Option<OrderId>,
}
db_async_handler!(UpdateAccountOrder, update_account_order, AccountOrder);
pub(crate) async fn update_account_order(
msg: UpdateAccountOrder,
db: PgPool,
) -> Result<AccountOrder> {
sqlx::query_as(
r#"
UPDATE account_orders
SET buyer_id = $2 AND status = $3 AND order_id = $4
WHERE id = $1
RETURNING id, buyer_id, status, order_id
"#,
)
.bind(msg.id)
.bind(msg.buyer_id)
.bind(msg.status)
.bind(msg.order_id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::AccountOrder(Error::CantCreate)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<AccountOrder>")]
pub struct FindAccountOrder {
pub id: AccountOrderId,
}
2022-04-16 18:57:37 +02:00
db_async_handler!(FindAccountOrder, find_account_order, AccountOrder);
2022-04-16 18:57:37 +02:00
pub(crate) async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result<AccountOrder> {
sqlx::query_as(
r#"
2022-04-18 08:22:51 +02:00
SELECT id, buyer_id, status, order_id
FROM account_orders
WHERE id = $1
"#,
)
.bind(msg.id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::AccountOrder(Error::NotExists)
})
}