150 lines
4.9 KiB
Rust
150 lines
4.9 KiB
Rust
use crate::models::{channel, channel_role_permission, channel_user_permission};
|
|
use crate::repositories::types::ChannelFilter;
|
|
use crate::repositories::{AnyResult, RepositoryContext};
|
|
use sea_orm::sea_query::OnConflict;
|
|
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
|
|
use std::sync::Arc;
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ChannelRepository {
|
|
pub context: Arc<RepositoryContext>,
|
|
}
|
|
|
|
impl ChannelRepository {
|
|
pub async fn get_by_id(&self, id: Uuid) -> AnyResult<Option<channel::Model>> {
|
|
Ok(channel::Entity::find_by_id(id)
|
|
.one(&self.context.db)
|
|
.await?)
|
|
}
|
|
|
|
pub async fn get_all(&self) -> AnyResult<Vec<channel::Model>> {
|
|
Ok(channel::Entity::find().all(&self.context.db).await?)
|
|
}
|
|
|
|
pub async fn filter(&self, filter: ChannelFilter) -> AnyResult<Vec<channel::Model>> {
|
|
let mut query = channel::Entity::find();
|
|
if let Some(s_id) = filter.server_id {
|
|
query = query.filter(channel::Column::ServerId.eq(s_id));
|
|
}
|
|
Ok(query.all(&self.context.db).await?)
|
|
}
|
|
|
|
pub async fn update(&self, active: channel::ActiveModel) -> AnyResult<channel::Model> {
|
|
let channel = active.update(&self.context.db).await?;
|
|
Ok(channel)
|
|
}
|
|
|
|
pub async fn create(&self, active: channel::ActiveModel) -> AnyResult<channel::Model> {
|
|
let channel = active.insert(&self.context.db).await?;
|
|
Ok(channel)
|
|
}
|
|
|
|
pub async fn delete(&self, id: Uuid) -> AnyResult<bool> {
|
|
let res = channel::Entity::delete_by_id(id)
|
|
.exec(&self.context.db)
|
|
.await?;
|
|
Ok(res.rows_affected > 0)
|
|
}
|
|
|
|
pub async fn get_user_permission(
|
|
&self,
|
|
channel_id: Uuid,
|
|
user_id: Uuid,
|
|
) -> AnyResult<Option<channel_user_permission::Model>> {
|
|
Ok(channel_user_permission::Entity::find()
|
|
.filter(channel_user_permission::Column::ChannelId.eq(channel_id))
|
|
.filter(channel_user_permission::Column::UserId.eq(user_id))
|
|
.one(&self.context.db)
|
|
.await?)
|
|
}
|
|
|
|
pub async fn set_user_permission(
|
|
&self,
|
|
channel_id: Uuid,
|
|
user_id: Uuid,
|
|
permissions: u64,
|
|
) -> AnyResult<()> {
|
|
let permission = channel_user_permission::ActiveModel {
|
|
channel_id: Set(channel_id),
|
|
user_id: Set(user_id),
|
|
permissions: Set(permissions as i64),
|
|
..Default::default()
|
|
};
|
|
|
|
channel_user_permission::Entity::insert(permission)
|
|
.on_conflict(
|
|
OnConflict::columns([
|
|
channel_user_permission::Column::ChannelId,
|
|
channel_user_permission::Column::UserId,
|
|
])
|
|
.update_columns([channel_user_permission::Column::Permissions])
|
|
.to_owned(),
|
|
)
|
|
.exec(&self.context.db)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn remove_user_permission(&self, channel_id: Uuid, user_id: Uuid) -> AnyResult<()> {
|
|
channel_user_permission::Entity::delete_many()
|
|
.filter(channel_user_permission::Column::ChannelId.eq(channel_id))
|
|
.filter(channel_user_permission::Column::UserId.eq(user_id))
|
|
.exec(&self.context.db)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn get_role_permission(
|
|
&self,
|
|
channel_id: Uuid,
|
|
role_id: Uuid,
|
|
) -> AnyResult<Option<channel_role_permission::Model>> {
|
|
Ok(channel_role_permission::Entity::find()
|
|
.filter(channel_role_permission::Column::ChannelId.eq(channel_id))
|
|
.filter(channel_role_permission::Column::RoleId.eq(role_id))
|
|
.one(&self.context.db)
|
|
.await?)
|
|
}
|
|
|
|
pub async fn set_role_permission(
|
|
&self,
|
|
channel_id: Uuid,
|
|
role_id: Uuid,
|
|
permissions: u64,
|
|
) -> AnyResult<()> {
|
|
let permission = channel_role_permission::ActiveModel {
|
|
channel_id: Set(channel_id),
|
|
role_id: Set(role_id),
|
|
permissions: Set(permissions as i64),
|
|
..Default::default()
|
|
};
|
|
|
|
channel_role_permission::Entity::insert(permission)
|
|
.on_conflict(
|
|
OnConflict::columns([
|
|
channel_role_permission::Column::ChannelId,
|
|
channel_role_permission::Column::RoleId,
|
|
])
|
|
.update_columns([channel_role_permission::Column::Permissions])
|
|
.to_owned(),
|
|
)
|
|
.exec(&self.context.db)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn remove_role_permission(&self, channel_id: Uuid, role_id: Uuid) -> AnyResult<()> {
|
|
channel_role_permission::Entity::delete_many()
|
|
.filter(channel_role_permission::Column::ChannelId.eq(channel_id))
|
|
.filter(channel_role_permission::Column::RoleId.eq(role_id))
|
|
.exec(&self.context.db)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|