init
This commit is contained in:
@@ -1 +1,2 @@
|
||||
pub struct Category {}
|
||||
// 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.
|
||||
|
||||
@@ -1,10 +1,30 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateCategoryRequest {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct CreateCategoryRequest {
|
||||
pub server_id: Uuid,
|
||||
#[schema(example = "Discussion")]
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub position: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UpdateCategoryRequest {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct UpdateCategoryRequest {
|
||||
#[schema(example = "Discussion (Maj)")]
|
||||
pub name: String,
|
||||
pub position: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CategoryResponse {}
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct CategoryResponse {
|
||||
pub id: Uuid,
|
||||
pub server_id: Uuid,
|
||||
pub name: String,
|
||||
pub position: i32,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
+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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,41 @@
|
||||
use super::{domain::Category, dto::CategoryResponse};
|
||||
use crate::models::category;
|
||||
use crate::routes::category::dto::{
|
||||
CategoryResponse, CreateCategoryRequest, UpdateCategoryRequest,
|
||||
};
|
||||
use sea_orm::Set;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn to_response(_item: Category) -> CategoryResponse {
|
||||
todo!()
|
||||
pub fn category_model_to_category_response(model: category::Model) -> CategoryResponse {
|
||||
CategoryResponse {
|
||||
id: model.id,
|
||||
server_id: model.server_id,
|
||||
name: model.name,
|
||||
position: model.position,
|
||||
created_at: model.created_at,
|
||||
updated_at: model.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_request_to_am(req: CreateCategoryRequest) -> category::ActiveModel {
|
||||
category::ActiveModel {
|
||||
id: Set(Uuid::new_v4()),
|
||||
server_id: Set(req.server_id),
|
||||
name: Set(req.name),
|
||||
position: Set(req.position),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_request_to_am(
|
||||
id: Uuid,
|
||||
server_id: Uuid,
|
||||
req: UpdateCategoryRequest,
|
||||
) -> category::ActiveModel {
|
||||
category::ActiveModel {
|
||||
id: Set(id),
|
||||
server_id: Set(server_id),
|
||||
name: Set(req.name),
|
||||
position: Set(req.position),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use axum::{Router, routing::get};
|
||||
use crate::core::state::AppState;
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
use super::handlers;
|
||||
|
||||
pub fn router() -> Router {
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/categorys", get(handlers::get_all).post(handlers::create))
|
||||
.route("/categories", get(handlers::get_all).post(handlers::create))
|
||||
.route(
|
||||
"/categorys/:id",
|
||||
"/categories/:id",
|
||||
get(handlers::get_by_id)
|
||||
.put(handlers::update)
|
||||
.delete(handlers::delete),
|
||||
|
||||
@@ -1,21 +1,2 @@
|
||||
use super::domain::Category;
|
||||
|
||||
pub async fn find_all() -> Vec<Category> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn find_by_id(_id: u64) -> Option<Category> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn create(_item: Category) -> Category {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn update(_id: u64, _item: Category) -> Option<Category> {
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user