Files
oxspeak_server/src/repositories/group.rs
T
2026-05-16 17:57:54 +02:00

48 lines
1.6 KiB
Rust

use crate::models::group;
use crate::repositories::{AnyResult, RepositoryContext};
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter};
use std::sync::Arc;
use uuid::Uuid;
#[derive(Clone, Debug)]
pub struct GroupRepository {
pub context: Arc<RepositoryContext>,
}
impl GroupRepository {
pub async fn get_all_by_server(&self, server_id: Uuid) -> AnyResult<Vec<group::Model>> {
Ok(group::Entity::find()
.filter(group::Column::ServerId.eq(server_id))
.all(&self.context.db)
.await?)
}
pub async fn get_all(&self) -> AnyResult<Vec<group::Model>> {
Ok(group::Entity::find().all(&self.context.db).await?)
}
pub async fn get_by_id(&self, id: Uuid) -> AnyResult<Option<group::Model>> {
Ok(group::Entity::find_by_id(id).one(&self.context.db).await?)
}
pub async fn create(&self, active: group::ActiveModel) -> AnyResult<group::Model> {
let group = active.insert(&self.context.db).await?;
self.context.events.emit("group_created", group.clone());
Ok(group)
}
pub async fn update(&self, active: group::ActiveModel) -> AnyResult<group::Model> {
let group = active.update(&self.context.db).await?;
self.context.events.emit("group_updated", group.clone());
Ok(group)
}
pub async fn delete(&self, id: Uuid) -> AnyResult<bool> {
let res = group::Entity::delete_by_id(id)
.exec(&self.context.db)
.await?;
self.context.events.emit("group_deleted", id);
Ok(res.rows_affected > 0)
}
}