use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::error::AppError; use crate::models::channels::ChannelMemberRole; use crate::service::ImService; use super::session::ImSession; #[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)] pub struct CreateChannelRoleParams { pub name: String, pub description: Option, pub permissions: Vec, pub assignable: bool, } #[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)] pub struct UpdateChannelRoleParams { pub name: Option, pub description: Option, pub permissions: Option>, pub assignable: Option, } impl ImService { pub async fn channel_role_list( &self, _ctx: &ImSession, channel_id: Uuid, ) -> Result, AppError> { sqlx::query_as::<_, ChannelMemberRole>( "SELECT id, channel_id, name, description, permissions, assignable, \ created_by, created_at, updated_at \ FROM channel_member_role WHERE channel_id = $1 ORDER BY created_at", ) .bind(channel_id) .fetch_all(self.ctx.db.reader()) .await .map_err(AppError::Database) } pub async fn channel_role_create( &self, ctx: &ImSession, channel_id: Uuid, params: CreateChannelRoleParams, ) -> Result { let now = chrono::Utc::now(); sqlx::query_as::<_, ChannelMemberRole>( "INSERT INTO channel_member_role \ (id, channel_id, name, description, permissions, assignable, created_by, created_at, updated_at) \ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $8) \ RETURNING id, channel_id, name, description, permissions, assignable, \ created_by, created_at, updated_at", ) .bind(Uuid::now_v7()) .bind(channel_id) .bind(¶ms.name) .bind(params.description.as_deref()) .bind(¶ms.permissions) .bind(params.assignable) .bind(ctx.user) .bind(now) .fetch_one(self.ctx.db.writer()) .await .map_err(AppError::Database) } pub async fn channel_role_update( &self, _ctx: &ImSession, role_id: Uuid, params: UpdateChannelRoleParams, ) -> Result { let now = chrono::Utc::now(); sqlx::query_as::<_, ChannelMemberRole>( "UPDATE channel_member_role SET \ name = COALESCE($1, name), \ description = COALESCE($2, description), \ permissions = COALESCE($3, permissions), \ assignable = COALESCE($4, assignable), \ updated_at = $5 \ WHERE id = $6 \ RETURNING id, channel_id, name, description, permissions, assignable, \ created_by, created_at, updated_at", ) .bind(params.name.as_deref()) .bind(params.description.as_deref()) .bind(params.permissions.as_ref()) .bind(params.assignable) .bind(now) .bind(role_id) .fetch_one(self.ctx.db.writer()) .await .map_err(AppError::Database) } pub async fn channel_role_delete( &self, _ctx: &ImSession, role_id: Uuid, ) -> Result<(), AppError> { sqlx::query("DELETE FROM channel_member_role WHERE id = $1") .bind(role_id) .execute(self.ctx.db.writer()) .await .map_err(AppError::Database)?; Ok(()) } }