diff --git a/frontend/src/components/server/server_detail.vue b/frontend/src/components/server/server_detail.vue index 6abd0b5..3c456d8 100644 --- a/frontend/src/components/server/server_detail.vue +++ b/frontend/src/components/server/server_detail.vue @@ -1,10 +1,10 @@ @@ -29,7 +29,7 @@ async function remove(){ if (response.ok) { emit('remove') } else { - console.error("Failed to fetch servers:", response.statusText) + console.error("Failed to delete server:", response.statusText) } } diff --git a/src/network/http/web/api/category.rs b/src/network/http/web/api/category.rs index 23e949c..68cc52f 100644 --- a/src/network/http/web/api/category.rs +++ b/src/network/http/web/api/category.rs @@ -1,8 +1,9 @@ use axum::{Extension, Json}; -use axum::extract::{Path, State}; +use axum::extract::{Path, State, Query}; use axum::http::{Extensions, StatusCode}; use axum::routing::{delete, get, post, put}; use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel}; +use serde::Deserialize; use uuid::Uuid; use crate::app::AppState; use crate::models::category; @@ -19,13 +20,26 @@ pub fn setup_route() -> AppRouter { .route("/categories/{id}/", delete(category_delete)) } +#[derive(Deserialize)] +pub struct CategoryQuery { + pub server_id: Option, +} + pub async fn category_list( State(app_state): State, - Extension(ctx): Extension + Extension(_ctx): Extension, + Query(query): Query ) -> Result>, HTTPError> { - let categories = category::Entity::find() - .all(app_state.db.get_connection()) - .await?; + let categories = if let Some(server_id) = query.server_id { + app_state.repositories.category + .get_by_server_id(server_id) + .await? + } else { + app_state.repositories.category + .get_all() + .await? + }; + Ok(Json(categories.into_iter().map(CategorySerializer::from).collect())) } diff --git a/src/repositories/category.rs b/src/repositories/category.rs index 281759d..1c2137b 100644 --- a/src/repositories/category.rs +++ b/src/repositories/category.rs @@ -1,5 +1,7 @@ use std::sync::Arc; -use sea_orm::{DbErr, EntityTrait, ActiveModelTrait}; +use sea_orm::{ + ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter, +}; use crate::models::category; use crate::repositories::RepositoryContext; @@ -13,6 +15,17 @@ impl CategoryRepository { category::Entity::find_by_id(id).one(&self.context.db).await } + pub async fn get_all(&self) -> Result, DbErr> { + category::Entity::find().all(&self.context.db).await + } + + pub async fn get_by_server_id(&self, server_id: uuid::Uuid) -> Result, DbErr> { + category::Entity::find() + .filter(category::Column::ServerId.eq(server_id)) + .all(&self.context.db) + .await + } + pub async fn update(&self, active: category::ActiveModel) -> Result { let category = active.update(&self.context.db).await?; self.context.events.emit("Category_updated", category.clone()); diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs index 63e257a..8dea626 100644 --- a/src/repositories/mod.rs +++ b/src/repositories/mod.rs @@ -1,7 +1,11 @@ use std::sync::Arc; use sea_orm::DatabaseConnection; use crate::event_bus::EventBus; +use crate::repositories::category::CategoryRepository; +use crate::repositories::channel::ChannelRepository; +use crate::repositories::message::MessageRepository; use crate::repositories::server::ServerRepository; +use crate::repositories::user::UserRepository; mod server; mod category; @@ -18,6 +22,10 @@ pub struct RepositoryContext { #[derive(Clone)] pub struct Repositories { pub server: ServerRepository, + pub category: CategoryRepository, + pub channel: ChannelRepository, + pub message: MessageRepository, + pub user: UserRepository, } impl Repositories { @@ -26,6 +34,10 @@ impl Repositories { Self { server: ServerRepository {context: context.clone()}, + category: CategoryRepository {context: context.clone()}, + channel: ChannelRepository {context: context.clone()}, + message: MessageRepository {context: context.clone()}, + user: UserRepository {context: context.clone()}, } } } \ No newline at end of file