From 68f16055a871cbaa6e405db58bcc4d9081a28692 Mon Sep 17 00:00:00 2001 From: Nell Date: Sat, 21 Feb 2026 19:14:04 +0100 Subject: [PATCH] Init --- src/interfaces/http/dto/category.rs | 12 +++++++++ src/interfaces/http/dto/server.rs | 39 ++++++++++++++++++++++++++-- src/network/http/web/api/category.rs | 2 +- src/network/http/web/api/server.rs | 18 +++++++------ 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/interfaces/http/dto/category.rs b/src/interfaces/http/dto/category.rs index 670156c..02fdf61 100644 --- a/src/interfaces/http/dto/category.rs +++ b/src/interfaces/http/dto/category.rs @@ -33,3 +33,15 @@ impl From for category::ActiveModel { } } } + +impl CreateCategoryRequest { + pub fn apply_to(self, mut am: category::ActiveModel) -> category::ActiveModel { + am.name = Set(self.name); + am + } +} + +#[derive(Debug, Deserialize)] +pub struct ListCategoryQuery { + pub server_id: Uuid, +} diff --git a/src/interfaces/http/dto/server.rs b/src/interfaces/http/dto/server.rs index 0ff1e0f..684de0f 100644 --- a/src/interfaces/http/dto/server.rs +++ b/src/interfaces/http/dto/server.rs @@ -1,5 +1,7 @@ -// On importe le modèle pour pouvoir mapper +use crate::interfaces::http::dto::category::CategoryResponse; +use crate::interfaces::http::dto::channel::ChannelResponse; use crate::models::server; +use crate::repositories::types::{ServerExplorerItem, ServerTree}; use sea_orm::Set; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -39,10 +41,23 @@ impl From for server::ActiveModel { } } +impl CreateServerRequest { + pub fn apply_to(self, mut am: server::ActiveModel) -> server::ActiveModel { + am.name = Set(self.name); + am.password = Set(self.password); + am + } +} + #[derive(Serialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum TreeItemType { - // todo : faire le CategoryResponse et ChannelResponse + Category { + #[serde(flatten)] + category: CategoryResponse, + channels: Vec, + }, + Channel(ChannelResponse), } #[derive(Serialize)] @@ -50,3 +65,23 @@ pub enum TreeItemType { pub struct ServerTreeResponse { items: Vec, } + +impl From for ServerTreeResponse { + fn from(layout: ServerTree) -> Self { + Self { + items: layout + .items + .into_iter() + .map(|item| match item { + ServerExplorerItem::Category(cat, chans) => TreeItemType::Category { + category: CategoryResponse::from(cat), + channels: chans.into_iter().map(ChannelResponse::from).collect(), + }, + ServerExplorerItem::Channel(chan) => { + TreeItemType::Channel(ChannelResponse::from(chan)) + } + }) + .collect(), + } + } +} diff --git a/src/network/http/web/api/category.rs b/src/network/http/web/api/category.rs index c480e24..0d43d37 100644 --- a/src/network/http/web/api/category.rs +++ b/src/network/http/web/api/category.rs @@ -107,7 +107,7 @@ pub async fn category_update( // todo : voir pour virer le into_active_model pour utiliser le dto let category: category::Model = serializer - .apply_to_active_model(active) + .apply_to(active) .update(app_state.db.get_connection()) .await?; diff --git a/src/network/http/web/api/server.rs b/src/network/http/web/api/server.rs index 4f53243..38edf91 100644 --- a/src/network/http/web/api/server.rs +++ b/src/network/http/web/api/server.rs @@ -1,7 +1,9 @@ use crate::app::AppState; -use crate::interfaces::http::dto::server::ServerResponse; +use crate::interfaces::http::dto::server::{ + CreateServerRequest, ServerResponse, ServerTreeResponse, +}; +use crate::models::server; use crate::network::http::{AppRouter, HTTPError}; -use crate::serializers::{ServerSerializer, ServerTreeSerializer}; use axum::extract::{Path, State}; use axum::http::StatusCode; use axum::routing::get; @@ -46,9 +48,9 @@ pub async fn server_detail( pub async fn server_create( State(state): State, - Json(serializer): Json, + Json(serializer): Json, ) -> Result, HTTPError> { - let active = serializer.into_active_model(); + let active: server::ActiveModel = serializer.into(); let server = state.repositories.server.create(active).await?; Ok(Json(ServerResponse::from(server))) @@ -57,7 +59,7 @@ pub async fn server_create( pub async fn server_update( State(state): State, Path(id): Path, - Json(serializer): Json, + Json(serializer): Json, ) -> Result, HTTPError> { let am_server = state .repositories @@ -70,7 +72,7 @@ pub async fn server_update( let server = state .repositories .server - .update(serializer.apply_to_active_model(am_server)) + .update(serializer.apply_to(am_server)) .await?; Ok(Json(ServerResponse::from(server))) @@ -104,8 +106,8 @@ pub async fn server_password( pub async fn tree( State(state): State, Path(id): Path, -) -> Result, HTTPError> { +) -> Result, HTTPError> { let layout = state.repositories.server.get_tree(id).await?; - Ok(Json(ServerTreeSerializer::from(layout))) + Ok(Json(ServerTreeResponse::from(layout))) }