Skip to content

Commit

Permalink
refactor: Add pattern to declare user_redis at default variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
bush1D3v committed Jul 31, 2024
1 parent a521ce3 commit 6bf3317
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
57 changes: 26 additions & 31 deletions src/modules/user/user_controllers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ async fn insert_user(
} else {
match insert_user_service(queue.clone(), pg_pool, body).await {
Ok(resp) => match UserSerdes::serde_json_to_string(&resp) {
Ok(string_user) => {
let _ = Redis::set(&redis_pool, &resp.id, &string_user).await;
let _ = Redis::set(&redis_pool, &resp.email, &string_user).await;
Ok(redis_user) => {
let _ = Redis::set(&redis_pool, &resp.id, &redis_user).await;
let _ = Redis::set(&redis_pool, &resp.email, &redis_user).await;
HttpResponse::Created()
.append_header(("Location", format!("/user/{}", resp.id)))
.finish()
Expand Down Expand Up @@ -222,12 +222,12 @@ async fn login_user(
Ok(_) => (),
Err(e) => return HttpResponse::BadRequest().json(e),
};
let string_user = match Redis::get(&redis_pool, &body.email).await {
Ok(string_user) => string_user,
let redis_user = match Redis::get(&redis_pool, &body.email).await {
Ok(redis_user) => redis_user,
Err(_) => String::from(""),
};
if !string_user.is_empty() {
let user_dto = match UserSerdes::serde_string_to_json(&string_user) {
if !redis_user.is_empty() {
let user_dto = match UserSerdes::serde_string_to_json(&redis_user) {
Ok(user) => user,
Err(e) => return e,
};
Expand All @@ -237,24 +237,19 @@ async fn login_user(
};
match login_user_service(web::Json(user), pg_pool, true).await {
Ok(service_resp) => {
login_user_response_constructor(
service_resp,
&redis_pool,
&string_user,
&body.email,
)
.await
login_user_response_constructor(service_resp, &redis_pool, &redis_user, &body.email)
.await
}
Err(e) => e,
}
} else {
match login_user_service(web::Json(body.clone()), pg_pool, false).await {
Ok(service_resp) => match UserSerdes::serde_json_to_string(&service_resp.user) {
Ok(string_user) => {
Ok(redis_user) => {
login_user_response_constructor(
service_resp,
&redis_pool,
&string_user,
&redis_user,
&body.email,
)
.await
Expand All @@ -277,11 +272,11 @@ struct LoginUserControllerResponse {
async fn login_user_response_constructor(
service_resp: LoginUserServiceResponse,
redis_pool: &deadpool_redis::Pool,
string_user: &str,
redis_user: &str,
email: &str,
) -> HttpResponse<BoxBody> {
let _ = Redis::set(redis_pool, &service_resp.user.id, string_user).await;
let _ = Redis::set(redis_pool, email, string_user).await;
let _ = Redis::set(redis_pool, &service_resp.user.id, redis_user).await;
let _ = Redis::set(redis_pool, email, redis_user).await;
HttpResponse::Ok().json(LoginUserControllerResponse {
access_token: service_resp.access_token,
access_expires_in: service_resp.access_expires_in,
Expand Down Expand Up @@ -606,17 +601,17 @@ async fn delete_user(
Ok(_) => (),
Err(e) => return HttpResponse::BadRequest().json(e),
};
let string_user = match Redis::get(&redis_pool, &user_id).await {
Ok(string_user) => string_user,
let redis_user = match Redis::get(&redis_pool, &user_id).await {
Ok(redis_user) => redis_user,
Err(_) => String::from(""),
};
if !string_user.is_empty() {
if !redis_user.is_empty() {
match delete_user_service(
pg_pool,
queue,
body.password.clone(),
user_id.clone(),
Some(string_user),
Some(redis_user),
)
.await
{
Expand Down Expand Up @@ -772,26 +767,26 @@ async fn put_user(
None,
))
} else {
let string_user = match Redis::get(&redis_pool, &user_id).await {
Ok(string_user) => string_user,
let redis_user = match Redis::get(&redis_pool, &user_id).await {
Ok(redis_user) => redis_user,
Err(_) => String::from(""),
};
match put_user_service(
pg_pool,
queue,
body.clone(),
user_id.clone(),
Some(string_user),
Some(redis_user),
)
.await
{
Ok(service_resp) => match UserSerdes::serde_json_to_string(&service_resp) {
Ok(string_user) => {
Ok(redis_user) => {
put_user_response_constructor(
&redis_pool,
&service_resp.id,
&body.email,
&string_user,
&redis_user,
&body.new_email,
)
.await
Expand All @@ -807,12 +802,12 @@ async fn put_user_response_constructor(
redis_pool: &deadpool_redis::Pool,
user_id: &str,
excluded_email: &str,
string_user: &str,
redis_user: &str,
new_email: &str,
) -> HttpResponse {
let _ = Redis::delete(redis_pool, excluded_email).await;
let _ = Redis::set(redis_pool, user_id, string_user).await;
let _ = Redis::set(redis_pool, new_email, string_user).await;
let _ = Redis::set(redis_pool, user_id, redis_user).await;
let _ = Redis::set(redis_pool, new_email, redis_user).await;
HttpResponse::Accepted()
.append_header(("Location", format!("/user/{}", user_id)))
.finish()
Expand Down
12 changes: 6 additions & 6 deletions src/modules/user/user_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ pub async fn delete_user_service(
queue: Data<Arc<DeleteUserAppQueue>>,
user_password: String,
user_id: String,
string_user: Option<String>,
redis_user: Option<String>,
) -> Result<String, HttpResponse> {
let db_user: UserDTO = if string_user.is_none() {
let db_user: UserDTO = if redis_user.is_none() {
match detail_user_repository(pg_pool.clone(), user_id.clone()).await {
Ok(user_dto) => user_dto,
Err(e) => return Err(e),
}
} else {
match UserSerdes::serde_string_to_json(&string_user.unwrap()) {
match UserSerdes::serde_string_to_json(&redis_user.unwrap()) {
Ok(user_dto) => user_dto,
Err(e) => return Err(e),
}
Expand Down Expand Up @@ -171,20 +171,20 @@ pub async fn put_user_service(
queue: Data<Arc<PutUserAppQueue>>,
mut body: PutUserDTO,
user_id: String,
string_user: Option<String>,
redis_user: Option<String>,
) -> Result<UserDTO, HttpResponse> {
match email_exists(pg_pool.clone(), body.new_email.clone()).await {
Ok(_) => (),
Err(e) => return Err(e),
};

let mut db_user: UserDTO = if string_user == Some(String::from("")) || string_user.is_none() {
let mut db_user: UserDTO = if redis_user == Some(String::from("")) || redis_user.is_none() {
match detail_user_repository(pg_pool.clone(), user_id.clone()).await {
Ok(user_dto) => user_dto,
Err(e) => return Err(e),
}
} else {
match UserSerdes::serde_string_to_json(&string_user.unwrap()) {
match UserSerdes::serde_string_to_json(&redis_user.unwrap()) {
Ok(user_dto) => user_dto,
Err(e) => return Err(e),
}
Expand Down

0 comments on commit 6bf3317

Please sign in to comment.