diff options
Diffstat (limited to 'src/db')
| -rw-r--r-- | src/db/game.rs | 6 | ||||
| -rw-r--r-- | src/db/mod.rs | 2 | ||||
| -rw-r--r-- | src/db/product.rs | 44 | ||||
| -rw-r--r-- | src/db/story.rs | 26 | ||||
| -rw-r--r-- | src/db/tool.rs | 6 |
5 files changed, 77 insertions, 7 deletions
diff --git a/src/db/game.rs b/src/db/game.rs index 0620314..e10006c 100644 --- a/src/db/game.rs +++ b/src/db/game.rs @@ -7,4 +7,8 @@ pub struct Game { pub pid: i64, } -impl ProductMarker for Game {} +impl ProductMarker for Game { + fn product_id(&self) -> i64 { + self.pid + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index 1d64ed8..0b310c6 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -14,7 +14,7 @@ pub mod tooltag; #[async_trait] pub trait ProductDatabaseHandler { - async fn get_all(pool: SqlitePool) -> Result<Vec<Self>, AppError> + async fn get_all(pool: &SqlitePool) -> Result<Vec<Self>, AppError> where Self: Sized; 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) +} diff --git a/src/db/story.rs b/src/db/story.rs index ee79e4f..686c33f 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -1,8 +1,10 @@ use axum::async_trait; +use maud::html; use sqlx::SqlitePool; use crate::error::AppError; use crate::db::product::Product; +use crate::handlers::frontend_builder::PageBuilder; use super::product::ProductMarker; use super::ProductDatabaseHandler; @@ -17,16 +19,20 @@ pub struct Story { pub pid: i64, } -impl ProductMarker for Story {} +impl ProductMarker for Story { + fn product_id(&self) -> i64 { + self.pid + } +} #[async_trait] impl ProductDatabaseHandler for Story { - async fn get_all(pool: SqlitePool) -> Result<Vec<Self>, AppError> + async fn get_all(pool: &SqlitePool) -> Result<Vec<Self>, AppError> where Self: Sized, { Ok(sqlx::query_as!(Story, "SELECT * FROM story") - .fetch_all(&pool) + .fetch_all(pool) .await?) } @@ -63,3 +69,17 @@ impl ProductDatabaseHandler for Story { Ok(story) } } + +impl PageBuilder for Story { + fn page_title() -> String { + "Stories".to_string() + } + + // TODO: Add pdf and epub download link + language info + fn product_specific_card_content(&self) -> maud::Markup { + html!( + span { + } + ) + } +} diff --git a/src/db/tool.rs b/src/db/tool.rs index 5f23f54..c53dc1e 100644 --- a/src/db/tool.rs +++ b/src/db/tool.rs @@ -7,4 +7,8 @@ pub struct Tool { pub pid: i64, } -impl ProductMarker for Tool {} +impl ProductMarker for Tool { + fn product_id(&self) -> i64 { + self.id + } +} |
