Add account orders db, add shopping cart sql
This commit is contained in:
parent
d2634598d5
commit
5354d909b3
@ -1,10 +1,12 @@
|
|||||||
use actix::{Actor, Context};
|
use actix::{Actor, Context};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
pub use account_orders::*;
|
||||||
pub use accounts::*;
|
pub use accounts::*;
|
||||||
pub use products::*;
|
pub use products::*;
|
||||||
pub use stocks::*;
|
pub use stocks::*;
|
||||||
|
|
||||||
|
mod account_orders;
|
||||||
mod accounts;
|
mod accounts;
|
||||||
mod products;
|
mod products;
|
||||||
mod stocks;
|
mod stocks;
|
||||||
@ -30,6 +32,8 @@ pub enum Error {
|
|||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Account(accounts::Error),
|
Account(accounts::Error),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
|
AccountOrder(account_orders::Error),
|
||||||
|
#[error("{0}")]
|
||||||
Product(products::Error),
|
Product(products::Error),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Stock(stocks::Error),
|
Stock(stocks::Error),
|
||||||
|
93
api/src/actors/database/account_orders.rs
Normal file
93
api/src/actors/database/account_orders.rs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
use crate::async_handler;
|
||||||
|
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 account")]
|
||||||
|
CantCreate,
|
||||||
|
#[error("Can't find account does to lack of identity")]
|
||||||
|
NoIdentity,
|
||||||
|
#[error("Account order does not exists")]
|
||||||
|
NotExists,
|
||||||
|
#[error("Failed to load all accounts")]
|
||||||
|
All,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(actix::Message)]
|
||||||
|
#[rtype(result = "Result<Vec<AccountOrder>>")]
|
||||||
|
pub struct AllAccountOrders;
|
||||||
|
|
||||||
|
async_handler!(AllAccountOrders, all_account_orders, Vec<AccountOrder>);
|
||||||
|
|
||||||
|
pub async fn all_account_orders(_msg: AllAccountOrders, pool: PgPool) -> Result<Vec<AccountOrder>> {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
async_handler!(CreateAccountOrder, create_account_order, AccountOrder);
|
||||||
|
|
||||||
|
async fn create_account_order(msg: CreateAccountOrder, db: PgPool) -> Result<AccountOrder> {
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
async_handler!(FindAccountOrder, find_account_order, AccountOrder);
|
||||||
|
|
||||||
|
async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result<AccountOrder> {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
}
|
@ -15,6 +15,29 @@ pub enum Error {
|
|||||||
NoIdentity,
|
NoIdentity,
|
||||||
#[error("Account does not exists")]
|
#[error("Account does not exists")]
|
||||||
NotExists,
|
NotExists,
|
||||||
|
#[error("Failed to load all accounts")]
|
||||||
|
All,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(actix::Message)]
|
||||||
|
#[rtype(result = "Result<Vec<FullAccount>>")]
|
||||||
|
pub struct AllAccounts;
|
||||||
|
|
||||||
|
async_handler!(AllAccounts, all_accounts, Vec<FullAccount>);
|
||||||
|
|
||||||
|
pub async fn all_accounts(_msg: AllAccounts, pool: PgPool) -> Result<Vec<FullAccount>> {
|
||||||
|
sqlx::query_as(
|
||||||
|
r#"
|
||||||
|
SELECT id, email, login, pass_hash, role
|
||||||
|
FROM accounts
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
log::error!("{e:?}");
|
||||||
|
super::Error::Account(Error::All)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(actix::Message)]
|
#[derive(actix::Message)]
|
||||||
|
@ -226,3 +226,15 @@ pub struct Stock {
|
|||||||
pub quantity: Quantity,
|
pub quantity: Quantity,
|
||||||
pub quantity_unit: QuantityUnit,
|
pub quantity_unit: QuantityUnit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(sqlx::Type, Serialize, Deserialize)]
|
||||||
|
#[sqlx(transparent)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
pub struct AccountOrderId(pub RecordId);
|
||||||
|
|
||||||
|
#[derive(sqlx::FromRow, Serialize, Deserialize)]
|
||||||
|
pub struct AccountOrder {
|
||||||
|
pub id: AccountOrderId,
|
||||||
|
pub buyer_id: AccountId,
|
||||||
|
pub status: OrderStatus,
|
||||||
|
}
|
||||||
|
21
db/migrate/202204160624_create_shopping_cart.sql
Normal file
21
db/migrate/202204160624_create_shopping_cart.sql
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
CREATE TYPE "PaymentMethod" AS ENUM (
|
||||||
|
'payu',
|
||||||
|
'payment_on_the_spot'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE shopping_carts
|
||||||
|
(
|
||||||
|
id serial not null primary key,
|
||||||
|
buyer_id int references accounts (id) not null,
|
||||||
|
payment_method "PaymentMethod" NOT NULL DEFAULT 'payment_on_the_spot'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE shopping_cart_items
|
||||||
|
(
|
||||||
|
id serial not null primary key,
|
||||||
|
product_id int references products (id) not null,
|
||||||
|
order_id int references shopping_carts (id),
|
||||||
|
quantity int not null default 0,
|
||||||
|
quantity_unit "QuantityUnit" not null,
|
||||||
|
CONSTRAINT positive_quantity check ( quantity >= 0 )
|
||||||
|
);
|
Loading…
Reference in New Issue
Block a user