init
This commit is contained in:
+147
-12
@@ -1,22 +1,157 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use crate::core::state::AppState;
|
||||
use crate::http::context::Superuser;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::routes::category::dto::{
|
||||
CategoryResponse, CreateCategoryRequest, UpdateCategoryRequest,
|
||||
};
|
||||
use crate::routes::category::mapper;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_all() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// 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")
|
||||
),
|
||||
tag = "Categories"
|
||||
)]
|
||||
pub async fn get_all(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<CategoryResponse>>, HTTPError> {
|
||||
let categories = state.repositories.category.get_all().await?;
|
||||
Ok(Json(
|
||||
categories
|
||||
.into_iter()
|
||||
.map(mapper::category_model_to_category_response)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_by_id() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// 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)))
|
||||
}
|
||||
|
||||
pub async fn create() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// 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"
|
||||
)]
|
||||
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 active_model = mapper::create_request_to_am(payload);
|
||||
let category = state.repositories.category.create(active_model).await?;
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(mapper::category_model_to_category_response(category)),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn update() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// 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"
|
||||
)]
|
||||
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 active_model = mapper::update_request_to_am(category.id, category.server_id, payload);
|
||||
let category = state.repositories.category.update(active_model).await?;
|
||||
|
||||
Ok(Json(mapper::category_model_to_category_response(category)))
|
||||
}
|
||||
|
||||
pub async fn delete() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// 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"
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
if state.repositories.category.delete(id).await? {
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
Err(HTTPError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user