bazzar/crates/channels/src/carts.rs

220 lines
5.8 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()
}
}
}
}
2022-11-04 21:26:30 +01:00
pub mod active_shopping_cart {
use super::Error;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Input {
pub buyer_id: model::AccountId,
}
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct Output {
pub error: Option<Error>,
pub cart: Option<model::ShoppingCart>,
}
impl Output {
pub fn error(error: Error) -> Self {
Self {
error: Some(error),
..Default::default()
}
}
pub fn cart(cart: model::ShoppingCart) -> Self {
Self {
cart: Some(cart),
..Default::default()
}
}
}
}
2022-11-04 18:40:14 +01:00
pub mod rpc {
2022-11-04 21:26:30 +01:00
use config::SharedAppConfig;
use super::{active_shopping_cart, modify_cart, modify_item, remove_product};
2022-11-04 18:40:14 +01:00
#[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;
2022-11-04 21:26:30 +01:00
/// Remove product from shopping cart.
async fn remove_product(input: remove_product::Input) -> remove_product::Output;
async fn active_shopping_cart(
input: active_shopping_cart::Input,
) -> active_shopping_cart::Output;
}
pub async fn create_client(config: SharedAppConfig) -> CartsClient {
use tarpc::client;
use tarpc::tokio_serde::formats::Bincode;
let addr = {
let l = config.lock();
2022-11-05 01:08:45 +01:00
(
2022-11-05 19:04:38 +01:00
l.account_manager().rpc_bind.clone(),
2022-11-05 01:08:45 +01:00
l.account_manager().rpc_port,
)
2022-11-04 21:26:30 +01:00
};
let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default);
let client = CartsClient::new(
client::Config::default(),
transport.await.expect("Failed to connect to server"),
)
.spawn();
client
2022-11-04 18:40:14 +01:00
}
}
2022-11-05 10:57:07 +01:00
pub mod mqtt {
use config::SharedAppConfig;
use rumqttc::EventLoop;
use crate::carts::CLIENT_NAME;
use crate::AsyncClient;
pub fn create_client(config: SharedAppConfig) -> (AsyncClient, EventLoop) {
2022-11-05 19:04:38 +01:00
crate::mqtt::create_client(CLIENT_NAME, config.lock().cart_manager().mqtt_addr())
2022-11-05 10:57:07 +01:00
}
}