From 600723b62ab5a54cddf8e100285d763ac855b2c0 Mon Sep 17 00:00:00 2001 From: flo-ride <43076999+flo-ride@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:15:22 +0100 Subject: [PATCH] feat(api): Add DELETE /recipe/:id endpoint --- api/src/recipe/delete.rs | 62 ++++++++++++++++++++++++++++++++++++++++ api/src/recipe/mod.rs | 1 + 2 files changed, 63 insertions(+) create mode 100644 api/src/recipe/delete.rs diff --git a/api/src/recipe/delete.rs b/api/src/recipe/delete.rs new file mode 100644 index 0000000..47621b4 --- /dev/null +++ b/api/src/recipe/delete.rs @@ -0,0 +1,62 @@ +//! This module defines the API endpoint to delete a recipe by its ID. +//! +//! Only an admin can delete a recipe. + +use crate::utils::openapi::RECIPE_TAG; +use axum::{ + extract::{Path, State}, + http::StatusCode, + response::IntoResponse, +}; +use entity::error::AppError; +use extractor::profile::admin::Admin; +use service::Connection; + +/// Deletes a recipe by its database ID. +/// +/// The recipe is not fully removed but marked as disabled in the database. +/// Only an admin can perform this action. +/// +/// - **Path Parameters:** +/// - `id`: The unique ID of the recipe in the database. +/// +/// - **Responses:** +/// - `500`: Internal error, likely related to the database. +/// - `400`: The request format is invalid. +/// - `200`: The recipe has been successfully disabled. +#[utoipa::path( + delete, + path = "/{id}", + tag = RECIPE_TAG, + params( + ("id" = uuid::Uuid, Path, description = "Recipe database id to delete recipe for"), + ), + responses( + (status = 500, description = "An internal error occured, probably databse related"), + (status = 400, description = "Your request is not correctly formatted"), + (status = 200, description = "The recipe is disabled") + ), + security( + ("axum-oidc" = []) + ) +)] +pub async fn delete_recipe( + admin: Admin, + Path(id): Path, + State(conn): State, +) -> Result { + let result = service::Query::find_recipe_by_id(&conn, id).await?; + + match result { + Some(recipe) => { + service::Mutation::delete_recipe(&conn, id).await?; + + log::info!("{admin} just deleted the recipe \"{}\" - {:?}", id, recipe); + + Ok((StatusCode::OK, "")) + } + None => Err(AppError::NotFound(format!( + "The recipe with id: {id} doesn't exist" + ))), + } +} diff --git a/api/src/recipe/mod.rs b/api/src/recipe/mod.rs index f91505e..24242f5 100644 --- a/api/src/recipe/mod.rs +++ b/api/src/recipe/mod.rs @@ -4,6 +4,7 @@ //! It includes operations for creating, retrieving, updating, and deleting recipes as well as //! interacting with external sources to sync or modify recipe data. +pub mod delete; pub mod edit; pub mod get; pub mod new;