This commit is contained in:
2026-07-27 09:15:28 +02:00
parent d0e4bdd90e
commit 70c0b649e6
6 changed files with 62 additions and 7 deletions
+9 -1
View File
@@ -1,6 +1,6 @@
use crate::models::category; use crate::models::category;
use crate::repositories::{AnyResult, RepositoryContext}; use crate::repositories::{AnyResult, RepositoryContext};
use sea_orm::{ActiveModelTrait, EntityTrait}; use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter};
use std::sync::Arc; use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
@@ -20,6 +20,14 @@ impl CategoryRepository {
Ok(category::Entity::find().all(&self.context.db).await?) Ok(category::Entity::find().all(&self.context.db).await?)
} }
pub async fn filter(&self, server_id: Option<Uuid>) -> AnyResult<Vec<category::Model>> {
let mut query = category::Entity::find();
if let Some(s_id) = server_id {
query = query.filter(category::Column::ServerId.eq(s_id));
}
Ok(query.all(&self.context.db).await?)
}
pub async fn update(&self, active: category::ActiveModel) -> AnyResult<category::Model> { pub async fn update(&self, active: category::ActiveModel) -> AnyResult<category::Model> {
let category = active.update(&self.context.db).await?; let category = active.update(&self.context.db).await?;
self.context self.context
+11
View File
@@ -21,6 +21,17 @@ impl ChannelRepository {
Ok(channel::Entity::find().all(&self.context.db).await?) Ok(channel::Entity::find().all(&self.context.db).await?)
} }
pub async fn filter(&self, server_id: Option<Uuid>, category_id: Option<Uuid>) -> AnyResult<Vec<channel::Model>> {
let mut query = channel::Entity::find();
if let Some(s_id) = server_id {
query = query.filter(channel::Column::ServerId.eq(s_id));
}
if let Some(c_id) = category_id {
query = query.filter(channel::Column::CategoryId.eq(c_id));
}
Ok(query.all(&self.context.db).await?)
}
pub async fn update(&self, active: channel::ActiveModel) -> AnyResult<channel::Model> { pub async fn update(&self, active: channel::ActiveModel) -> AnyResult<channel::Model> {
let channel = active.update(&self.context.db).await?; let channel = active.update(&self.context.db).await?;
self.context.events.emit("channel_updated", channel.clone()); self.context.events.emit("channel_updated", channel.clone());
+13
View File
@@ -3,6 +3,19 @@ use serde::{Deserialize, Serialize};
use utoipa::ToSchema; use utoipa::ToSchema;
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, ToSchema, utoipa::IntoParams)]
pub struct CategoryQueryParams {
pub server_id: Option<Uuid>,
}
impl Default for CategoryQueryParams {
fn default() -> Self {
Self {
server_id: None,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)] #[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreateCategoryRequest { pub struct CreateCategoryRequest {
pub server_id: Uuid, pub server_id: Uuid,
+7 -3
View File
@@ -2,11 +2,11 @@ use crate::core::state::AppState;
use crate::http::context::Superuser; use crate::http::context::Superuser;
use crate::http::error::HTTPError; use crate::http::error::HTTPError;
use crate::routes::category::dto::{ use crate::routes::category::dto::{
CategoryResponse, CreateCategoryRequest, UpdateCategoryRequest, CategoryQueryParams, CategoryResponse, CreateCategoryRequest, UpdateCategoryRequest,
}; };
use crate::routes::category::mapper; use crate::routes::category::mapper;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, Query, State},
http::StatusCode, http::StatusCode,
Json, Json,
}; };
@@ -20,12 +20,16 @@ use uuid::Uuid;
(status = 200, description = "Liste des catégories récupérée avec succès", body = [CategoryResponse]), (status = 200, description = "Liste des catégories récupérée avec succès", body = [CategoryResponse]),
(status = 500, description = "Erreur interne du serveur") (status = 500, description = "Erreur interne du serveur")
), ),
params(
CategoryQueryParams
),
tag = "Categories" tag = "Categories"
)] )]
pub async fn get_all( pub async fn get_all(
State(state): State<AppState>, State(state): State<AppState>,
Query(filters): Query<CategoryQueryParams>,
) -> Result<Json<Vec<CategoryResponse>>, HTTPError> { ) -> Result<Json<Vec<CategoryResponse>>, HTTPError> {
let categories = state.repositories.category.get_all().await?; let categories = state.repositories.category.filter(filters.server_id).await?;
Ok(Json( Ok(Json(
categories categories
.into_iter() .into_iter()
+15
View File
@@ -4,6 +4,21 @@ use serde::{Deserialize, Serialize};
use utoipa::ToSchema; use utoipa::ToSchema;
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, ToSchema, utoipa::IntoParams)]
pub struct ChannelQueryParams {
pub server_id: Option<Uuid>,
pub category_id: Option<Uuid>,
}
impl Default for ChannelQueryParams {
fn default() -> Self {
Self {
server_id: None,
category_id: None,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)] #[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreateChannelRequest { pub struct CreateChannelRequest {
pub server_id: Option<Uuid>, pub server_id: Option<Uuid>,
+7 -3
View File
@@ -2,12 +2,12 @@ use crate::core::state::AppState;
use crate::http::context::Superuser; use crate::http::context::Superuser;
use crate::http::error::HTTPError; use crate::http::error::HTTPError;
use crate::routes::channel::dto::{ use crate::routes::channel::dto::{
ChannelResponse, ChannelRolePermissionResponse, ChannelUserPermissionResponse, ChannelQueryParams, ChannelResponse, ChannelRolePermissionResponse, ChannelUserPermissionResponse,
CreateChannelRequest, SetChannelPermissionRequest, UpdateChannelRequest, CreateChannelRequest, SetChannelPermissionRequest, UpdateChannelRequest,
}; };
use crate::routes::channel::mapper; use crate::routes::channel::mapper;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, Query, State},
http::StatusCode, http::StatusCode,
Json, Json,
}; };
@@ -21,12 +21,16 @@ use uuid::Uuid;
(status = 200, description = "Liste des channels récupérée avec succès", body = [ChannelResponse]), (status = 200, description = "Liste des channels récupérée avec succès", body = [ChannelResponse]),
(status = 500, description = "Erreur interne du serveur") (status = 500, description = "Erreur interne du serveur")
), ),
params(
ChannelQueryParams
),
tag = "Channels" tag = "Channels"
)] )]
pub async fn get_all( pub async fn get_all(
State(state): State<AppState>, State(state): State<AppState>,
Query(filters): Query<ChannelQueryParams>,
) -> Result<Json<Vec<ChannelResponse>>, HTTPError> { ) -> Result<Json<Vec<ChannelResponse>>, HTTPError> {
let channels = state.repositories.channel.get_all().await?; let channels = state.repositories.channel.filter(filters.server_id, filters.category_id).await?;
Ok(Json( Ok(Json(
channels channels
.into_iter() .into_iter()