diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2026-03-21 11:56:19 +0100 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2026-03-21 11:56:19 +0100 |
| commit | f491ed95a3bc82c31c70b0f38ed841408576ec0c (patch) | |
| tree | 1cfc9a957e150ded75c93fad54028a18640ba7c2 /src/db/product.rs | |
| parent | 74db93c431ebc02b1dc43ec12714007d005a9704 (diff) | |
Purge + prepare for local website builder
Diffstat (limited to 'src/db/product.rs')
| -rw-r--r-- | src/db/product.rs | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/src/db/product.rs b/src/db/product.rs deleted file mode 100644 index e5e797f..0000000 --- a/src/db/product.rs +++ /dev/null @@ -1,110 +0,0 @@ -use std::collections::HashMap; -use std::time::SystemTime; - -use sqlx::{QueryBuilder, Sqlite, SqlitePool, Transaction}; - -use crate::error::AppError; - -use super::tag::Tag; -use super::ProductDatabaseHandler; - -#[derive(Debug, serde::Serialize, sqlx::FromRow)] -pub struct Product { - pub id: i64, - pub name: String, - pub description: String, - pub uploaddate: i64, - pub updatedate: i64, -} - -impl Product { - pub async fn post(tx: &mut Transaction<'static, Sqlite>, name: &str, description: &str, tags: Vec<&str>) -> 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?; - - if !tags.is_empty() { - // insert tags that do not exist yet - let mut tags_query_builder: QueryBuilder<Sqlite> = QueryBuilder::new("INSERT INTO tag (name) "); - tags_query_builder.push_values(tags.iter(), | mut b, tag | { b.push_bind(tag); }); - tags_query_builder.push(" ON CONFLICT(name) DO NOTHING;"); - tags_query_builder.build().execute(&mut **tx).await?; - - // fetch all tags of story - let added_tags: Vec<Tag> = { - let mut tags_fetch_query_builder = QueryBuilder::new("WITH input(name) AS ("); - tags_fetch_query_builder.push_values(tags.iter(), |mut b, tag| { - b.push_bind(tag); - }); - tags_fetch_query_builder.push( - ") \ - SELECT t.id, t.name \ - FROM tag t \ - JOIN input i ON i.name = t.name;" - ); - tags_fetch_query_builder.build_query_as::<Tag>().fetch_all(&mut **tx).await? - }; - println!("{:?}", added_tags); - - // bind tags to story if they are not bound yet - let mut producttags_query_builder: QueryBuilder<Sqlite> = QueryBuilder::new("INSERT INTO producttag (pid, tid) "); - producttags_query_builder.push_values(added_tags.iter(), | mut b, added_tag | { - b.push_bind(product.id).push_bind(added_tag.id); - }); - producttags_query_builder.push(" ON CONFLICT(pid, tid) DO NOTHING;"); - producttags_query_builder - .build().execute(&mut **tx).await?; - - } - Ok(product) - } - - pub async fn delete(tx: &mut Transaction<'static, Sqlite>, pid: i64) -> Result<(), AppError> { - sqlx::query!("DELETE FROM producttag WHERE pid = $1", pid).execute(&mut **tx).await?; - sqlx::query!("DELETE FROM product WHERE id = $1", pid).execute(&mut **tx).await?; - Ok(()) - } -} - -pub trait ProductMarker { - fn product_id(&self) -> i64; -} - -pub struct SpecificProduct<T: ProductMarker> { - pub product: Product, - pub detail: T, -} - -pub async fn get_all_products(pool: &SqlitePool) -> Result<Vec<Product>, AppError> { - Ok(sqlx::query_as!(Product, "SELECT * FROM product") - .fetch_all(pool) - .await?) -} - -pub async fn get_all_specificproducts<T: ProductMarker + ProductDatabaseHandler>( - pool: &SqlitePool, -) -> Result<Vec<SpecificProduct<T>>, AppError> { - let product_details = T::get_all(pool).await?; - let products = get_all_products(pool).await?; - let mut details_map: HashMap<i64, T> = product_details - .into_iter() - .map(|detail| (detail.product_id(), detail)) - .collect(); - - let specific_products = products - .into_iter() - .filter_map(|product| { - details_map - .remove(&product.id) - .map(|detail| SpecificProduct { product, detail }) - }) - .collect(); - Ok(specific_products) -} |
