Skip to content

Commit

Permalink
more repos
Browse files Browse the repository at this point in the history
  • Loading branch information
monzeromer-lab committed Jul 13, 2024
1 parent 417304e commit e45afd7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 21 deletions.
12 changes: 8 additions & 4 deletions src/domain/collaboration/repository.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use async_trait::async_trait;
use sea_orm::DbErr;

pub type Collaboration = crate::domain::collaboration::model::Model;

pub struct NewCollaboration {
pub task_id: i32,
pub user_id: i32
}

#[async_trait]
pub trait CollaborationRepository {
fn find_task_collaborators(task_id: i32) -> Vec<Collaboration>;
fn find_user_collaborations(user_id: i32) -> Vec<Collaboration>;
fn add(collaboration: NewCollaboration) -> Collaboration;
fn remove(collaboration_id: i32) -> bool;
fn find_task_collaborators(&self, task_id: i32) -> Result<Vec<Collaboration>, DbErr>;
fn find_user_collaborations(&self, user_id: i32) -> Result<Vec<Collaboration>, DbErr>;
fn add(&self, collaboration: NewCollaboration) -> Result<Collaboration, DbErr>;
fn remove(&self, collaboration_id: i32) -> Result<bool, DbErr>;
}
1 change: 1 addition & 0 deletions src/domain/notification/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Model {
pub user_id: Option<i32>,
pub task_id: Option<i32>,
pub message: String,
#[sea_orm(default_value = false)]
pub read: bool,
pub created_at: Option<DateTime>,
}
Expand Down
20 changes: 8 additions & 12 deletions src/domain/notification/repository.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
pub type Notification = crate::domain::notification::model::Model;

pub struct NewNotification {
pub user_id: Option<i32>,
pub task_id: Option<i32>,
pub message: String,
pub read: bool,
}
use async_trait::async_trait;
use sea_orm::DbErr;
use super::model::{ Model as Notification };

#[async_trait]
pub trait NotificationRepository {
fn find_notifications(user_id: i32, page_number: u8) -> Vec<Notification>;
fn create_notification(notification: NewNotification) -> Notification;
fn mark_notification_as_read(notification_id: i32) -> bool;
}
async fn find_notifications(&self, user_id: i32, page_number: u8) -> Result<Vec<Notification>, DbErr>;
async fn create_notification(&self, notification: Notification) -> Result<Notification, DbErr>;
async fn mark_notification_as_read(&self, notification_id: i32) -> Result<Notification, DbErr>;
}
1 change: 0 additions & 1 deletion src/domain/task/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ pub trait TaskRepository {
async fn update(&self, task_id: i32, task: Task) -> Result<Task, DbErr>;
async fn delete(&self, task_id: i32) -> Result<bool, DbErr>;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use sea_orm::DatabaseConnection;
use async_trait::async_trait;
use sea_orm::DbErr;
use crate::domain::collaboration::repository::Collaboration;
use crate::domain::collaboration::repository::CollaborationRepository;

pub struct CollaborationRepo {
db: DatabaseConnection
}

impl CollaborationRepo {
fn _new(db: DatabaseConnection) -> Self {
Self { db }
}
}

#[async_trait]
impl CollaborationRepository for CollaborationRepo {
fn find_task_collaborators(&self, task_id: i32) -> Result<Vec<Collaboration>, DbErr> {
todo!()
}

fn find_user_collaborations(&self, user_id: i32) -> Result<Vec<Collaboration>, DbErr> {
todo!()
}

fn add(&self, collaboration: crate::domain::collaboration::repository::NewCollaboration) -> Result<Collaboration, DbErr> {
todo!()
}

fn remove(&self, collaboration_id: i32) -> Result<bool, DbErr> {
todo!()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use sea_orm::{ActiveModelTrait, ColumnTrait, DatabaseConnection, DbErr, EntityTrait, PaginatorTrait, QueryFilter, Set};
use async_trait::async_trait;
use crate::domain::notification::model::{Entity as NotificationEntity, Model as Notification, Column, ActiveModel};
use crate::domain::notification::repository::NotificationRepository;

pub struct NotificationRepo {
db: DatabaseConnection,
}

impl NotificationRepo {
pub fn _new(db: DatabaseConnection) -> Self {
Self { db }
}
}

#[async_trait]
impl NotificationRepository for NotificationRepo {
async fn find_notifications(&self, user_id: i32, page_number: u8) -> Result<Vec<crate::domain::notification::model::Model>, sea_orm::DbErr> {
NotificationEntity::find()
.filter(Column::UserId.eq(user_id))
.paginate(&self.db, page_number as u64)
.fetch()
.await
}

async fn create_notification(&self, notification: Notification) -> Result<crate::domain::notification::model::Model, sea_orm::DbErr> {
let new_notification = ActiveModel {
user_id: Set(notification.user_id),
task_id: Set(notification.task_id),
message: Set(notification.message),
..Default::default()
};
new_notification.insert(&self.db).await
}

async fn mark_notification_as_read(&self, notification_id: i32) -> Result<Notification, sea_orm::DbErr> {
let notification_record = NotificationEntity::find_by_id(notification_id).one(&self.db).await?;
let mut notification: ActiveModel = notification_record.ok_or(DbErr::RecordNotFound("No Such a Notification".to_string()))?.into();

notification.read = Set(true);

notification.update(&self.db).await

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ impl TaskRepository for TaskRepo {
}

async fn find(&self, limit: Option<u8>) -> Result<Vec<Task>, DbErr> {
let query = TaskEntity::find()
TaskEntity::find()
.limit(limit.unwrap_or(10) as u64)
.all(&self.db)
.await?;

Ok(query)
.await
}

async fn filter_tasks(
Expand Down

0 comments on commit e45afd7

Please # to comment.