Skip to content

Commit

Permalink
Merge access_context.rs from 21-get-model
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Nov 28, 2023
1 parent 5d93cf7 commit d5fc74a
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/database/access_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,39 @@ use crate::database::entity_context::EntityContextTrait;
use crate::entities::access;
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::ActiveValue::{Set, Unchanged};
use sea_orm::{ActiveModelTrait, DbErr, EntityTrait};
use sea_orm::{ActiveModelTrait, ColumnTrait, Condition, DbErr, EntityTrait, QueryFilter};
use std::sync::Arc;

pub struct AccessContext {
db_context: Arc<dyn DatabaseContextTrait>,
}

pub trait AccessContextTrait: EntityContextTrait<access::Model> {}
#[async_trait]
pub trait AccessContextTrait: EntityContextTrait<access::Model> {
async fn get_access_by_uid_and_model_id(
&self,
uid: i32,
model_id: i32,
) -> Result<Option<access::Model>, DbErr>;
}

impl AccessContextTrait for AccessContext {}
#[async_trait]
impl AccessContextTrait for AccessContext {
async fn get_access_by_uid_and_model_id(
&self,
uid: i32,
model_id: i32,
) -> Result<Option<access::Model>, DbErr> {
access::Entity::find()
.filter(
Condition::all()
.add(access::Column::UserId.eq(uid))
.add(access::Column::ModelId.eq(model_id)),
)
.one(&self.db_context.get_connection())
.await
}
}

impl AccessContext {
pub fn new(db_context: Arc<dyn DatabaseContextTrait>) -> AccessContext {
Expand Down

0 comments on commit d5fc74a

Please sign in to comment.