Db shopping carts

This commit is contained in:
eraden 2022-04-16 09:30:11 +02:00
parent 388bfd634a
commit 160c1f2563
2 changed files with 127 additions and 0 deletions

View File

@ -5,12 +5,14 @@ pub use account_orders::*;
pub use accounts::*;
pub use order_items::*;
pub use products::*;
pub use shopping_carts::*;
pub use stocks::*;
mod account_orders;
mod accounts;
mod order_items;
mod products;
mod shopping_carts;
mod stocks;
#[macro_export]
@ -41,6 +43,8 @@ pub enum Error {
Stock(stocks::Error),
#[error("{0}")]
OrderItem(order_items::Error),
#[error("{0}")]
ShoppingCart(shopping_carts::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -0,0 +1,123 @@
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 shopping cart")]
CantCreate,
#[error("Can't find shopping cart does to lack of identity")]
NoIdentity,
#[error("Shopping cart does not exists")]
NotExists,
#[error("Failed to load all shopping carts")]
All,
#[error("Failed to load account shopping carts")]
AccountCarts,
}
#[derive(actix::Message)]
#[rtype(result = "Result<Vec<ShoppingCart>>")]
pub struct AllShoppingCarts;
async_handler!(AllShoppingCarts, all_shopping_carts, Vec<ShoppingCart>);
pub async fn all_shopping_carts(_msg: AllShoppingCarts, pool: PgPool) -> Result<Vec<ShoppingCart>> {
sqlx::query_as(
r#"
SELECT id, buyer_id, payment_method
FROM shopping_carts
"#,
)
.fetch_all(&pool)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::ShoppingCart(Error::All)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<Vec<ShoppingCart>>")]
pub struct AccountShoppingCarts {
pub account_id: AccountId,
}
async_handler!(AccountShoppingCarts, account_shopping_carts, Vec<ShoppingCart>);
pub async fn account_shopping_carts(
msg: AccountShoppingCarts,
pool: PgPool,
) -> Result<Vec<ShoppingCart>> {
sqlx::query_as(
r#"
SELECT id, buyer_id, payment_method
FROM shopping_carts
WHERE buyer_id = $1
"#,
)
.bind(msg.acocunt_id)
.fetch_all(&pool)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::ShoppingCart(Error::AccountCarts)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<ShoppingCart>")]
pub struct CreateShoppingCart {
pub buyer_id: AccountId,
pub payment_method: PaymentMethod,
}
async_handler!(CreateShoppingCart, create_shopping_cart, ShoppingCart);
async fn create_shopping_cart(msg: CreateShoppingCart, db: PgPool) -> Result<ShoppingCart> {
sqlx::query_as(
r#"
INSERT INTO shopping_carts (buyer_id, payment_method)
VALUES ($1, $2)
RETURNING id, buyer_id, payment_method
"#,
)
.bind(msg.buyer_id)
.bind(msg.payment_method)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::ShoppingCart(Error::CantCreate)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<ShoppingCart>")]
pub struct FindShoppingCart {
pub id: ShoppingCartId,
}
async_handler!(FindShoppingCart, find_shopping_cart, ShoppingCart);
async fn find_shopping_cart(msg: FindShoppingCart, db: PgPool) -> Result<ShoppingCart> {
sqlx::query_as(
r#"
SELECT id, buyer_id, payment_method
FROM shopping_carts
WHERE id = $1
"#,
)
.bind(msg.id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::ShoppingCart(Error::NotExists)
})
}