56 lines
1.2 KiB
Rust
56 lines
1.2 KiB
Rust
|
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
|
||
|
})
|
||
|
}
|
||
|
}
|