use crate::core::state::AppState; use crate::http::context::Superuser; use crate::http::error::HTTPError; use crate::domain::dto::role::{CreateRoleRequest, RoleResponse, UpdateRoleRequest}; use crate::routes::role::mapper; use axum::{ Json, extract::{Path, State}, http::StatusCode, }; use uuid::Uuid; /// Liste tous les groupes #[utoipa::path( get, path = "/groups", responses( (status = 200, description = "Liste des groupes récupérée avec succès", body = [RoleResponse]), (status = 500, description = "Erreur interne du serveur") ), tag = "Roles" )] pub async fn get_all(State(state): State) -> Result>, HTTPError> { let groups = state.repositories.role.get_all().await?; Ok(Json( groups .into_iter() .map(mapper::group_model_to_group_response) .collect(), )) } /// Récupère un groupe par son ID #[utoipa::path( get, path = "/groups/{id}", responses( (status = 200, description = "Rolee trouvé", body = RoleResponse), (status = 404, description = "Rolee non trouvé"), (status = 500, description = "Erreur interne du serveur") ), params( ("id" = Uuid, Path, description = "ID du groupe") ), tag = "Roles" )] pub async fn get_by_id( State(state): State, Path(id): Path, ) -> Result, HTTPError> { let group = state .repositories .role .get_by_id(id) .await? .ok_or(HTTPError::NotFound)?; Ok(Json(mapper::group_model_to_group_response(group))) } /// Crée un nouveau groupe #[utoipa::path( post, path = "/groups", request_body = CreateRoleRequest, responses( (status = 201, description = "Role créé avec succès", body = RoleResponse), (status = 404, description = "Serveur non trouvé"), (status = 500, description = "Erreur interne du serveur") ), tag = "Roles", security( ("bearerAuth" = []) ) )] pub async fn create( _admin: Superuser, State(state): State, Json(payload): Json, ) -> Result<(StatusCode, Json), 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 group = state.repositories.role.create(active_model).await?; Ok(( StatusCode::CREATED, Json(mapper::group_model_to_group_response(group)), )) } /// Met à jour un groupe existant #[utoipa::path( put, path = "/groups/{id}", request_body = UpdateRoleRequest, responses( (status = 200, description = "Role mis à jour avec succès", body = RoleResponse), (status = 404, description = "Role non trouvé"), (status = 500, description = "Erreur interne du serveur") ), params( ("id" = Uuid, Path, description = "ID du groupe") ), tag = "Roles", security( ("bearerAuth" = []) ) )] pub async fn update( _admin: Superuser, State(state): State, Path(id): Path, Json(payload): Json, ) -> Result, HTTPError> { // Vérifier l'existence let group = state .repositories .role .get_by_id(id) .await? .ok_or(HTTPError::NotFound)?; let active_model = mapper::update_request_to_am(group.id, group.server_id, payload); let group = state.repositories.role.update(active_model).await?; Ok(Json(mapper::group_model_to_group_response(group))) } /// Supprime un groupe #[utoipa::path( delete, path = "/groups/{id}", responses( (status = 204, description = "Role supprimé avec succès"), (status = 404, description = "Role non trouvé"), (status = 500, description = "Erreur interne du serveur") ), params( ("id" = Uuid, Path, description = "ID du groupe") ), tag = "Roles", security( ("bearerAuth" = []) ) )] pub async fn delete( _admin: Superuser, State(state): State, Path(id): Path, ) -> Result { if state.repositories.role.delete(id).await? { Ok(StatusCode::NO_CONTENT) } else { Err(HTTPError::NotFound) } }