Create product
This commit is contained in:
parent
ccaa039fe6
commit
0d7e72239b
@ -28,6 +28,12 @@ pub enum Error {
|
|||||||
VariantStocks(Vec<ProductVariantId>),
|
VariantStocks(Vec<ProductVariantId>),
|
||||||
#[error("Failed to load stocks for products variants {0:?}")]
|
#[error("Failed to load stocks for products variants {0:?}")]
|
||||||
VariantPhotos(Vec<ProductVariantId>),
|
VariantPhotos(Vec<ProductVariantId>),
|
||||||
|
#[error("Failed to create product")]
|
||||||
|
CreateProduct,
|
||||||
|
#[error("Failed to create variant of product {0:?}")]
|
||||||
|
CreateProductVariant(ProductId),
|
||||||
|
#[error("Failed to create stock of variant {0:?}")]
|
||||||
|
CreateVariantStock(ProductVariantId),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod rpc {
|
pub mod rpc {
|
||||||
|
@ -28,9 +28,7 @@ pub mod create_product {
|
|||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Details {
|
pub struct Details {
|
||||||
pub product: model::Product,
|
pub product: DetailedProduct,
|
||||||
pub stocks: Vec<Stock>,
|
|
||||||
pub photos: Vec<Photo>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Output = Result<Details, Error>;
|
pub type Output = Result<Details, Error>;
|
||||||
|
@ -1,16 +1,72 @@
|
|||||||
use channels::stocks::{create_product, delete_product, update_product};
|
use channels::stocks::{create_product, delete_product, update_product, Error};
|
||||||
use channels::AsyncClient;
|
use channels::AsyncClient;
|
||||||
use config::SharedAppConfig;
|
use config::SharedAppConfig;
|
||||||
|
use db_utils::PgT;
|
||||||
|
|
||||||
|
use crate::begin_t;
|
||||||
use crate::db::Database;
|
use crate::db::Database;
|
||||||
|
|
||||||
pub async fn create_product(
|
pub async fn create_product(
|
||||||
_input: create_product::Input,
|
input: create_product::Input,
|
||||||
_db: Database,
|
db: Database,
|
||||||
_mqtt: AsyncClient,
|
_mqtt: AsyncClient,
|
||||||
_config: SharedAppConfig,
|
_config: SharedAppConfig,
|
||||||
) -> create_product::Output {
|
) -> create_product::Output {
|
||||||
todo!()
|
let mut t = begin_t!(db, Error::InternalServerError);
|
||||||
|
|
||||||
|
let res = inner_create_product(input, &mut t, _mqtt, _config).await;
|
||||||
|
|
||||||
|
t.commit().await.ok();
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn inner_create_product(
|
||||||
|
input: create_product::Input,
|
||||||
|
t: &mut PgT<'_>,
|
||||||
|
_mqtt: AsyncClient,
|
||||||
|
_config: SharedAppConfig,
|
||||||
|
) -> create_product::Output {
|
||||||
|
let dbm = crate::db::CreateProduct {
|
||||||
|
name: input.product.name.clone(),
|
||||||
|
category: input.product.category,
|
||||||
|
deliver_days_flag: input.product.deliver_days_flag,
|
||||||
|
};
|
||||||
|
let product = match dbm.run(t).await {
|
||||||
|
Ok(product) => product,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("{}", e);
|
||||||
|
return Err(Error::CreateProduct);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let dbm = crate::db::CreateProductVariant {
|
||||||
|
product_id: product.id,
|
||||||
|
name: input.product.name,
|
||||||
|
short_description: input.product.short_description,
|
||||||
|
long_description: input.product.long_description,
|
||||||
|
price: input.product.price,
|
||||||
|
};
|
||||||
|
let variant = match dbm.run(t).await {
|
||||||
|
Ok(variant) => variant,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("{}", e);
|
||||||
|
return Err(Error::CreateProductVariant(product.id));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let dbm = crate::db::CreateStock {
|
||||||
|
product_variant_id: variant.id,
|
||||||
|
quantity: input.stock.quantity,
|
||||||
|
quantity_unit: input.stock.quantity_unit,
|
||||||
|
};
|
||||||
|
let stock = match dbm.run(t).await {
|
||||||
|
Ok(stock) => stock,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("{}", e);
|
||||||
|
return Err(Error::CreateVariantStock(variant.id));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_product(
|
pub async fn update_product(
|
||||||
|
Loading…
Reference in New Issue
Block a user