-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use crate::user::models::User; | ||
use async_trait::async_trait; | ||
use errors::Error; | ||
use mockall::automock; | ||
|
||
#[automock] | ||
#[async_trait] | ||
pub trait UserRepository: Send + Sync + 'static { | ||
async fn upsert_user(&self, user: &User) -> Result<(), Error>; | ||
} | ||
use async_trait::async_trait; | ||
use errors::Error; | ||
use mockall::automock; | ||
|
||
use crate::user::models::User; | ||
|
||
#[automock] | ||
#[async_trait] | ||
pub trait UserRepository: Send + Sync + 'static { | ||
async fn upsert_user(&self, user: &User) -> Result<(), Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
use crate::database::components::UserDatabase; | ||
use crate::database::connection::ConnectionPool; | ||
use async_trait::async_trait; | ||
use domain::user::models::User; | ||
use errors::infra::InfraError; | ||
use sea_orm::{ConnectionTrait, DatabaseBackend, Statement}; | ||
|
||
#[async_trait] | ||
impl UserDatabase for ConnectionPool { | ||
async fn upsert_user(&self, user: &User) -> Result<(), InfraError> { | ||
self.pool | ||
.execute(Statement::from_sql_and_values( | ||
DatabaseBackend::MySql, | ||
"INSERT INTO users (uuid, name) VALUES (UUID_TO_BIN(?), ?) | ||
ON DUPLICATE KEY UPDATE | ||
name = VALUES(name)", | ||
[user.id.to_string().into(), user.name.to_owned().into()], | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
use async_trait::async_trait; | ||
use domain::user::models::User; | ||
use errors::infra::InfraError; | ||
use sea_orm::{ConnectionTrait, DatabaseBackend, Statement}; | ||
|
||
use crate::database::{components::UserDatabase, connection::ConnectionPool}; | ||
|
||
#[async_trait] | ||
impl UserDatabase for ConnectionPool { | ||
async fn upsert_user(&self, user: &User) -> Result<(), InfraError> { | ||
self.pool | ||
.execute(Statement::from_sql_and_values( | ||
DatabaseBackend::MySql, | ||
"INSERT INTO users (uuid, name) VALUES (UUID_TO_BIN(?), ?) | ||
ON DUPLICATE KEY UPDATE | ||
name = VALUES(name)", | ||
[user.id.to_string().into(), user.name.to_owned().into()], | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
36 changes: 19 additions & 17 deletions
36
server/infra/resource/src/repository/user_repository_impl.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
use crate::database::components::UserDatabase; | ||
use crate::{database::components::DatabaseComponents, repository::Repository}; | ||
use async_trait::async_trait; | ||
use domain::repository::user_repository::UserRepository; | ||
use domain::user::models::User; | ||
use errors::Error; | ||
|
||
#[async_trait] | ||
impl<Client: DatabaseComponents + 'static> UserRepository for Repository<Client> { | ||
async fn upsert_user(&self, user: &User) -> Result<(), Error> { | ||
self.client | ||
.user() | ||
.upsert_user(user) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
} | ||
use async_trait::async_trait; | ||
use domain::{repository::user_repository::UserRepository, user::models::User}; | ||
use errors::Error; | ||
|
||
use crate::{ | ||
database::components::{DatabaseComponents, UserDatabase}, | ||
repository::Repository, | ||
}; | ||
|
||
#[async_trait] | ||
impl<Client: DatabaseComponents + 'static> UserRepository for Repository<Client> { | ||
async fn upsert_user(&self, user: &User) -> Result<(), Error> { | ||
self.client | ||
.user() | ||
.upsert_user(user) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
use domain::repository::user_repository::UserRepository; | ||
use domain::user::models::User; | ||
use errors::Error; | ||
|
||
pub struct UserUseCase<'a, UserRepo: UserRepository> { | ||
pub repository: &'a UserRepo, | ||
} | ||
|
||
impl<R: UserRepository> UserUseCase<'_, R> { | ||
pub async fn upsert_user(&self, user: &User) -> Result<(), Error> { | ||
self.repository.upsert_user(user).await | ||
} | ||
} | ||
use domain::{repository::user_repository::UserRepository, user::models::User}; | ||
use errors::Error; | ||
|
||
pub struct UserUseCase<'a, UserRepo: UserRepository> { | ||
pub repository: &'a UserRepo, | ||
} | ||
|
||
impl<R: UserRepository> UserUseCase<'_, R> { | ||
pub async fn upsert_user(&self, user: &User) -> Result<(), Error> { | ||
self.repository.upsert_user(user).await | ||
} | ||
} |