Skip to content

Commit

Permalink
feat(api): Add DELETE /recipe/:id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-ride committed Dec 6, 2024
1 parent 6ab1860 commit 600723b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions api/src/recipe/delete.rs
Original file line number Diff line number Diff line change
@@ -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<uuid::Uuid>,
State(conn): State<Connection>,
) -> Result<impl IntoResponse, AppError> {
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"
))),
}
}
1 change: 1 addition & 0 deletions api/src/recipe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 600723b

Please sign in to comment.