bazzar/crates/stock_manager/src/db/product_variants.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

2022-11-09 16:59:12 +01:00
use model::v2::*;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("")]
CreateProductVariant,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct CreateProductVariant {
pub product_id: ProductId,
pub name: ProductName,
pub short_description: ProductShortDesc,
pub long_description: ProductLongDesc,
pub price: Price,
}
impl CreateProductVariant {
pub async fn run(
self,
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<ProductVariant> {
sqlx::query_as(
r#"
INSERT INTO product_variants (
product_id,
name,
short_description,
long_description,
price
) VALUES ($1, $2, $3, $4, $5)
RETURNINGS id,
product_id,
name,
short_description,
long_description,
price
"#,
)
.bind(self.product_id)
.bind(self.name)
.bind(self.short_description)
.bind(self.long_description)
.bind(self.price)
.fetch_one(pool)
.await
.map_err(|e| {
tracing::error!("{}", e);
dbg!(e);
Error::CreateProductVariant
})
}
}