init
This commit is contained in:
+178
-12
@@ -1,22 +1,188 @@
|
||||
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::channel::dto::{ChannelResponse, CreateChannelRequest, UpdateChannelRequest};
|
||||
use crate::routes::channel::mapper;
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
Json,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_all() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Liste tous les channels
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/channels",
|
||||
responses(
|
||||
(status = 200, description = "Liste des channels récupérée avec succès", body = [ChannelResponse]),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channels"
|
||||
)]
|
||||
pub async fn get_all(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<ChannelResponse>>, HTTPError> {
|
||||
let channels = state.repositories.channel.get_all().await?;
|
||||
Ok(Json(
|
||||
channels
|
||||
.into_iter()
|
||||
.map(mapper::channel_model_to_channel_response)
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_by_id() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Récupère un channel par son ID
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/channels/{id}",
|
||||
responses(
|
||||
(status = 200, description = "Channel trouvé", body = ChannelResponse),
|
||||
(status = 404, description = "Channel non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du channel")
|
||||
),
|
||||
tag = "Channels"
|
||||
)]
|
||||
pub async fn get_by_id(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ChannelResponse>, HTTPError> {
|
||||
let channel = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
Ok(Json(mapper::channel_model_to_channel_response(channel)))
|
||||
}
|
||||
|
||||
pub async fn create() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Crée un nouveau channel
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/channels",
|
||||
request_body = CreateChannelRequest,
|
||||
responses(
|
||||
(status = 201, description = "Channel créé avec succès", body = ChannelResponse),
|
||||
(status = 400, description = "Données invalides (Serveur ou Catégorie non trouvée)"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
tag = "Channels"
|
||||
)]
|
||||
pub async fn create(
|
||||
_admin: Superuser,
|
||||
State(state): State<AppState>,
|
||||
Json(payload): Json<CreateChannelRequest>,
|
||||
) -> Result<(StatusCode, Json<ChannelResponse>), HTTPError> {
|
||||
// Vérifier que le serveur existe si fourni
|
||||
if let Some(server_id) = payload.server_id {
|
||||
state
|
||||
.repositories
|
||||
.server
|
||||
.get_by_id(server_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Server not found".to_string()))?;
|
||||
}
|
||||
|
||||
// Vérifier que la catégorie existe si fournie
|
||||
if let Some(category_id) = payload.category_id {
|
||||
state
|
||||
.repositories
|
||||
.category
|
||||
.get_by_id(category_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Category not found".to_string()))?;
|
||||
}
|
||||
|
||||
let active_model = mapper::create_request_to_am(payload);
|
||||
let channel = state.repositories.channel.create(active_model).await?;
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(mapper::channel_model_to_channel_response(channel)),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn update() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Met à jour un channel existant
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/channels/{id}",
|
||||
request_body = UpdateChannelRequest,
|
||||
responses(
|
||||
(status = 200, description = "Channel mis à jour avec succès", body = ChannelResponse),
|
||||
(status = 404, description = "Channel non trouvé"),
|
||||
(status = 400, description = "Données invalides (Serveur ou Catégorie non trouvée)"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du channel")
|
||||
),
|
||||
tag = "Channels"
|
||||
)]
|
||||
pub async fn update(
|
||||
_admin: Superuser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(payload): Json<UpdateChannelRequest>,
|
||||
) -> Result<Json<ChannelResponse>, HTTPError> {
|
||||
// Vérifier l'existence
|
||||
state
|
||||
.repositories
|
||||
.channel
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
// Vérifier que le serveur existe si fourni
|
||||
if let Some(server_id) = payload.server_id {
|
||||
state
|
||||
.repositories
|
||||
.server
|
||||
.get_by_id(server_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Server not found".to_string()))?;
|
||||
}
|
||||
|
||||
// Vérifier que la catégorie existe si fournie
|
||||
if let Some(category_id) = payload.category_id {
|
||||
state
|
||||
.repositories
|
||||
.category
|
||||
.get_by_id(category_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Category not found".to_string()))?;
|
||||
}
|
||||
|
||||
let active_model = mapper::update_request_to_am(id, payload);
|
||||
let channel = state.repositories.channel.update(active_model).await?;
|
||||
|
||||
Ok(Json(mapper::channel_model_to_channel_response(channel)))
|
||||
}
|
||||
|
||||
pub async fn delete() -> impl IntoResponse {
|
||||
StatusCode::NOT_IMPLEMENTED
|
||||
/// Supprime un channel
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/channels/{id}",
|
||||
responses(
|
||||
(status = 204, description = "Channel supprimé avec succès"),
|
||||
(status = 404, description = "Channel non trouvé"),
|
||||
(status = 500, description = "Erreur interne du serveur")
|
||||
),
|
||||
params(
|
||||
("id" = Uuid, Path, description = "ID du channel")
|
||||
),
|
||||
tag = "Channels"
|
||||
)]
|
||||
pub async fn delete(
|
||||
_admin: Superuser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
if state.repositories.channel.delete(id).await? {
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
Err(HTTPError::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user