Skip to content

Commit

Permalink
feat: [#448] new user repository trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Jun 3, 2024
1 parent a2ce990 commit b1d5536
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::sync::Arc;

use argon2::password_hash::SaltString;
use argon2::{Argon2, PasswordHasher};
use async_trait::async_trait;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use log::{debug, info};
#[cfg(test)]
use mockall::automock;
use pbkdf2::password_hash::rand_core::OsRng;

use crate::config::{Configuration, EmailOnSignup};
Expand All @@ -26,7 +29,7 @@ fn no_email() -> String {
pub struct RegistrationService {
configuration: Arc<Configuration>,
mailer: Arc<mailer::Service>,
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
}

Expand All @@ -35,7 +38,7 @@ impl RegistrationService {
pub fn new(
configuration: Arc<Configuration>,
mailer: Arc<mailer::Service>,
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
) -> Self {
Self {
Expand Down Expand Up @@ -184,15 +187,15 @@ impl RegistrationService {
}

pub struct BanService {
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
banned_user_list: Arc<DbBannedUserList>,
}

impl BanService {
#[must_use]
pub fn new(
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
banned_user_list: Arc<DbBannedUserList>,
) -> Self {
Expand Down Expand Up @@ -233,6 +236,15 @@ impl BanService {
}
}

#[cfg_attr(test, automock)]
#[async_trait]
pub trait Repository: Sync + Send {
async fn get_compact(&self, user_id: &UserId) -> Result<UserCompact, ServiceError>;
async fn grant_admin_role(&self, user_id: &UserId) -> Result<(), Error>;
async fn delete(&self, user_id: &UserId) -> Result<(), Error>;
async fn add(&self, username: &str, email: &str, password_hash: &str) -> Result<UserId, Error>;
}

pub struct DbUserRepository {
database: Arc<Box<dyn Database>>,
}
Expand All @@ -242,13 +254,16 @@ impl DbUserRepository {
pub fn new(database: Arc<Box<dyn Database>>) -> Self {
Self { database }
}
}

#[async_trait]
impl Repository for DbUserRepository {
/// It returns the compact user.
///
/// # Errors
///
/// It returns an error if there is a database error.
pub async fn get_compact(&self, user_id: &UserId) -> Result<UserCompact, ServiceError> {
async fn get_compact(&self, user_id: &UserId) -> Result<UserCompact, ServiceError> {
// todo: persistence layer should have its own errors instead of
// returning a `ServiceError`.
self.database
Expand All @@ -262,7 +277,7 @@ impl DbUserRepository {
/// # Errors
///
/// It returns an error if there is a database error.
pub async fn grant_admin_role(&self, user_id: &UserId) -> Result<(), Error> {
async fn grant_admin_role(&self, user_id: &UserId) -> Result<(), Error> {
self.database.grant_admin_role(*user_id).await
}

Expand All @@ -271,7 +286,7 @@ impl DbUserRepository {
/// # Errors
///
/// It returns an error if there is a database error.
pub async fn delete(&self, user_id: &UserId) -> Result<(), Error> {
async fn delete(&self, user_id: &UserId) -> Result<(), Error> {
self.database.delete_user(*user_id).await
}

Expand All @@ -280,7 +295,7 @@ impl DbUserRepository {
/// # Errors
///
/// It returns an error if there is a database error.
pub async fn add(&self, username: &str, email: &str, password_hash: &str) -> Result<UserId, Error> {
async fn add(&self, username: &str, email: &str, password_hash: &str) -> Result<UserId, Error> {
self.database.insert_user_and_get_id(username, email, password_hash).await
}
}
Expand Down

0 comments on commit b1d5536

Please sign in to comment.