From 9357c577e4167fade9e9a3df4b11668437a3619c Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 31 May 2024 19:02:48 +0200 Subject: [PATCH] feat: [#448] new user repository trait --- src/services/user.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/services/user.rs b/src/services/user.rs index 05f9ecfc..dc397c17 100644 --- a/src/services/user.rs +++ b/src/services/user.rs @@ -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}; @@ -26,7 +29,7 @@ fn no_email() -> String { pub struct RegistrationService { configuration: Arc, mailer: Arc, - user_repository: Arc, + user_repository: Arc>, user_profile_repository: Arc, } @@ -35,7 +38,7 @@ impl RegistrationService { pub fn new( configuration: Arc, mailer: Arc, - user_repository: Arc, + user_repository: Arc>, user_profile_repository: Arc, ) -> Self { Self { @@ -184,7 +187,7 @@ impl RegistrationService { } pub struct BanService { - user_repository: Arc, + user_repository: Arc>, user_profile_repository: Arc, banned_user_list: Arc, } @@ -192,7 +195,7 @@ pub struct BanService { impl BanService { #[must_use] pub fn new( - user_repository: Arc, + user_repository: Arc>, user_profile_repository: Arc, banned_user_list: Arc, ) -> Self { @@ -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; + 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; +} + pub struct DbUserRepository { database: Arc>, } @@ -242,13 +254,16 @@ impl DbUserRepository { pub fn new(database: Arc>) -> 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 { + async fn get_compact(&self, user_id: &UserId) -> Result { // todo: persistence layer should have its own errors instead of // returning a `ServiceError`. self.database @@ -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 } @@ -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 } @@ -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 { + async fn add(&self, username: &str, email: &str, password_hash: &str) -> Result { self.database.insert_user_and_get_id(username, email, password_hash).await } }