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, } impl GroupRepository { pub async fn get_all_by_server(&self, server_id: Uuid) -> AnyResult> { Ok(group::Entity::find() .filter(group::Column::ServerId.eq(server_id)) .all(&self.context.db) .await?) } pub async fn get_all(&self) -> AnyResult> { Ok(group::Entity::find().all(&self.context.db).await?) } pub async fn get_by_id(&self, id: Uuid) -> AnyResult> { Ok(group::Entity::find_by_id(id).one(&self.context.db).await?) } pub async fn create(&self, active: group::ActiveModel) -> AnyResult { 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 { 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 { 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) } }