diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2024-11-07 18:35:27 +0100 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2024-11-07 18:35:27 +0100 |
| commit | 680867b4bb5e2708f02fc3f8c21c3563975c44c7 (patch) | |
| tree | 0e90e583f1696d3c7c66484a5f8dbd919c75245d /src | |
| parent | 99fc959383d5b7b26d8e80f6257adf8b98989f41 (diff) | |
Feat: Add admin API route to delete story by product id
Diffstat (limited to 'src')
| -rw-r--r-- | src/db/mod.rs | 4 | ||||
| -rw-r--r-- | src/db/product.rs | 7 | ||||
| -rw-r--r-- | src/db/story.rs | 13 | ||||
| -rw-r--r-- | src/handlers/admin.rs | 18 | ||||
| -rw-r--r-- | src/handlers/mod.rs | 4 |
5 files changed, 43 insertions, 3 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index e44c9ad..51665a3 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -25,4 +25,8 @@ pub trait ProductDatabaseHandler { ) -> Result<Self, AppError> where Self: Sized; + + async fn delete(pool: &SqlitePool, id: i64) -> Result<(), AppError> + where + Self: Sized; } diff --git a/src/db/product.rs b/src/db/product.rs index 06217c0..c21734e 100644 --- a/src/db/product.rs +++ b/src/db/product.rs @@ -5,7 +5,6 @@ use sqlx::{QueryBuilder, Sqlite, SqlitePool, Transaction}; use crate::error::AppError; -use super::producttag::ProductTag; use super::tag::Tag; use super::ProductDatabaseHandler; @@ -50,6 +49,12 @@ impl Product { } Ok(product) } + + pub async fn delete(tx: &mut Transaction<'static, Sqlite>, pid: i64) -> Result<(), AppError> { + sqlx::query!("DELETE FROM producttag WHERE pid = $1", pid).execute(&mut **tx).await?; + sqlx::query!("DELETE FROM product WHERE id = $1", pid).execute(&mut **tx).await?; + Ok(()) + } } pub trait ProductMarker { diff --git a/src/db/story.rs b/src/db/story.rs index 137c964..9e5fdb9 100644 --- a/src/db/story.rs +++ b/src/db/story.rs @@ -99,6 +99,19 @@ impl ProductDatabaseHandler for Story { Ok(story) } + + async fn delete(pool: &SqlitePool, pid: i64) -> Result<(), AppError> + where + Self: Sized, + { + let mut tx = pool.begin().await?; + sqlx::query!("DELETE FROM story WHERE pid = $1", pid) + .execute(&mut *tx) + .await?; + Product::delete(&mut tx, pid).await?; + tx.commit().await?; + Ok(()) + } } impl PageBuilder for Story { diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs index 7c016dd..3d16b32 100644 --- a/src/handlers/admin.rs +++ b/src/handlers/admin.rs @@ -132,3 +132,21 @@ pub async fn upload_story( .await?; Ok((StatusCode::CREATED, Json(story)).into_response()) } + +pub async fn delete_story( + ctx: Extension<ApiContext>, + mut multipart: Multipart, +) -> Result<impl IntoResponse, AppError> { + if let Some(field) = multipart.next_field().await? { + if let Some(label) = field.name() { + if label == "id" { + let pid = field.text().await?.parse::<i64>()?; + Story::delete(&ctx.pool, pid).await?; + return Ok((StatusCode::NO_CONTENT).into_response()); + } + return Ok(StatusCode::BAD_REQUEST.into_response()); + } + return Ok(StatusCode::BAD_REQUEST.into_response()); + } + Ok(StatusCode::BAD_REQUEST.into_response()) +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index e2063eb..14ac176 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -9,7 +9,7 @@ use sqlx::SqlitePool; use tower_http::add_extension::AddExtensionLayer; use tower_http::services::ServeDir; -use admin::{admin_healthcheck, admin_middleware, generate_token, upload_story}; +use admin::{admin_healthcheck, admin_middleware, delete_story, generate_token, upload_story}; use frontend_builder::home; use crate::db::story::Story; @@ -35,7 +35,7 @@ pub async fn router(pool: SqlitePool) -> Router { }; Router::new() - .route("/admin/story", post(upload_story)) + .route("/admin/story", post(upload_story).delete(delete_story)) .route("/admin", get(admin_healthcheck)) .layer(middleware::from_fn_with_state( state.clone(), |
