init
This commit is contained in:
+106
-4
@@ -1,7 +1,9 @@
|
||||
use crate::models::channel;
|
||||
use crate::models::{channel, channel_role_permission, channel_user_permission};
|
||||
use crate::repositories::{AnyResult, RepositoryContext};
|
||||
use sea_orm::{ActiveModelTrait, EntityTrait};
|
||||
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 {
|
||||
@@ -9,7 +11,7 @@ pub struct ChannelRepository {
|
||||
}
|
||||
|
||||
impl ChannelRepository {
|
||||
pub async fn get_by_id(&self, id: uuid::Uuid) -> AnyResult<Option<channel::Model>> {
|
||||
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?)
|
||||
@@ -31,11 +33,111 @@ impl ChannelRepository {
|
||||
Ok(channel)
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: uuid::Uuid) -> AnyResult<bool> {
|
||||
pub async fn delete(&self, id: Uuid) -> AnyResult<bool> {
|
||||
let res = channel::Entity::delete_by_id(id)
|
||||
.exec(&self.context.db)
|
||||
.await?;
|
||||
self.context.events.emit("channel_deleted", id);
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user