This commit is contained in:
2025-12-15 00:17:41 +01:00
parent 060fb38bb0
commit 18f88303f3
4 changed files with 49 additions and 10 deletions

View File

@@ -1,10 +1,10 @@
<template>
<div>
<div>
{{server}}
<button @click="remove">Remove</button>
</div>
<h3>server : {{server.name}} <button @click="remove">Remove</button></h3>
</div>
<hr>
</div>
</template>
@@ -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)
}
}

View File

@@ -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<Uuid>,
}
pub async fn category_list(
State(app_state): State<AppState>,
Extension(ctx): Extension<RequestContext>
Extension(_ctx): Extension<RequestContext>,
Query(query): Query<CategoryQuery>
) -> Result<Json<Vec<CategorySerializer>>, 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()))
}

View File

@@ -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<Vec<category::Model>, DbErr> {
category::Entity::find().all(&self.context.db).await
}
pub async fn get_by_server_id(&self, server_id: uuid::Uuid) -> Result<Vec<category::Model>, 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<category::Model, DbErr> {
let category = active.update(&self.context.db).await?;
self.context.events.emit("Category_updated", category.clone());

View File

@@ -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()},
}
}
}