From 6092e37e3a8e890cbe9d13305bcaf67daf410d93 Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Wed, 30 Oct 2024 18:16:07 +0100 Subject: Feat: Add product db handler method to post record + implement for story --- src/db/mod.rs | 11 ++++++++++- src/db/story.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/handlers/admin.rs | 30 +++++++----------------------- 3 files changed, 54 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/db/mod.rs b/src/db/mod.rs index 2f2aaeb..1d64ed8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -13,8 +13,17 @@ pub mod tool; pub mod tooltag; #[async_trait] -pub trait DatabaseHandler { +pub trait ProductDatabaseHandler { async fn get_all(pool: SqlitePool) -> Result, AppError> where Self: Sized; + + async fn post( + &self, + pool: &SqlitePool, + name: String, + description: String, + ) -> Result + where + Self: Sized; } diff --git a/src/db/story.rs b/src/db/story.rs index 7bc5577..5acc818 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -2,8 +2,10 @@ use axum::async_trait; use sqlx::SqlitePool; use crate::error::AppError; +use crate::db::product::Product; + +use super::ProductDatabaseHandler; -use super::DatabaseHandler; #[derive(Debug, serde::Serialize, sqlx::FromRow)] pub struct Story { @@ -15,7 +17,7 @@ pub struct Story { } #[async_trait] -impl DatabaseHandler for Story { +impl ProductDatabaseHandler for Story { async fn get_all(pool: SqlitePool) -> Result, AppError> where Self: Sized, @@ -24,4 +26,37 @@ impl DatabaseHandler for Story { .fetch_all(&pool) .await?) } + + async fn post(&self, pool: &SqlitePool, name: String, description: String) -> Result + where + Self: Sized, + { + let time = std::time::SystemTime::now(); + let uploaddate = time.duration_since(std::time::UNIX_EPOCH)?.as_millis() as i64; + + let mut tx = pool.begin().await?; + 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, + uploaddate, + uploaddate) + .fetch_one(&mut *tx) + .await?; + + let story = sqlx::query_as!( + Story, + "INSERT INTO story (language, pdf, epub, pid) VALUES ($1, $2, $3, $4) RETURNING id, language, pdf, epub, pid", + self.language, + self.pdf, + self.epub, + product.id) + .fetch_one(&mut *tx) + .await?; + + tx.commit().await?; + + Ok(story) + } } diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs index bdb2b40..ca18ebc 100644 --- a/src/handlers/admin.rs +++ b/src/handlers/admin.rs @@ -10,8 +10,8 @@ use axum::response::{IntoResponse, Response}; use axum::{Extension, Json}; use subtle::ConstantTimeEq; -use crate::db::product::Product; use crate::db::story::Story; +use crate::db::ProductDatabaseHandler; use crate::error::AppError; use super::ApiContext; @@ -90,30 +90,14 @@ pub async fn upload_story( return Ok(StatusCode::BAD_REQUEST.into_response()); }; - let time = std::time::SystemTime::now(); - let uploaddate = time.duration_since(std::time::UNIX_EPOCH)?.as_millis() as i64; - - let mut tx = ctx.pool.begin().await?; - 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, - uploaddate, - uploaddate) - .fetch_one(&mut *tx) - .await?; - - let story = sqlx::query_as!( - Story, - "INSERT INTO story (language, pdf, epub, pid) VALUES ($1, $2, $3, $4) RETURNING id, language, pdf, epub, pid", language, + let story_template = Story { + id: 0, + pid: 0, + language, pdf, epub, - product.id) - .fetch_one(&mut *tx) - .await?; - - tx.commit().await?; + }; + let story = story_template.post(&ctx.pool, name, description).await?; Ok((StatusCode::CREATED, Json(story)).into_response()) } -- cgit v1.2.3