Update shopping cart item
This commit is contained in:
parent
074c3a9900
commit
a84fda2382
@ -90,16 +90,16 @@ impl CartManager {
|
|||||||
|
|
||||||
#[derive(actix::Message)]
|
#[derive(actix::Message)]
|
||||||
#[rtype(result = "Result<ShoppingCartItem>")]
|
#[rtype(result = "Result<ShoppingCartItem>")]
|
||||||
pub struct AddItem {
|
pub struct ModifyItem {
|
||||||
pub buyer_id: AccountId,
|
pub buyer_id: AccountId,
|
||||||
pub product_id: ProductId,
|
pub product_id: ProductId,
|
||||||
pub quantity: Quantity,
|
pub quantity: Quantity,
|
||||||
pub quantity_unit: QuantityUnit,
|
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!(
|
let _cart = query_db!(
|
||||||
db,
|
db,
|
||||||
database_manager::EnsureActiveShoppingCart {
|
database_manager::EnsureActiveShoppingCart {
|
||||||
@ -121,17 +121,40 @@ async fn add_item(msg: AddItem, db: actix::Addr<Database>) -> Result<ShoppingCar
|
|||||||
} else {
|
} else {
|
||||||
carts.remove(0)
|
carts.remove(0)
|
||||||
};
|
};
|
||||||
Ok(query_db!(
|
|
||||||
|
let item: Option<model::ShoppingCartItem> = query_db!(
|
||||||
db,
|
db,
|
||||||
database_manager::CreateShoppingCartItem {
|
database_manager::ActiveCartItemByProduct {
|
||||||
product_id: msg.product_id,
|
product_id: msg.product_id
|
||||||
shopping_cart_id: cart.id,
|
|
||||||
quantity: msg.quantity,
|
|
||||||
quantity_unit: msg.quantity_unit,
|
|
||||||
},
|
},
|
||||||
passthrough Error::Db,
|
|
||||||
Error::CantAddItem
|
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,
|
||||||
|
shopping_cart_id: cart.id,
|
||||||
|
quantity: msg.quantity,
|
||||||
|
quantity_unit: msg.quantity_unit,
|
||||||
|
},
|
||||||
|
passthrough Error::Db,
|
||||||
|
Error::CantAddItem
|
||||||
|
)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(actix::Message)]
|
#[derive(actix::Message)]
|
||||||
|
@ -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)]
|
#[derive(actix::Message)]
|
||||||
#[rtype(result = "Result<Vec<ShoppingCartItem>>")]
|
#[rtype(result = "Result<Vec<ShoppingCartItem>>")]
|
||||||
pub struct CartItems {
|
pub struct CartItems {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use actix::Addr;
|
use actix::Addr;
|
||||||
use actix_web::web::{scope, Data, Json, ServiceConfig};
|
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 actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||||
use cart_manager::{query_cart, CartManager};
|
use cart_manager::{query_cart, CartManager};
|
||||||
use database_manager::{query_db, Database};
|
use database_manager::{query_db, Database};
|
||||||
@ -88,8 +88,8 @@ async fn shopping_cart(
|
|||||||
Ok(Json(cart))
|
Ok(Json(cart))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/shopping-cart-item")]
|
#[put("/shopping-cart-item")]
|
||||||
async fn create_cart_item(
|
async fn update_cart_item(
|
||||||
cart: Data<Addr<CartManager>>,
|
cart: Data<Addr<CartManager>>,
|
||||||
tm: Data<Addr<TokenManager>>,
|
tm: Data<Addr<TokenManager>>,
|
||||||
credentials: BearerAuth,
|
credentials: BearerAuth,
|
||||||
@ -99,7 +99,7 @@ async fn create_cart_item(
|
|||||||
|
|
||||||
let item: model::ShoppingCartItem = query_cart!(
|
let item: model::ShoppingCartItem = query_cart!(
|
||||||
cart,
|
cart,
|
||||||
cart_manager::AddItem {
|
cart_manager::ModifyItem {
|
||||||
buyer_id: token.account_id(),
|
buyer_id: token.account_id(),
|
||||||
product_id: payload.product_id,
|
product_id: payload.product_id,
|
||||||
quantity: payload.quantity,
|
quantity: payload.quantity,
|
||||||
@ -108,6 +108,7 @@ async fn create_cart_item(
|
|||||||
routes::Error::Public(super::Error::AddItem.into()),
|
routes::Error::Public(super::Error::AddItem.into()),
|
||||||
routes::Error::Public(PublicError::DatabaseConnection)
|
routes::Error::Public(PublicError::DatabaseConnection)
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Json(api::CreateItemOutput {
|
Ok(Json(api::CreateItemOutput {
|
||||||
success: true,
|
success: true,
|
||||||
shopping_cart_item: item.into(),
|
shopping_cart_item: item.into(),
|
||||||
|
@ -90,7 +90,7 @@ pub struct AccountOrder {
|
|||||||
#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
|
#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct ShoppingCartItem {
|
pub struct ShoppingCartItem {
|
||||||
pub id: ShoppingCartId,
|
pub id: ShoppingCartItemId,
|
||||||
pub product_id: ProductId,
|
pub product_id: ProductId,
|
||||||
pub shopping_cart_id: ShoppingCartId,
|
pub shopping_cart_id: ShoppingCartId,
|
||||||
pub quantity: Quantity,
|
pub quantity: Quantity,
|
||||||
|
@ -932,7 +932,7 @@ pub struct ShoppingCartItemId(RecordId);
|
|||||||
#[cfg_attr(feature = "db", derive(sqlx::FromRow))]
|
#[cfg_attr(feature = "db", derive(sqlx::FromRow))]
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct ShoppingCartItem {
|
pub struct ShoppingCartItem {
|
||||||
pub id: ShoppingCartId,
|
pub id: ShoppingCartItemId,
|
||||||
pub product_id: ProductId,
|
pub product_id: ProductId,
|
||||||
pub shopping_cart_id: ShoppingCartId,
|
pub shopping_cart_id: ShoppingCartId,
|
||||||
pub quantity: Quantity,
|
pub quantity: Quantity,
|
||||||
|
Loading…
Reference in New Issue
Block a user