This commit is contained in:
2026-07-25 01:17:19 +02:00
parent 62f7c6edba
commit b54b60c988
12 changed files with 224 additions and 98 deletions
+57 -59
View File
@@ -1,7 +1,5 @@
use super::types::{ServerExplorerItem, ServerTree};
use super::{AnyResult, RepositoryContext};
use crate::models::{
category, channel, role, server, server_role_permission, server_user, server_user_permission,
use crate::models::{role, server, server_role_permission, server_user, server_user_permission,
};
use sea_orm::prelude::*;
use sea_orm::{ActiveModelTrait, QuerySelect, Set};
@@ -204,59 +202,59 @@ impl ServerRepository {
}
// Helpers
impl ServerRepository {
pub async fn get_tree(&self, server_id: Uuid) -> AnyResult<ServerTree> {
// 1. Récupération des catégories avec leurs channels
let categories_with_channels = category::Entity::find()
.filter(category::Column::ServerId.eq(server_id))
.find_with_related(channel::Entity)
.all(&self.context.db)
.await?;
// 2. Récupération des channels orphelins (sans catégorie)
let orphan_channels = channel::Entity::find()
.filter(channel::Column::ServerId.eq(server_id))
.filter(channel::Column::CategoryId.is_null())
.all(&self.context.db)
.await?;
// 3. Transformation et tri des enfants
let mut items: Vec<ServerExplorerItem> = Vec::new();
for (cat, mut channels) in categories_with_channels {
// On trie les channels internes (obligatoire car SQL ne garantit aucun ordre ici)
channels.sort_by(|a, b| {
a.position
.cmp(&b.position)
.then(a.created_at.cmp(&b.created_at))
});
items.push(ServerExplorerItem::Category(cat, channels));
}
for chan in orphan_channels {
items.push(ServerExplorerItem::Channel(chan));
}
// 4. Tri final de la liste globale (Mélange catégories et orphelins)
items.sort_by(|a, b| {
let pos_cmp = a.position().cmp(&b.position());
if pos_cmp == std::cmp::Ordering::Equal {
// Départage par date si position identique
let date_a = match a {
ServerExplorerItem::Category(c, _) => c.created_at,
ServerExplorerItem::Channel(c) => c.created_at,
};
let date_b = match b {
ServerExplorerItem::Category(c, _) => c.created_at,
ServerExplorerItem::Channel(c) => c.created_at,
};
date_a.cmp(&date_b)
} else {
pos_cmp
}
});
Ok(ServerTree { items })
}
}
// impl ServerRepository {
// pub async fn get_tree(&self, server_id: Uuid) -> AnyResult<ServerTree> {
// // 1. Récupération des catégories avec leurs channels
// let categories_with_channels = category::Entity::find()
// .filter(category::Column::ServerId.eq(server_id))
// .find_with_related(channel::Entity)
// .all(&self.context.db)
// .await?;
//
// // 2. Récupération des channels orphelins (sans catégorie)
// let orphan_channels = channel::Entity::find()
// .filter(channel::Column::ServerId.eq(server_id))
// .filter(channel::Column::CategoryId.is_null())
// .all(&self.context.db)
// .await?;
//
// // 3. Transformation et tri des enfants
// let mut items: Vec<ServerExplorerItem> = Vec::new();
//
// for (cat, mut channels) in categories_with_channels {
// // On trie les channels internes (obligatoire car SQL ne garantit aucun ordre ici)
// channels.sort_by(|a, b| {
// a.position
// .cmp(&b.position)
// .then(a.created_at.cmp(&b.created_at))
// });
// items.push(ServerExplorerItem::Category(cat, channels));
// }
//
// for chan in orphan_channels {
// items.push(ServerExplorerItem::Channel(chan));
// }
//
// // 4. Tri final de la liste globale (Mélange catégories et orphelins)
// items.sort_by(|a, b| {
// let pos_cmp = a.position().cmp(&b.position());
//
// if pos_cmp == std::cmp::Ordering::Equal {
// // Départage par date si position identique
// let date_a = match a {
// ServerExplorerItem::Category(c, _) => c.created_at,
// ServerExplorerItem::Channel(c) => c.created_at,
// };
// let date_b = match b {
// ServerExplorerItem::Category(c, _) => c.created_at,
// ServerExplorerItem::Channel(c) => c.created_at,
// };
// date_a.cmp(&date_b)
// } else {
// pos_cmp
// }
// });
//
// Ok(ServerTree { items })
// }
// }