Handle new fields

This commit is contained in:
eraden 2022-04-18 08:22:51 +02:00
parent b04f73cb51
commit 4be0f69511
3 changed files with 94 additions and 14 deletions

View File

@ -31,7 +31,7 @@ pub(crate) async fn all_account_orders(
) -> Result<Vec<AccountOrder>> {
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<OrderId>,
}
db_async_handler!(CreateAccountOrder, create_account_order, AccountOrder);
@ -58,13 +59,49 @@ pub(crate) async fn create_account_order(
) -> Result<AccountOrder> {
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<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| {
@ -84,7 +121,7 @@ db_async_handler!(FindAccountOrder, find_account_order, AccountOrder);
pub(crate) async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result<AccountOrder> {
sqlx::query_as(
r#"
SELECT id, buyer_id, status
SELECT id, buyer_id, status, order_id
FROM account_orders
WHERE id = $1
"#,

View File

@ -28,7 +28,7 @@ db_async_handler!(AllAccounts, all_accounts, Vec<FullAccount>);
pub(crate) async fn all_accounts(_msg: AllAccounts, pool: PgPool) -> Result<Vec<FullAccount>> {
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<Ful
r#"
INSERT INTO accounts (login, email, role, pass_hash)
VALUES ($1, $2, $3, $4)
RETURNING id, email, login, pass_hash, role
RETURNING id, email, login, pass_hash, role, customer_id
"#,
)
.bind(msg.login)
.bind(msg.email)
.bind(msg.role)
.bind(msg.pass_hash)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::Account(Error::CantCreate)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<FullAccount>")]
pub struct UpdateAccount {
pub id: AccountId,
pub email: Email,
pub login: Login,
pub pass_hash: PassHash,
pub role: Role,
}
db_async_handler!(UpdateAccount, update_account, FullAccount);
pub(crate) async fn update_account(msg: UpdateAccount, db: PgPool) -> Result<FullAccount> {
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<FullAccount> {
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
"#,

View File

@ -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<FullAccount> 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<OrderId>,
}
#[derive(sqlx::Type, Serialize, Deserialize, Copy, Clone, Deref)]