Skip to content

Commit

Permalink
feat(routes): Add DELETE /location/id route
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-ride committed Oct 30, 2024
1 parent cd51511 commit 230340a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/routes/location/delete.rs
Original file line number Diff line number Diff line change
@@ -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<uuid::Uuid>,
State(conn): State<Connection>,
) -> Result<impl IntoResponse, AppError> {
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"
))),
}
}
2 changes: 2 additions & 0 deletions src/routes/location/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::routing::{delete, get, post, put};

pub mod delete;
pub mod edit;
pub mod get;
pub mod new;
Expand All @@ -10,4 +11,5 @@ pub fn router() -> axum::Router<crate::state::AppState> {
.route("/", get(get::get_all_locations))
.route("/", post(new::post_new_location))
.route("/:id", put(edit::edit_location))
.route("/:id", delete(delete::delete_location))
}

0 comments on commit 230340a

Please sign in to comment.