2022-04-16 18:57:37 +02:00
|
|
|
use crate::db_async_handler;
|
2022-04-16 06:58:48 +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 {
|
2022-04-16 07:31:19 +02:00
|
|
|
#[error("Can't create account order")]
|
2022-04-16 06:58:48 +02:00
|
|
|
CantCreate,
|
2022-04-16 07:31:19 +02:00
|
|
|
#[error("Can't find account order does to lack of identity")]
|
2022-04-16 06:58:48 +02:00
|
|
|
NoIdentity,
|
|
|
|
#[error("Account order does not exists")]
|
|
|
|
NotExists,
|
2022-04-16 07:31:19 +02:00
|
|
|
#[error("Failed to load all account orders")]
|
2022-04-16 06:58:48 +02:00
|
|
|
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 06:58:48 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn all_account_orders(
|
|
|
|
_msg: AllAccountOrders,
|
|
|
|
pool: PgPool,
|
|
|
|
) -> Result<Vec<AccountOrder>> {
|
2022-04-16 06:58:48 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
SELECT id, buyer_id, status
|
|
|
|
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-16 18:57:37 +02:00
|
|
|
db_async_handler!(CreateAccountOrder, create_account_order, AccountOrder);
|
2022-04-16 06:58:48 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn create_account_order(
|
|
|
|
msg: CreateAccountOrder,
|
|
|
|
db: PgPool,
|
|
|
|
) -> Result<AccountOrder> {
|
2022-04-16 06:58:48 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
INSERT INTO account_orders (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::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 06:58:48 +02:00
|
|
|
|
2022-04-16 18:57:37 +02:00
|
|
|
pub(crate) async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result<AccountOrder> {
|
2022-04-16 06:58:48 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
SELECT id, buyer_id, status
|
|
|
|
FROM account_orders
|
|
|
|
WHERE id = $1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.id)
|
|
|
|
.fetch_one(&db)
|
|
|
|
.await
|
|
|
|
.map_err(|e| {
|
|
|
|
log::error!("{e:?}");
|
|
|
|
super::Error::AccountOrder(Error::NotExists)
|
|
|
|
})
|
|
|
|
}
|