Add account orders db, add shopping cart sql

This commit is contained in:
eraden 2022-04-16 06:58:48 +02:00
parent d2634598d5
commit 5354d909b3
5 changed files with 153 additions and 0 deletions

View File

@ -1,10 +1,12 @@
use actix::{Actor, Context};
use sqlx::PgPool;
pub use account_orders::*;
pub use accounts::*;
pub use products::*;
pub use stocks::*;
mod account_orders;
mod accounts;
mod products;
mod stocks;
@ -30,6 +32,8 @@ pub enum Error {
#[error("{0}")]
Account(accounts::Error),
#[error("{0}")]
AccountOrder(account_orders::Error),
#[error("{0}")]
Product(products::Error),
#[error("{0}")]
Stock(stocks::Error),

View 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)
})
}

View File

@ -15,6 +15,29 @@ pub enum Error {
NoIdentity,
#[error("Account does not exists")]
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)]

View File

@ -226,3 +226,15 @@ pub struct Stock {
pub quantity: Quantity,
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,
}

View 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 )
);