Skip to content

Commit

Permalink
feat: add config to disable modify the data (#15)
Browse files Browse the repository at this point in the history
* feat: use mock when register to avoid spam

* feat: support mock response when edit or delete user

* chore: rename ALLOW_MODIFIED to IS_SUPPORT_MODIFIED

* fix: temporary to remove the never warning
  • Loading branch information
Lzyct authored Aug 22, 2024
1 parent af77074 commit 1ffa0b9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ MAIL_API_KEY=your_mailjet_api_key
MAIL_SECRET_KEY=your_mailjet_secret_key

APP_HOST=localhost
APP_PORT=8000
APP_PORT=8000

IS_SUPPORT_MODIFIED=false
1 change: 1 addition & 0 deletions src/core/middlewares/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
features::auth::data::models::general_token::GeneralToken,
};

#[allow(dead_code)]
pub struct GeneralMiddleware {
pub data: TokenData<GeneralToken>,
}
Expand Down
43 changes: 43 additions & 0 deletions src/features/user/data/repository/user_repository_impl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use chrono::Utc;
use diesel::prelude::*;
use diesel::{ExpressionMethods, RunQueryDsl};
Expand Down Expand Up @@ -38,6 +40,22 @@ impl UserRepositoryImpl for UserRepository {
let _ = user.hash_password();
let email_register = user.email.clone();

let is_allow_modified =
env::var("IS_SUPPORT_MODIFIED").expect("DATABASE_URL not found.") == "true";

if !is_allow_modified {
// return mock user
return Ok(UserResponse {
id: user.id,
name: user.name,
email: user.email,
photo: user.photo,
verified: user.verified,
created_at: user.created_at,
updated_at: user.updated_at,
});
}

diesel::insert_into(users::table)
.values(&user)
.execute(&mut self.source.get().unwrap())
Expand Down Expand Up @@ -78,8 +96,24 @@ impl UserRepositoryImpl for UserRepository {
}

fn update_user(&self, user_id: Uuid, params: UpdateUserParams) -> AppResult<UserResponse> {
let is_allow_modified =
env::var("IS_SUPPORT_MODIFIED").expect("DATABASE_URL not found.") == "true";

self.find_user_by_id(user_id)
.map(|user| {
if !is_allow_modified {
// return mock response
return Ok(UserResponse {
id: user_id,
name: params.name.unwrap_or("".to_string()),
email: user.email.to_string(),
photo: params.photo.unwrap_or("".to_string()),
verified: params.verified.unwrap_or(false),
created_at: user.created_at,
updated_at: Utc::now().naive_utc(),
});
}

diesel::update(users.find(user.id))
.set((
name.eq(params.name.unwrap_or(user.name)),
Expand All @@ -103,8 +137,17 @@ impl UserRepositoryImpl for UserRepository {
}

fn delete(&self, user_id: Uuid) -> AppResult<String> {
let is_allow_modified =
env::var("IS_SUPPORT_MODIFIED").expect("DATABASE_URL not found.") == "true";

self.find_user_by_id(user_id)
.map(|user| {
if !is_allow_modified {
return Ok(format!(
"User with id '{}' deleted successfully",
user.email
));
}
diesel::delete(users.find(user.id))
.execute(&mut self.source.get().unwrap())
.map(|_| format!("User with email '{}' deleted successfully", user.email))
Expand Down

0 comments on commit 1ffa0b9

Please sign in to comment.