From 6eb60f4223a9ddb2bf927c2f354028aca4f64829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Tue, 29 Nov 2022 11:21:31 +0100 Subject: [PATCH] Replace entire category to db --- crates/model/src/api.rs | 19 ++-- crates/model/src/lib.rs | 100 +------------------- crates/stock_manager/src/actions/product.rs | 16 +++- 3 files changed, 21 insertions(+), 114 deletions(-) diff --git a/crates/model/src/api.rs b/crates/model/src/api.rs index 48904d0..64e3301 100644 --- a/crates/model/src/api.rs +++ b/crates/model/src/api.rs @@ -274,18 +274,6 @@ pub struct Category { pub svg: CategorySvg, } -impl From<&crate::Category> for Category { - fn from(&crate::Category { name, key, svg }: &crate::Category) -> Self { - Self { - id: Default::default(), - parent_id: None, - name: CategoryName::from(name), - key: CategoryKey::from(key), - svg: CategorySvg::from(svg), - } - } -} - #[derive(Serialize, Deserialize, Debug, Hash)] pub struct Product { pub id: ProductId, @@ -305,6 +293,7 @@ impl<'path> crate::Product, &mut Vec, &mut Vec, + &[crate::v2::Category], &'path str, )> for Product { @@ -321,11 +310,13 @@ impl<'path> }, photos, product_stocks, + categories, public_path, ): ( crate::Product, &mut Vec, &mut Vec, + &[crate::v2::Category], &'path str, ), ) -> Self { @@ -336,12 +327,14 @@ impl<'path> .map(|idx| product_stocks.remove(idx)) .map(|stock| (**stock.quantity > 0, stock.quantity_unit)) .unwrap_or_else(|| (false, QuantityUnit::Piece)); + Self { id, name, short_description, long_description, - category: category.and_then(CategoryMapper::api_from_product_category), + category: category + .and_then(|category| CategoryMapper::db_into_api(category, categories)), price, available, quantity_unit, diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 8de040a..02b233b 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -14,7 +14,7 @@ use serde::de::{Error, Visitor}; use serde::{Deserialize, Deserializer, Serialize}; pub use crate::encrypt::*; -use crate::v2::{CategoryKey, CategoryName, CategorySvg, ProductVariantId}; +use crate::v2::ProductVariantId; #[derive(Debug, Hash, thiserror::Error)] pub enum TransformError { @@ -28,43 +28,6 @@ pub enum TransformError { pub type RecordId = i32; -#[derive(Clone, Debug, Hash, Deserialize, Serialize)] -#[serde(rename_all = "snake_case")] -pub struct Category { - pub name: &'static str, - pub key: &'static str, - pub svg: &'static str, -} - -impl Category { - pub const CAMERAS_NAME: &'static str = "Cameras"; - pub const CAMERAS_KEY: &'static str = "cameras"; - - pub const DRUGSTORE_NAME: &'static str = "Drugstore"; - pub const DRUGSTORE_KEY: &'static str = "drugstore"; - - pub const SPEAKERS_NAME: &'static str = "Speakers"; - pub const SPEAKERS_KEY: &'static str = "speakers"; - - pub const PHONES_NAME: &'static str = "Phones"; - pub const PHONES_KEY: &'static str = "phones"; - - pub const SWEETS_NAME: &'static str = "Sweets"; - pub const SWEETS_KEY: &'static str = "sweets"; - - pub const MEMORY_NAME: &'static str = "Memory"; - pub const MEMORY_KEY: &'static str = "memory"; - - pub const PANTS_NAME: &'static str = "Pants"; - pub const PANTS_KEY: &'static str = "pants"; - - pub const CLOTHES_NAME: &'static str = "Clothes"; - pub const CLOTHES_KEY: &'static str = "clothes"; - - pub const PLATES_NAME: &'static str = "Plates"; - pub const PLATES_KEY: &'static str = "plates"; -} - macro_rules! category_svg { ($name: expr) => { concat!("/svg/", $name, ".svg") @@ -74,67 +37,6 @@ macro_rules! category_svg { pub struct CategoryMapper; impl CategoryMapper { - pub const CATEGORIES: [Category; 9] = [ - Category { - name: Category::CAMERAS_NAME, - key: Category::CAMERAS_KEY, - svg: category_svg!("cameras"), - }, - Category { - name: Category::DRUGSTORE_NAME, - key: Category::DRUGSTORE_KEY, - svg: category_svg!("drugstore"), - }, - Category { - name: Category::SPEAKERS_NAME, - key: Category::SPEAKERS_KEY, - svg: category_svg!("speakers"), - }, - Category { - name: Category::PHONES_NAME, - key: Category::PHONES_KEY, - svg: category_svg!("phones"), - }, - Category { - name: Category::SWEETS_NAME, - key: Category::SWEETS_KEY, - svg: category_svg!("sweets"), - }, - Category { - name: Category::MEMORY_NAME, - key: Category::MEMORY_KEY, - svg: category_svg!("memory"), - }, - Category { - name: Category::PANTS_NAME, - key: Category::PANTS_KEY, - svg: category_svg!("pants"), - }, - Category { - name: Category::CLOTHES_NAME, - key: Category::CLOTHES_KEY, - svg: category_svg!("clothes"), - }, - Category { - name: Category::PLATES_NAME, - key: Category::PLATES_KEY, - svg: category_svg!("plates"), - }, - ]; - - pub fn api_from_product_category(name: ProductCategory) -> Option { - Self::CATEGORIES - .iter() - .find(|category| category.name == name.as_str()) - .map(|&Category { name, key, svg }| crate::api::Category { - id: Default::default(), - parent_id: None, - name: CategoryName::from(name), - key: CategoryKey::from(key), - svg: CategorySvg::from(svg), - }) - } - pub fn db_into_api( name: ProductCategory, categories: &[crate::v2::Category], diff --git a/crates/stock_manager/src/actions/product.rs b/crates/stock_manager/src/actions/product.rs index 66e44d6..adfac4d 100644 --- a/crates/stock_manager/src/actions/product.rs +++ b/crates/stock_manager/src/actions/product.rs @@ -6,7 +6,7 @@ use model::v2::*; use model::CategoryMapper; use crate::begin_t; -use crate::db::Database; +use crate::db::{AllCategories, Database}; pub async fn create_product( input: create_product::Input, @@ -72,6 +72,18 @@ async fn inner_create_product( } }; + let dbm = AllCategories { + limit: Limit::from_u32(2000), + offset: Offset::from_u32(0), + }; + let categories = match dbm.run(&mut *t).await { + Ok(categories) => categories, + Err(e) => { + tracing::warn!("{}", e); + return Err(Error::AllCategories); + } + }; + let dbm = crate::db::CreateStock { product_variant_id: variant.id, quantity: input.stock.quantity, @@ -91,7 +103,7 @@ async fn inner_create_product( name: product.name, category: product .category - .and_then(CategoryMapper::api_from_product_category), + .and_then(|category| CategoryMapper::db_into_api(category, &categories)), deliver_days_flag: product.deliver_days_flag, variants: vec![DetailedProductVariant { id: variant.id,