diff --git a/api/src/actors/database/account_orders.rs b/api/src/actors/database/account_orders.rs index 7809f0f..03bc2e2 100644 --- a/api/src/actors/database/account_orders.rs +++ b/api/src/actors/database/account_orders.rs @@ -31,7 +31,7 @@ pub(crate) async fn all_account_orders( ) -> Result> { sqlx::query_as( r#" -SELECT id, buyer_id, status +SELECT id, buyer_id, status, order_id FROM account_orders "#, ) @@ -48,6 +48,7 @@ FROM account_orders pub struct CreateAccountOrder { pub buyer_id: AccountId, pub status: OrderStatus, + pub order_id: Option, } db_async_handler!(CreateAccountOrder, create_account_order, AccountOrder); @@ -58,13 +59,49 @@ pub(crate) async fn create_account_order( ) -> Result { sqlx::query_as( r#" -INSERT INTO account_orders (buyer_id, status) -VALUES ($1, $2) -RETURNING id, buyer_id, status +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) + .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")] +pub struct UpdateAccountOrder { + pub id: AccountOrderId, + pub buyer_id: AccountId, + pub status: OrderStatus, + pub order_id: Option, +} + +db_async_handler!(UpdateAccountOrder, update_account_order, AccountOrder); + +pub(crate) async fn update_account_order( + msg: UpdateAccountOrder, + db: PgPool, +) -> Result { + 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| { @@ -84,7 +121,7 @@ db_async_handler!(FindAccountOrder, find_account_order, AccountOrder); pub(crate) async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result { sqlx::query_as( r#" -SELECT id, buyer_id, status +SELECT id, buyer_id, status, order_id FROM account_orders WHERE id = $1 "#, diff --git a/api/src/actors/database/accounts.rs b/api/src/actors/database/accounts.rs index b0e0726..2377633 100644 --- a/api/src/actors/database/accounts.rs +++ b/api/src/actors/database/accounts.rs @@ -28,7 +28,7 @@ db_async_handler!(AllAccounts, all_accounts, Vec); pub(crate) async fn all_accounts(_msg: AllAccounts, pool: PgPool) -> Result> { sqlx::query_as( r#" -SELECT id, email, login, pass_hash, role +SELECT id, email, login, pass_hash, role, customer_id FROM accounts "#, ) @@ -56,7 +56,40 @@ pub(crate) async fn create_account(msg: CreateAccount, db: PgPool) -> Result Result { + sqlx::query_as( + r#" +UPDATE accounts +SET login = $2 AND email = $3 AND role = $4 AND pass_hash = $5 +WHERE id = $1 +RETURNING id, email, login, pass_hash, role, customer_id "#, ) .bind(msg.login) @@ -82,7 +115,7 @@ db_async_handler!(FindAccount, find_account, FullAccount); pub(crate) async fn find_account(msg: FindAccount, db: PgPool) -> Result { sqlx::query_as( r#" -SELECT id, email, login, pass_hash, role +SELECT id, email, login, pass_hash, role, customer_id FROM accounts WHERE id = $1 "#, @@ -109,7 +142,7 @@ pub(crate) async fn account_by_identity(msg: AccountByIdentity, db: PgPool) -> R match (msg.login, msg.email) { (Some(login), None) => sqlx::query_as( r#" -SELECT id, email, login, pass_hash, role +SELECT id, email, login, pass_hash, role, customer_id FROM accounts WHERE login = $1 "#, @@ -117,7 +150,7 @@ WHERE login = $1 .bind(login), (None, Some(email)) => sqlx::query_as( r#" -SELECT id, email, login, pass_hash, role +SELECT id, email, login, pass_hash, role, customer_id FROM accounts WHERE email = $1 "#, @@ -125,7 +158,7 @@ WHERE email = $1 .bind(email), (Some(login), Some(email)) => sqlx::query_as( r#" -SELECT id, email, login, pass_hash, role +SELECT id, email, login, pass_hash, role, customer_id FROM accounts WHERE login = $1 AND email = $2 "#, diff --git a/api/src/model.rs b/api/src/model.rs index 192faa7..3ec8566 100644 --- a/api/src/model.rs +++ b/api/src/model.rs @@ -306,6 +306,7 @@ pub struct FullAccount { pub login: Login, pub pass_hash: PassHash, pub role: Role, + pub customer_id: uuid::Uuid, } #[derive(sqlx::FromRow, Serialize, Deserialize)] @@ -314,11 +315,14 @@ pub struct Account { pub email: Email, pub login: Login, pub role: Role, + pub customer_id: uuid::Uuid, } impl From for Account { - fn from(FullAccount { id, email, login, pass_hash: _, role }: FullAccount) -> Self { - Self { id, email, login, role } + fn from( + FullAccount { id, email, login, pass_hash: _, role, customer_id }: FullAccount, + ) -> Self { + Self { id, email, login, role, customer_id } } } @@ -375,13 +379,19 @@ pub struct Stock { #[derive(sqlx::Type, Serialize, Deserialize, Deref)] #[sqlx(transparent)] #[serde(transparent)] -pub struct AccountOrderId(pub RecordId); +pub struct AccountOrderId(RecordId); + +#[derive(sqlx::Type, Serialize, Deserialize, Deref)] +#[sqlx(transparent)] +#[serde(transparent)] +pub struct OrderId(String); #[derive(sqlx::FromRow, Serialize, Deserialize)] pub struct AccountOrder { pub id: AccountOrderId, pub buyer_id: AccountId, pub status: OrderStatus, + pub order_id: Option, } #[derive(sqlx::Type, Serialize, Deserialize, Copy, Clone, Deref)]