From 230340a2a01fcbdd8a7434723641c801a1ff4a7d Mon Sep 17 00:00:00 2001 From: flo-ride <43076999+flo-ride@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:03:30 +0100 Subject: [PATCH] feat(routes): Add DELETE /location/id route --- src/routes/location/delete.rs | 61 +++++++++++++++++++++++++++++++++++ src/routes/location/mod.rs | 2 ++ 2 files changed, 63 insertions(+) create mode 100644 src/routes/location/delete.rs diff --git a/src/routes/location/delete.rs b/src/routes/location/delete.rs new file mode 100644 index 0000000..e8e319f --- /dev/null +++ b/src/routes/location/delete.rs @@ -0,0 +1,61 @@ +//! This module defines the API endpoint to delete a location by its ID. +//! +//! Only an admin can delete a location. + +use crate::{error::AppError, models::profile::admin::Admin}; +use axum::{ + extract::{Path, State}, + http::StatusCode, + response::IntoResponse, +}; +use service::Connection; + +/// Deletes a location by its database ID. +/// +/// The location 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 location in the database. +/// +/// - **Responses:** +/// - `500`: Internal error, likely related to the database. +/// - `400`: The request format is invalid. +/// - `200`: The location has been successfully disabled. +#[utoipa::path(delete, path = "/location/{id}", + params( + ("id" = uuid::Uuid, Path, description = "Location database id to delete location 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 location is disabled") + ) + )] +pub async fn delete_location( + admin: Admin, + Path(id): Path, + State(conn): State, +) -> Result { + let result = service::Query::find_location_by_id(&conn, id).await?; + + match result { + Some(location) => { + service::Mutation::delete_location(&conn, id).await?; + + tracing::info!( + "Admin {} \"{}\" just deleted the location {} \"{}\" - {:?}", + admin.name, + admin.id, + location.name, + id, + location + ); + + Ok((StatusCode::OK, "")) + } + None => Err(AppError::NotFound(format!( + "The location with id: {id} doesn't exist" + ))), + } +} diff --git a/src/routes/location/mod.rs b/src/routes/location/mod.rs index 351facf..56c52ee 100644 --- a/src/routes/location/mod.rs +++ b/src/routes/location/mod.rs @@ -1,5 +1,6 @@ use axum::routing::{delete, get, post, put}; +pub mod delete; pub mod edit; pub mod get; pub mod new; @@ -10,4 +11,5 @@ pub fn router() -> axum::Router { .route("/", get(get::get_all_locations)) .route("/", post(new::post_new_location)) .route("/:id", put(edit::edit_location)) + .route("/:id", delete(delete::delete_location)) }