summaryrefslogtreecommitdiff
path: root/src/db/product.rs
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2024-11-03 18:50:29 +0100
committerWriterV2 <primary-v2@protonmail.com>2024-11-03 18:50:29 +0100
commitfa7f650562337c7f3a4fb7446e56575e26380494 (patch)
tree082643ee68f3773e6b9bfd2ed42e75d1bee79d44 /src/db/product.rs
parenteb326116f4abc6b12ec81d06586d7cf08755538c (diff)
Feat: Finalize ability to get all products with details
- Remove dummy product - Add ProductMarker method to get product id + implement - Move method to get all products out of PageBuilder into its own function in Product module - Finalize getting all products with details - Add empty implementation for Story for testing purposes (TODO: add card details)
Diffstat (limited to 'src/db/product.rs')
-rw-r--r--src/db/product.rs44
1 files changed, 43 insertions, 1 deletions
diff --git a/src/db/product.rs b/src/db/product.rs
index 09b0dff..0fc8f05 100644
--- a/src/db/product.rs
+++ b/src/db/product.rs
@@ -1,3 +1,11 @@
+use std::collections::HashMap;
+
+use sqlx::SqlitePool;
+
+use crate::error::AppError;
+
+use super::ProductDatabaseHandler;
+
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct Product {
pub id: i64,
@@ -7,4 +15,38 @@ pub struct Product {
pub updatedate: i64,
}
-pub trait ProductMarker {}
+pub trait ProductMarker {
+ fn product_id(&self) -> i64;
+}
+
+pub struct SpecificProduct<T: ProductMarker> {
+ pub product: Product,
+ pub detail: T,
+}
+
+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)
+}