Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: user feature #11

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/core/config/di.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
use std::sync::Arc;

use crate::{
core::types::DBConn,
features::{
auth::data::repository::auth_repository_impl::AuthRepository,
user::{
data::repository::user_repository_impl::UserRepository,
domain::usecase::service::UserService,
},
user::data::repository::user_repository_impl::UserRepository,
},
};

#[derive(Clone)]
pub struct DiContainer {
pub user_service: UserService,
pub auth_repository: AuthRepository,
pub user_repository: UserRepository,
}

impl DiContainer {
pub fn new(db_conn: &DBConn) -> Self {
// user
let user_repository = UserRepository::new(db_conn.clone());
let user_service = UserService::new(Arc::new(user_repository.clone()));

// auth
let auth_repository = AuthRepository::new(db_conn.clone());

Self {
user_service,
auth_repository,
user_repository,
}
}
}
11 changes: 7 additions & 4 deletions src/core/config/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ pub fn config_services(cfg: &mut web::ServiceConfig) {
web::scope("/user")
.service(
web::resource("")
.route(web::post().to(user_controller::register))
.route(web::post().to(user_controller::register_controller))
.route(web::get().to(user_controller::get_user))
.route(web::put().to(user_controller::update_user))
.route(web::delete().to(user_controller::delete_user)),
.route(web::put().to(user_controller::update_user_controller))
.route(web::delete().to(user_controller::delete_user_controller)),
)
.service(web::resource("/all").route(web::get().to(user_controller::users))),
.service(
web::resource("/all")
.route(web::get().to(user_controller::users_controller)),
),
),
)
.default_service(web::route().to(route_not_found));
Expand Down
8 changes: 4 additions & 4 deletions src/core/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use crate::{
data::models::auth_token::AuthToken,
domain::repository::auth_repository::AuthRepositoryImpl,
},
user::{
domain::entity::user_response::UserEntity, domain::usecase::interface::IUserService,
user::domain::{
entity::user_response::UserResponse, repository::user_repository::UserRepositoryImpl,
},
},
};

pub struct AuthMiddleware {
pub user: UserEntity,
pub user: UserResponse,
pub login_session: Uuid,
}

Expand Down Expand Up @@ -72,7 +72,7 @@ impl FromRequest for AuthMiddleware {
}
})?;

let user = di.user_service.find_user_by_id(user_id).map_err(|_| {
let user = di.user_repository.find_user_by_id(user_id).map_err(|_| {
APIError::UnauthorizedMessage {
message: "User not found".to_string(),
}
Expand Down
25 changes: 13 additions & 12 deletions src/features/user/data/repository/user_repository_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use diesel::prelude::*;
use diesel::{ExpressionMethods, RunQueryDsl};
use uuid::Uuid;

use crate::features::user::domain::usecase::update_user::UpdateUserParams;
use crate::{
core::{
error::APIError,
Expand All @@ -11,10 +12,10 @@ use crate::{
features::user::{
data::models::user::User,
domain::{
entity::user_response::UserEntity,
entity::user_response::UsersEntity,
entity::user_response::UserResponse,
entity::user_response::UsersResponse,
repository::user_repository::UserRepositoryImpl,
usecase::dto::{PaginationParams, RegisterParams, UpdateUserParams},
usecase::{list_user::PaginationParams, register::RegisterParams},
},
},
schema::users::{self, dsl::*},
Expand All @@ -32,15 +33,15 @@ impl UserRepository {
}

impl UserRepositoryImpl for UserRepository {
fn create(&self, params: RegisterParams) -> AppResult<UserEntity> {
fn create(&self, params: RegisterParams) -> AppResult<UserResponse> {
let mut user = User::from(params);
let _ = user.hash_password();
let email_register = user.email.clone();

diesel::insert_into(users::table)
.values(&user)
.execute(&mut self.source.get().unwrap())
.map(|_| UserEntity {
.map(|_| UserResponse {
id: user.id,
name: user.name,
email: user.email,
Expand All @@ -60,11 +61,11 @@ impl UserRepositoryImpl for UserRepository {
})
}

fn find_user_by_id(&self, user_id: Uuid) -> AppResult<UserEntity> {
fn find_user_by_id(&self, user_id: Uuid) -> AppResult<UserResponse> {
users::table
.filter(id.eq(user_id))
.get_result::<User>(&mut self.source.get().unwrap())
.map(|user| UserEntity {
.map(|user| UserResponse {
id: user.id,
name: user.name,
email: user.email,
Expand All @@ -76,7 +77,7 @@ impl UserRepositoryImpl for UserRepository {
.map_err(|_| APIError::UserNotFoundError)
}

fn update_user(&self, user_id: Uuid, params: UpdateUserParams) -> AppResult<UserEntity> {
fn update_user(&self, user_id: Uuid, params: UpdateUserParams) -> AppResult<UserResponse> {
self.find_user_by_id(user_id)
.map(|user| {
diesel::update(users.find(user.id))
Expand All @@ -87,7 +88,7 @@ impl UserRepositoryImpl for UserRepository {
updated_at.eq(Utc::now().naive_utc()),
))
.get_result::<User>(&mut self.source.get().unwrap())
.map(|updated_user| UserEntity {
.map(|updated_user| UserResponse {
id: updated_user.id,
name: updated_user.name,
email: updated_user.email,
Expand All @@ -112,7 +113,7 @@ impl UserRepositoryImpl for UserRepository {
.map_err(|_| APIError::UserNotFoundError)?
}

fn users(&self, params: PaginationParams) -> AppResult<UsersEntity> {
fn users(&self, params: PaginationParams) -> AppResult<UsersResponse> {
// Calculate the offset (skip `page * per_page` results)
let page = params.page.unwrap_or(1);
let per_page = params.per_page.unwrap_or(20);
Expand All @@ -128,10 +129,10 @@ impl UserRepositoryImpl for UserRepository {
.limit(per_page)
.offset(offset)
.load::<User>(&mut self.source.get().unwrap())
.map(|list_user| UsersEntity {
.map(|list_user| UsersResponse {
users: list_user
.iter()
.map(|user| UserEntity {
.map(|user| UserResponse {
id: user.id,
name: user.name.clone(),
email: user.email.clone(),
Expand Down
6 changes: 3 additions & 3 deletions src/features/user/domain/entity/user_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use uuid::Uuid;

use crate::camel_case_struct;

camel_case_struct!(UserEntity {
camel_case_struct!(UserResponse {
id: Uuid,
name: String,
email: String,
Expand All @@ -12,8 +12,8 @@ camel_case_struct!(UserEntity {
updated_at: chrono::NaiveDateTime,
});

camel_case_struct!(UsersEntity {
users: Vec<UserEntity>,
camel_case_struct!(UsersResponse {
users: Vec<UserResponse>,
total: i64,
page: i64,
per_page: i64,
Expand Down
13 changes: 8 additions & 5 deletions src/features/user/domain/repository/user_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use uuid::Uuid;
use crate::{
core::types::AppResult,
features::user::domain::{
entity::user_response::UserEntity, entity::user_response::UsersEntity, usecase::dto::*,
entity::user_response::{UserResponse, UsersResponse},
usecase::{
list_user::PaginationParams, register::RegisterParams, update_user::UpdateUserParams,
},
},
};

pub trait UserRepositoryImpl: Send + Sync {
fn create(&self, params: RegisterParams) -> AppResult<UserEntity>;
fn find_user_by_id(&self, user_id: Uuid) -> AppResult<UserEntity>;
fn update_user(&self, user_id: Uuid, params: UpdateUserParams) -> AppResult<UserEntity>;
fn create(&self, params: RegisterParams) -> AppResult<UserResponse>;
fn find_user_by_id(&self, user_id: Uuid) -> AppResult<UserResponse>;
fn update_user(&self, user_id: Uuid, params: UpdateUserParams) -> AppResult<UserResponse>;
fn delete(&self, user_id: Uuid) -> AppResult<String>;
fn users(&self, params: PaginationParams) -> AppResult<UsersEntity>;
fn users(&self, params: PaginationParams) -> AppResult<UsersResponse>;
}
13 changes: 13 additions & 0 deletions src/features/user/domain/usecase/delete_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use uuid::Uuid;

use crate::{
core::types::AppResult,
features::user::{
data::repository::user_repository_impl::UserRepository,
domain::repository::user_repository::UserRepositoryImpl,
},
};

pub fn delete_user(user_repository: &UserRepository, user_id: Uuid) -> AppResult<String> {
user_repository.delete(user_id)
}
55 changes: 0 additions & 55 deletions src/features/user/domain/usecase/dto.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/features/user/domain/usecase/interface.rs

This file was deleted.

23 changes: 23 additions & 0 deletions src/features/user/domain/usecase/list_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{
core::types::AppResult,
features::user::{
data::repository::user_repository_impl::UserRepository,
domain::{
entity::user_response::UsersResponse, repository::user_repository::UserRepositoryImpl,
},
},
};

use crate::camel_case_struct;

camel_case_struct!(PaginationParams {
page: Option<i64>,
per_page: Option<i64>,
});

pub fn list_user(
user_repository: &UserRepository,
params: PaginationParams,
) -> AppResult<UsersResponse> {
user_repository.users(params)
}
7 changes: 4 additions & 3 deletions src/features/user/domain/usecase/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dto;
pub mod interface;
pub mod service;
pub mod delete_user;
pub mod list_user;
pub mod register;
pub mod update_user;
Loading
Loading