Replace entire category to db

This commit is contained in:
Adrian Woźniak 2022-11-29 11:21:31 +01:00
parent df1594e3f4
commit 6eb60f4223
3 changed files with 21 additions and 114 deletions

View File

@ -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<ProductLinkedPhoto>,
&mut Vec<Stock>,
&[crate::v2::Category],
&'path str,
)> for Product
{
@ -321,11 +310,13 @@ impl<'path>
},
photos,
product_stocks,
categories,
public_path,
): (
crate::Product,
&mut Vec<ProductLinkedPhoto>,
&mut Vec<Stock>,
&[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,

View File

@ -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<crate::api::Category> {
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],

View File

@ -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,