summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2024-10-30 17:04:26 +0100
committerWriterV2 <primary-v2@protonmail.com>2024-10-30 17:04:26 +0100
commit61f784a5fb437489d5dd3f629618d49279ff15a5 (patch)
treed92110ca3016c6c1417085d0a8fb9d2ec894a468
parent977c93ef0568aa488c226915d14e6f164519fd98 (diff)
Feat: Add db handler method to get all records from table + implement for story
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml1
-rw-r--r--src/db/mod.rs12
-rw-r--r--src/db/story.rs19
4 files changed, 35 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0a0b4fa..03d3ce9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -74,9 +74,9 @@ dependencies = [
[[package]]
name = "async-trait"
-version = "0.1.81"
+version = "0.1.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107"
+checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
dependencies = [
"proc-macro2",
"quote",
@@ -1217,6 +1217,7 @@ name = "portfolio-website"
version = "0.1.0"
dependencies = [
"anyhow",
+ "async-trait",
"axum",
"axum-extra",
"dotenvy",
diff --git a/Cargo.toml b/Cargo.toml
index 0445a55..ee1040d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,3 +16,4 @@ sqlx = { version = "0.8.2", features = ["runtime-tokio", "tls-native-tls", "sqli
dotenvy = "0.15.7"
subtle = "2.6.1"
axum-extra = { version = "0.9.4", features = ["query"] }
+async-trait = "0.1.83"
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?)
+ }
+}