169 lines
4.7 KiB
Rust
169 lines
4.7 KiB
Rust
use crate::core::state::AppState;
|
|
use crate::http::context::Superuser;
|
|
use crate::http::error::HTTPError;
|
|
use crate::domain::dto::category::{
|
|
CategoryQueryParams, CategoryResponse, CreateCategoryRequest, UpdateCategoryRequest,
|
|
};
|
|
use crate::routes::category::mapper;
|
|
use axum::{
|
|
extract::{Path, Query, State},
|
|
http::StatusCode,
|
|
Json,
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
/// Liste toutes les catégories
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/categories",
|
|
responses(
|
|
(status = 200, description = "Liste des catégories récupérée avec succès", body = [CategoryResponse]),
|
|
(status = 500, description = "Erreur interne du serveur")
|
|
),
|
|
params(
|
|
CategoryQueryParams
|
|
),
|
|
tag = "Categories"
|
|
)]
|
|
pub async fn get_all(
|
|
State(state): State<AppState>,
|
|
Query(filters): Query<CategoryQueryParams>,
|
|
) -> Result<Json<Vec<CategoryResponse>>, HTTPError> {
|
|
let categories = state.repositories.category.filter(filters.server_id).await?;
|
|
Ok(Json(
|
|
categories
|
|
.into_iter()
|
|
.map(mapper::category_model_to_category_response)
|
|
.collect(),
|
|
))
|
|
}
|
|
|
|
/// Récupère une catégorie par son ID
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/categories/{id}",
|
|
responses(
|
|
(status = 200, description = "Catégorie trouvée", body = CategoryResponse),
|
|
(status = 404, description = "Catégorie non trouvée"),
|
|
(status = 500, description = "Erreur interne du serveur")
|
|
),
|
|
params(
|
|
("id" = Uuid, Path, description = "ID de la catégorie")
|
|
),
|
|
tag = "Categories"
|
|
)]
|
|
pub async fn get_by_id(
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<CategoryResponse>, HTTPError> {
|
|
let category = state
|
|
.repositories
|
|
.category
|
|
.get_by_id(id)
|
|
.await?
|
|
.ok_or(HTTPError::NotFound)?;
|
|
|
|
Ok(Json(mapper::category_model_to_category_response(category)))
|
|
}
|
|
|
|
/// Crée une nouvelle catégorie
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/categories",
|
|
request_body = CreateCategoryRequest,
|
|
responses(
|
|
(status = 201, description = "Catégorie créée avec succès", body = CategoryResponse),
|
|
(status = 404, description = "Serveur non trouvé"),
|
|
(status = 500, description = "Erreur interne du serveur")
|
|
),
|
|
tag = "Categories",
|
|
security(
|
|
("bearerAuth" = [])
|
|
)
|
|
)]
|
|
pub async fn create(
|
|
_admin: Superuser,
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<CreateCategoryRequest>,
|
|
) -> Result<(StatusCode, Json<CategoryResponse>), HTTPError> {
|
|
// Vérifier que le serveur existe
|
|
state
|
|
.repositories
|
|
.server
|
|
.get_by_id(payload.server_id)
|
|
.await?
|
|
.ok_or(HTTPError::BadRequest("Server not found".to_string()))?;
|
|
|
|
let category = state.services.category.create_category(payload.server_id, payload.name).await?;
|
|
Ok((
|
|
StatusCode::CREATED,
|
|
Json(mapper::category_model_to_category_response(category)),
|
|
))
|
|
}
|
|
|
|
/// Met à jour une catégorie existante
|
|
#[utoipa::path(
|
|
put,
|
|
path = "/categories/{id}",
|
|
request_body = UpdateCategoryRequest,
|
|
responses(
|
|
(status = 200, description = "Catégorie mise à jour avec succès", body = CategoryResponse),
|
|
(status = 404, description = "Catégorie non trouvée"),
|
|
(status = 500, description = "Erreur interne du serveur")
|
|
),
|
|
params(
|
|
("id" = Uuid, Path, description = "ID de la catégorie")
|
|
),
|
|
tag = "Categories",
|
|
security(
|
|
("bearerAuth" = [])
|
|
)
|
|
)]
|
|
pub async fn update(
|
|
_admin: Superuser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
Json(payload): Json<UpdateCategoryRequest>,
|
|
) -> Result<Json<CategoryResponse>, HTTPError> {
|
|
// Vérifier l'existence
|
|
let _category = state
|
|
.repositories
|
|
.category
|
|
.get_by_id(id)
|
|
.await?
|
|
.ok_or(HTTPError::NotFound)?;
|
|
|
|
let category = state.services.category.update_category(id, payload.name).await?;
|
|
|
|
Ok(Json(mapper::category_model_to_category_response(category)))
|
|
}
|
|
|
|
/// Supprime une catégorie
|
|
#[utoipa::path(
|
|
delete,
|
|
path = "/categories/{id}",
|
|
responses(
|
|
(status = 204, description = "Catégorie supprimée avec succès"),
|
|
(status = 404, description = "Catégorie non trouvée"),
|
|
(status = 500, description = "Erreur interne du serveur")
|
|
),
|
|
params(
|
|
("id" = Uuid, Path, description = "ID de la catégorie")
|
|
),
|
|
tag = "Categories",
|
|
security(
|
|
("bearerAuth" = [])
|
|
)
|
|
)]
|
|
pub async fn delete(
|
|
_admin: Superuser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<StatusCode, HTTPError> {
|
|
if state.services.category.delete_category(id).await? {
|
|
Ok(StatusCode::NO_CONTENT)
|
|
} else {
|
|
Err(HTTPError::NotFound)
|
|
}
|
|
}
|