bazzar/crates/channels/src/carts.rs

148 lines
4.0 KiB
Rust
Raw Normal View History

2022-11-04 18:40:14 +01:00
pub static CLIENT_NAME: &str = "cart-manager";
pub enum Topic {}
#[derive(Debug, Clone, thiserror::Error, serde::Serialize, serde::Deserialize)]
pub enum Error {
#[error("Internal server error")]
InternalServerError,
#[error("Failed to load account shopping carts")]
NoCarts,
#[error("Account does not have active shopping cart")]
NoActiveCart,
#[error("Failed to delete item {0:?}")]
DeleteItem(model::ShoppingCartItemId),
#[error("Failed to modify item {0:?}")]
ModifyItem(model::ShoppingCartItemId),
#[error("Failed to create item")]
CreateItem,
#[error("Failed to modify cart {0:?}")]
ModifyCart(model::ShoppingCartId),
#[error("Failed to load cart {0:?} items")]
LoadItems(model::ShoppingCartId),
}
pub mod remove_product {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub shopping_cart_id: model::ShoppingCartId,
pub shopping_cart_item_id: model::ShoppingCartItemId,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub item: Option<model::ShoppingCartItem>,
pub error: Option<Error>,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn item(item: model::ShoppingCartItem) -> Self {
Self {
item: Some(item),
..Default::default()
}
}
}
}
pub mod modify_item {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
pub product_id: model::ProductId,
pub quantity: model::Quantity,
pub quantity_unit: model::QuantityUnit,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub item: Option<model::ShoppingCartItem>,
pub error: Option<Error>,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn item(item: model::ShoppingCartItem) -> Self {
Self {
item: Some(item),
..Default::default()
}
}
}
}
pub mod modify_cart {
use super::{modify_item, Error};
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
pub items: Vec<modify_item::Input>,
pub checkout_notes: String,
pub payment_method: Option<model::PaymentMethod>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct CartDetails {
pub cart_id: model::ShoppingCartId,
pub items: Vec<model::ShoppingCartItem>,
pub checkout_notes: String,
pub payment_method: model::PaymentMethod,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub cart: Option<CartDetails>,
pub error: Option<Error>,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn cart(cart: CartDetails) -> Self {
Self {
cart: Some(cart),
..Default::default()
}
}
}
}
pub mod rpc {
use super::{modify_cart, modify_item, remove_product};
#[tarpc::service]
pub trait Carts {
/// Change shopping cart item.
async fn modify_item(input: modify_item::Input) -> modify_item::Output;
/// Change entire shopping cart content.
async fn modify_cart(input: modify_cart::Input) -> modify_cart::Output;
/// Remove entire shopping cart.
async fn remove_cart(input: remove_product::Input) -> remove_product::Output;
}
}