This commit is contained in:
2026-05-16 17:57:54 +02:00
parent 1a2ec26f27
commit b2cefb7d66
55 changed files with 1654 additions and 334 deletions
+2 -1
View File
@@ -1 +1,2 @@
pub struct Channel {}
// Ce fichier est conservé pour structure mais n'est plus utilisé.
// Les modèles de domaine sont directement gérés par SeaORM dans src/models.
+36 -6
View File
@@ -1,10 +1,40 @@
use crate::models::channel::ChannelType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateChannelRequest {}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreateChannelRequest {
pub server_id: Option<Uuid>,
pub category_id: Option<Uuid>,
#[serde(default)]
pub position: i32,
pub channel_type: ChannelType,
#[schema(example = "général")]
pub name: Option<String>,
pub default_permissions: Option<u64>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateChannelRequest {}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdateChannelRequest {
pub server_id: Option<Uuid>,
pub category_id: Option<Uuid>,
pub position: i32,
pub channel_type: ChannelType,
pub name: Option<String>,
pub default_permissions: Option<u64>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ChannelResponse {}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ChannelResponse {
pub id: Uuid,
pub server_id: Option<Uuid>,
pub category_id: Option<Uuid>,
pub position: i32,
pub channel_type: ChannelType,
pub name: Option<String>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub default_permissions: Option<u64>,
}
+178 -12
View File
@@ -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)
}
}
+42 -3
View File
@@ -1,5 +1,44 @@
use super::{domain::Channel, dto::ChannelResponse};
use crate::models::channel;
use crate::routes::channel::dto::{ChannelResponse, CreateChannelRequest, UpdateChannelRequest};
use sea_orm::Set;
use uuid::Uuid;
pub fn to_response(_item: Channel) -> ChannelResponse {
todo!()
pub fn channel_model_to_channel_response(model: channel::Model) -> ChannelResponse {
ChannelResponse {
id: model.id,
server_id: model.server_id,
category_id: model.category_id,
position: model.position,
channel_type: model.channel_type,
name: model.name,
created_at: model.created_at,
updated_at: model.updated_at,
default_permissions: model.default_permissions,
}
}
pub fn create_request_to_am(req: CreateChannelRequest) -> channel::ActiveModel {
channel::ActiveModel {
id: Set(Uuid::new_v4()),
server_id: Set(req.server_id),
category_id: Set(req.category_id),
position: Set(req.position),
channel_type: Set(req.channel_type),
name: Set(req.name),
default_permissions: Set(req.default_permissions),
..Default::default()
}
}
pub fn update_request_to_am(id: Uuid, req: UpdateChannelRequest) -> channel::ActiveModel {
channel::ActiveModel {
id: Set(id),
server_id: Set(req.server_id),
category_id: Set(req.category_id),
position: Set(req.position),
channel_type: Set(req.channel_type),
name: Set(req.name),
default_permissions: Set(req.default_permissions),
..Default::default()
}
}
+3 -3
View File
@@ -1,8 +1,8 @@
use axum::{Router, routing::get};
use super::handlers;
use crate::core::state::AppState;
use axum::{routing::get, Router};
pub fn router() -> Router {
pub fn router() -> Router<AppState> {
Router::new()
.route("/channels", get(handlers::get_all).post(handlers::create))
.route(
+2 -21
View File
@@ -1,21 +1,2 @@
use super::domain::Channel;
pub async fn find_all() -> Vec<Channel> {
todo!()
}
pub async fn find_by_id(_id: u64) -> Option<Channel> {
todo!()
}
pub async fn create(_item: Channel) -> Channel {
todo!()
}
pub async fn update(_id: u64, _item: Channel) -> Option<Channel> {
todo!()
}
pub async fn delete(_id: u64) -> bool {
todo!()
}
// Ce fichier est conservé pour structure mais n'est plus utilisé.
// La logique a été déplacée directement dans les handlers.