diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/db/mod.rs | 12 | ||||
| -rw-r--r-- | src/db/story.rs | 19 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index 01111d8..2f2aaeb 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,3 +1,8 @@ +use sqlx::SqlitePool; + +use crate::error::AppError; +use async_trait::async_trait; + pub mod game; pub mod gametag; pub mod product; @@ -6,3 +11,10 @@ pub mod storytag; pub mod tag; pub mod tool; pub mod tooltag; + +#[async_trait] +pub trait DatabaseHandler { + async fn get_all(pool: SqlitePool) -> Result<Vec<Self>, AppError> + where + Self: Sized; +} diff --git a/src/db/story.rs b/src/db/story.rs index 10fc49b..7bc5577 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -1,3 +1,10 @@ +use axum::async_trait; +use sqlx::SqlitePool; + +use crate::error::AppError; + +use super::DatabaseHandler; + #[derive(Debug, serde::Serialize, sqlx::FromRow)] pub struct Story { pub id: i64, @@ -6,3 +13,15 @@ pub struct Story { pub epub: Vec<u8>, pub pid: i64, } + +#[async_trait] +impl DatabaseHandler for Story { + async fn get_all(pool: SqlitePool) -> Result<Vec<Self>, AppError> + where + Self: Sized, + { + Ok(sqlx::query_as!(Story, "SELECT * FROM story") + .fetch_all(&pool) + .await?) + } +} |
