Skip to content

Commit

Permalink
refactor: The borrowed expression implements the required traits
Browse files Browse the repository at this point in the history
  • Loading branch information
bush1D3v committed Jul 29, 2024
1 parent 7308938 commit 84be450
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/modules/user/user_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use crate::utils::{
};
use actix_web::{web::Data, HttpResponse};
use deadpool_postgres::Pool;
use sql_builder::{quote, SqlBuilder};
use sql_builder::SqlBuilder;

pub async fn email_exists(pg_pool: Data<Pool>, email: String) -> Result<(), HttpResponse> {
let mut sql_builder = SqlBuilder::select_from("users");
sql_builder.field("id");
sql_builder.or_where_eq("email", &quote(email.clone()));
sql_builder.or_where_eq("email", email.clone());

let rows = match query_constructor_executor(pg_pool, sql_builder).await {
Ok(x) => x,
Expand All @@ -31,7 +31,7 @@ pub async fn email_exists(pg_pool: Data<Pool>, email: String) -> Result<(), Http
pub async fn email_not_exists(pg_pool: Data<Pool>, email: String) -> Result<(), HttpResponse> {
let mut sql_builder = SqlBuilder::select_from("users");
sql_builder.field("id");
sql_builder.or_where_eq("email", &quote(email.clone()));
sql_builder.or_where_eq("email", email.clone());

let rows = match query_constructor_executor(pg_pool, sql_builder).await {
Ok(x) => x,
Expand Down
12 changes: 6 additions & 6 deletions src/modules/user/user_queues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ async fn put_user_queue(pool: Pool, queue: Arc<PutUserAppQueue>) -> Result<(), H

let mut sql_builder = sql_builder::SqlBuilder::update_table("users");

sql_builder.set("password", &quote(&user.new_password));
sql_builder.set("email", &quote(&user.new_email));
sql_builder.set("updated_at", &quote(&updated_at));
sql_builder.or_where_eq("id", &quote(user_id));
sql_builder.set("password", &quote(user.new_password.clone()));
sql_builder.set("email", &quote(user.new_email.clone()));
sql_builder.set("updated_at", &quote(updated_at));
sql_builder.or_where_eq("id", user_id);

let mut this_sql = match sql_builder.sql() {
Ok(x) => x,
Expand Down Expand Up @@ -171,7 +171,7 @@ async fn delete_user_queue(pool: Pool, queue: Arc<DeleteUserAppQueue>) -> Result
let user_id = queue.pop().await;

let mut sql_builder = SqlBuilder::delete_from("salt");
sql_builder.or_where_eq("user_id", &quote(user_id.clone()));
sql_builder.or_where_eq("user_id", user_id.clone());

let mut this_sql = match sql_builder.sql() {
Ok(x) => x,
Expand All @@ -181,7 +181,7 @@ async fn delete_user_queue(pool: Pool, queue: Arc<DeleteUserAppQueue>) -> Result
salt_sql.push_str(&this_sql);

let mut sql_builder = SqlBuilder::delete_from("users");
sql_builder.or_where_eq("id", &quote(user_id));
sql_builder.or_where_eq("id", user_id);

let mut this_sql = match sql_builder.sql() {
Ok(x) => x,
Expand Down
7 changes: 3 additions & 4 deletions src/modules/user/user_repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use actix_web::{
web::{Data, Json, Query},
HttpResponse,
};
use sql_builder::quote;
use std::sync::Arc;

fn user_dto_constructor(rows: Vec<postgres::Row>) -> UserDTO {
Expand All @@ -36,7 +35,7 @@ pub async fn get_user_salt_repository(
) -> Result<String, HttpResponse> {
let mut sql_builder = sql_builder::SqlBuilder::select_from("salt");
sql_builder.field("salt");
sql_builder.or_where_eq("user_id", &quote(user_id));
sql_builder.or_where_eq("user_id", user_id);

let rows = match query_constructor_executor(pg_pool, sql_builder).await {
Ok(x) => x,
Expand Down Expand Up @@ -86,7 +85,7 @@ pub async fn login_user_repository(
pg_pool: Data<deadpool_postgres::Pool>,
) -> Result<UserDTO, HttpResponse> {
let mut sql_builder = sql_builder::SqlBuilder::select_from("users");
sql_builder.or_where_eq("email", &quote(email.clone()));
sql_builder.or_where_eq("email", email.clone());

let rows = match query_constructor_executor(pg_pool, sql_builder).await {
Ok(x) => x,
Expand All @@ -112,7 +111,7 @@ pub async fn detail_user_repository(
user_id: String,
) -> Result<UserDTO, HttpResponse> {
let mut sql_builder = sql_builder::SqlBuilder::select_from("users");
sql_builder.or_where_eq("id", &quote(user_id));
sql_builder.or_where_eq("id", user_id);

let rows = match query_constructor_executor(pg_pool, sql_builder).await {
Ok(x) => x,
Expand Down
13 changes: 6 additions & 7 deletions src/modules/user/user_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,17 @@ pub async fn put_user_service(
Err(e) => return Err(e),
};

let mut db_user: UserDTO;
if string_user == None {
db_user = match detail_user_repository(pg_pool.clone(), user_id.clone()).await {
let mut db_user: UserDTO = if string_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 {
db_user = match UserSerdes::serde_string_to_json(&string_user.unwrap()) {
match UserSerdes::serde_string_to_json(&string_user.unwrap()) {
Ok(user_dto) => user_dto,
Err(e) => return Err(e),
};
}
}
};

if db_user.email != body.email {
return Err(HttpResponse::Forbidden().json(error_construct(
Expand Down

0 comments on commit 84be450

Please sign in to comment.