diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2024-11-06 18:19:31 +0100 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2024-11-07 17:06:16 +0100 |
| commit | 696af52b9b3a732b369a771e1f6409d8e2049ea9 (patch) | |
| tree | cf28d09e396702a465c29fbbe5a638fba8a5569b /src/db/product.rs | |
| parent | 3fc548058bac5ef33f3955f4188bf6bea9ac9311 (diff) | |
Refactoring: Add product post method
Diffstat (limited to 'src/db/product.rs')
| -rw-r--r-- | src/db/product.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/db/product.rs b/src/db/product.rs index 0fc8f05..5b58560 100644 --- a/src/db/product.rs +++ b/src/db/product.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; +use std::time::SystemTime; -use sqlx::SqlitePool; +use sqlx::{Sqlite, SqlitePool, Transaction}; use crate::error::AppError; @@ -15,6 +16,22 @@ pub struct Product { pub updatedate: i64, } +impl Product { + pub async fn post(tx: &mut Transaction<'static, Sqlite>, name: String, description: String) -> Result<Product, AppError> { + let now = SystemTime::now().duration_since(std::time::UNIX_EPOCH)?.as_millis() as i64; + let product = sqlx::query_as!( + Product, + "INSERT INTO product (name, description, uploaddate, updatedate) VALUES ($1, $2, $3, $4) RETURNING id, name, description, uploaddate, updatedate", + name, + description, + now, + now) + .fetch_one(&mut **tx) + .await?; + Ok(product) + } +} + pub trait ProductMarker { fn product_id(&self) -> i64; } |
