bazzar/shared/model/src/api.rs

197 lines
5.3 KiB
Rust
Raw Normal View History

2022-05-06 11:47:18 +02:00
use serde::Serialize;
2022-05-08 14:59:59 +02:00
use crate::ProductLinkedPhoto;
2022-05-06 11:47:18 +02:00
#[derive(Serialize, Debug)]
#[serde(transparent)]
pub struct AccountOrders(pub Vec<AccountOrder>);
impl From<(Vec<crate::AccountOrder>, Vec<crate::OrderItem>)> for AccountOrders {
fn from((orders, mut items): (Vec<crate::AccountOrder>, Vec<crate::OrderItem>)) -> Self {
Self(
orders
.into_iter()
.map(
|crate::AccountOrder {
id,
buyer_id,
status,
order_id,
order_ext_id: _,
service_order_id: _,
}| {
AccountOrder {
id,
buyer_id,
status,
order_id,
items: items.drain_filter(|item| item.order_id == id).collect(),
}
},
)
.collect(),
)
}
}
impl From<(crate::AccountOrder, Vec<crate::OrderItem>)> for AccountOrder {
fn from(
(
crate::AccountOrder {
id,
buyer_id,
status,
order_id,
order_ext_id: _,
service_order_id: _,
},
mut items,
): (crate::AccountOrder, Vec<crate::OrderItem>),
) -> Self {
AccountOrder {
id,
buyer_id,
status,
order_id,
items: items.drain_filter(|item| item.order_id == id).collect(),
}
}
}
#[derive(Serialize, Debug)]
pub struct AccountOrder {
pub id: crate::AccountOrderId,
pub buyer_id: crate::AccountId,
pub status: crate::OrderStatus,
pub order_id: Option<crate::OrderId>,
pub items: Vec<crate::OrderItem>,
}
#[derive(Serialize, Debug)]
pub struct ShoppingCartItem {
pub id: crate::ShoppingCartId,
pub product_id: crate::ProductId,
pub shopping_cart_id: crate::ShoppingCartId,
pub quantity: crate::Quantity,
pub quantity_unit: crate::QuantityUnit,
}
#[derive(Serialize, Debug)]
pub struct ShoppingCart {
pub id: crate::ShoppingCartId,
pub buyer_id: crate::AccountId,
pub payment_method: crate::PaymentMethod,
pub state: crate::ShoppingCartState,
pub items: Vec<ShoppingCartItem>,
}
impl From<(crate::ShoppingCart, Vec<crate::ShoppingCartItem>)> for ShoppingCart {
fn from(
(
crate::ShoppingCart {
id,
buyer_id,
payment_method,
state,
},
items,
): (crate::ShoppingCart, Vec<crate::ShoppingCartItem>),
) -> Self {
Self {
id,
buyer_id,
payment_method,
state,
items: items
.into_iter()
.map(
|crate::ShoppingCartItem {
id,
product_id,
shopping_cart_id,
quantity,
quantity_unit,
}| ShoppingCartItem {
id,
product_id,
shopping_cart_id,
quantity,
quantity_unit,
},
)
.collect(),
}
}
}
2022-05-08 14:59:59 +02:00
#[derive(Serialize, Debug)]
pub struct Photo {
pub id: crate::PhotoId,
pub file_name: crate::FileName,
}
#[derive(Serialize, Debug)]
pub struct Product {
pub id: crate::ProductId,
pub name: crate::ProductName,
pub short_description: crate::ProductShortDesc,
pub long_description: crate::ProductLongDesc,
pub category: Option<crate::ProductCategory>,
pub price: crate::Price,
pub deliver_days_flag: crate::Days,
pub photos: Vec<Photo>,
}
impl From<(crate::Product, &mut Vec<crate::ProductLinkedPhoto>)> for Product {
fn from(
(
crate::Product {
id,
name,
short_description,
long_description,
category,
price,
deliver_days_flag,
},
photos,
): (crate::Product, &mut Vec<ProductLinkedPhoto>),
) -> Self {
Self {
id,
name,
short_description,
long_description,
category,
price,
deliver_days_flag,
photos: photos
.drain_filter(|photo| photo.product_id == id)
.map(
|ProductLinkedPhoto {
id,
local_path: _,
file_name,
product_id: _,
}| Photo { id, file_name },
)
.collect(),
}
}
}
#[derive(Serialize, Debug)]
#[serde(transparent)]
pub struct Products(Vec<Product>);
impl From<(Vec<crate::Product>, Vec<crate::ProductLinkedPhoto>)> for Products {
fn from((products, mut photos): (Vec<crate::Product>, Vec<crate::ProductLinkedPhoto>)) -> Self {
Self(
products
.into_iter()
.map(|p| (p, &mut photos).into())
.collect(),
)
}
}