Update shopping cart item

This commit is contained in:
Adrian Woźniak 2022-05-18 16:10:39 +02:00
parent 074c3a9900
commit a84fda2382
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
5 changed files with 79 additions and 17 deletions

View File

@ -90,16 +90,16 @@ impl CartManager {
#[derive(actix::Message)]
#[rtype(result = "Result<ShoppingCartItem>")]
pub struct AddItem {
pub struct ModifyItem {
pub buyer_id: AccountId,
pub product_id: ProductId,
pub quantity: Quantity,
pub quantity_unit: QuantityUnit,
}
cart_async_handler!(AddItem, add_item, ShoppingCartItem);
cart_async_handler!(ModifyItem, modify_item, ShoppingCartItem);
async fn add_item(msg: AddItem, db: actix::Addr<Database>) -> Result<ShoppingCartItem> {
async fn modify_item(msg: ModifyItem, db: actix::Addr<Database>) -> Result<ShoppingCartItem> {
let _cart = query_db!(
db,
database_manager::EnsureActiveShoppingCart {
@ -121,7 +121,29 @@ async fn add_item(msg: AddItem, db: actix::Addr<Database>) -> Result<ShoppingCar
} else {
carts.remove(0)
};
Ok(query_db!(
let item: Option<model::ShoppingCartItem> = query_db!(
db,
database_manager::ActiveCartItemByProduct {
product_id: msg.product_id
},
Error::CantAddItem
);
match item {
Some(item) => Ok(query_db!(
db,
database_manager::UpdateShoppingCartItem {
id: item.id,
product_id: msg.product_id,
shopping_cart_id: cart.id,
quantity: msg.quantity,
quantity_unit: msg.quantity_unit,
},
passthrough Error::Db,
Error::CantAddItem
)),
None => Ok(query_db!(
db,
database_manager::CreateShoppingCartItem {
product_id: msg.product_id,
@ -131,7 +153,8 @@ async fn add_item(msg: AddItem, db: actix::Addr<Database>) -> Result<ShoppingCar
},
passthrough Error::Db,
Error::CantAddItem
))
)),
}
}
#[derive(actix::Message)]

View File

@ -222,6 +222,44 @@ WHERE id = $1
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<Option<ShoppingCartItem>>")]
pub struct ActiveCartItemByProduct {
pub product_id: model::ProductId,
}
db_async_handler!(
ActiveCartItemByProduct,
active_cart_item_by_product,
Option<ShoppingCartItem>
);
pub(crate) async fn active_cart_item_by_product(
msg: ActiveCartItemByProduct,
db: PgPool,
) -> Result<Option<ShoppingCartItem>> {
sqlx::query_as(
r#"
SELECT shopping_cart_items.id,
shopping_cart_items.product_id,
shopping_cart_items.shopping_cart_id,
shopping_cart_items.quantity,
shopping_cart_items.quantity_unit
FROM shopping_cart_items
INNER JOIN shopping_carts ON shopping_cart_items.shopping_cart_id = shopping_carts.id
WHERE product_id = $1 AND shopping_carts.state = $2
"#,
)
.bind(msg.product_id)
.bind(model::ShoppingCartState::Active)
.fetch_optional(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::ShoppingCartItem(Error::NotExists)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<Vec<ShoppingCartItem>>")]
pub struct CartItems {

View File

@ -1,6 +1,6 @@
use actix::Addr;
use actix_web::web::{scope, Data, Json, ServiceConfig};
use actix_web::{delete, get, post, HttpRequest, HttpResponse};
use actix_web::{delete, get, post, put, HttpRequest, HttpResponse};
use actix_web_httpauth::extractors::bearer::BearerAuth;
use cart_manager::{query_cart, CartManager};
use database_manager::{query_db, Database};
@ -88,8 +88,8 @@ async fn shopping_cart(
Ok(Json(cart))
}
#[post("/shopping-cart-item")]
async fn create_cart_item(
#[put("/shopping-cart-item")]
async fn update_cart_item(
cart: Data<Addr<CartManager>>,
tm: Data<Addr<TokenManager>>,
credentials: BearerAuth,
@ -99,7 +99,7 @@ async fn create_cart_item(
let item: model::ShoppingCartItem = query_cart!(
cart,
cart_manager::AddItem {
cart_manager::ModifyItem {
buyer_id: token.account_id(),
product_id: payload.product_id,
quantity: payload.quantity,
@ -108,6 +108,7 @@ async fn create_cart_item(
routes::Error::Public(super::Error::AddItem.into()),
routes::Error::Public(PublicError::DatabaseConnection)
);
Ok(Json(api::CreateItemOutput {
success: true,
shopping_cart_item: item.into(),

View File

@ -90,7 +90,7 @@ pub struct AccountOrder {
#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
#[derive(Serialize, Deserialize, Debug)]
pub struct ShoppingCartItem {
pub id: ShoppingCartId,
pub id: ShoppingCartItemId,
pub product_id: ProductId,
pub shopping_cart_id: ShoppingCartId,
pub quantity: Quantity,

View File

@ -932,7 +932,7 @@ pub struct ShoppingCartItemId(RecordId);
#[cfg_attr(feature = "db", derive(sqlx::FromRow))]
#[derive(Serialize, Deserialize)]
pub struct ShoppingCartItem {
pub id: ShoppingCartId,
pub id: ShoppingCartItemId,
pub product_id: ProductId,
pub shopping_cart_id: ShoppingCartId,
pub quantity: Quantity,