cargo fmt
This commit is contained in:
+30
-12
@@ -35,7 +35,11 @@ pub mod request {
|
|||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
pub fn is_valid(&self) -> bool {
|
pub fn is_valid(&self) -> bool {
|
||||||
!self.user_id.is_nil() || !self.username.is_empty() || !self.current_password.is_empty() || !self.updated_password.is_empty() || !self.confirmed_password.is_empty()
|
!self.user_id.is_nil()
|
||||||
|
|| !self.username.is_empty()
|
||||||
|
|| !self.current_password.is_empty()
|
||||||
|
|| !self.updated_password.is_empty()
|
||||||
|
|| !self.confirmed_password.is_empty()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -320,18 +324,30 @@ pub mod endpoint {
|
|||||||
|
|
||||||
match repo::user::get(&pool, &payload.username).await {
|
match repo::user::get(&pool, &payload.username).await {
|
||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
if hashing::verify_password(&payload.current_password, user.password.clone()).unwrap() {
|
if hashing::verify_password(&payload.current_password, user.password.clone())
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
match repo::salt::get(&pool, &user.salt_id).await {
|
match repo::salt::get(&pool, &user.salt_id).await {
|
||||||
Ok(salt) => {
|
Ok(salt) => {
|
||||||
let updated_salt_string = hashing::generate_salt().unwrap();
|
let updated_salt_string = hashing::generate_salt().unwrap();
|
||||||
let mut updated_salt = simodels::user::salt::Salt::default();
|
let mut updated_salt = simodels::user::salt::Salt::default();
|
||||||
updated_salt.salt = updated_salt_string.to_string();
|
updated_salt.salt = updated_salt_string.to_string();
|
||||||
updated_salt.id = salt.id.clone();
|
updated_salt.id = salt.id.clone();
|
||||||
let updated_hashed_password = hashing::hash_password(&payload.updated_password, &updated_salt_string).unwrap();
|
let updated_hashed_password = hashing::hash_password(
|
||||||
|
&payload.updated_password,
|
||||||
|
&updated_salt_string,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
match repo::salt::update_salt(&pool, &salt, &updated_salt.salt).await {
|
match repo::salt::update_salt(&pool, &salt, &updated_salt.salt).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
match repo::user::update_password(&pool, &user, &updated_hashed_password).await {
|
match repo::user::update_password(
|
||||||
|
&pool,
|
||||||
|
&user,
|
||||||
|
&updated_hashed_password,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
response.message = "Successful".to_string();
|
response.message = "Successful".to_string();
|
||||||
response.data.push(user.id);
|
response.data.push(user.id);
|
||||||
@@ -339,13 +355,10 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(
|
||||||
}
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
}
|
axum::Json(response),
|
||||||
}
|
)
|
||||||
Err(err) => {
|
|
||||||
response.message = err.to_string();
|
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -354,7 +367,12 @@ pub mod endpoint {
|
|||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = err.to_string();
|
||||||
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
response.message = "Invalid".to_string();
|
response.message = "Invalid".to_string();
|
||||||
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
||||||
|
|||||||
+19
-13
@@ -44,25 +44,30 @@ pub mod user {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_password(pool: &sqlx::PgPool, user: &simodels::user::User, password: &str) -> Result<bool, sqlx::Error> {
|
pub async fn update_password(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
user: &simodels::user::User,
|
||||||
|
password: &str,
|
||||||
|
) -> Result<bool, sqlx::Error> {
|
||||||
match sqlx::query(
|
match sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
UPDATE "user" SET password = $1 WHERE id = $2
|
UPDATE "user" SET password = $1 WHERE id = $2
|
||||||
"#
|
"#,
|
||||||
)
|
)
|
||||||
.bind(password)
|
.bind(password)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
// .fetch_optional(pool)
|
// .fetch_optional(pool)
|
||||||
.await {
|
.await
|
||||||
|
{
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
return if r.rows_affected() > 0 {
|
return if r.rows_affected() > 0 {
|
||||||
Ok(true)
|
Ok(true)
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Err(err) => Err(err)
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +224,11 @@ pub mod salt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_salt(pool: &sqlx::PgPool, salt: &simodels::user::salt::Salt, updated_salt: &str) -> Result<bool, sqlx::Error> {
|
pub async fn update_salt(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
salt: &simodels::user::salt::Salt,
|
||||||
|
updated_salt: &str,
|
||||||
|
) -> Result<bool, sqlx::Error> {
|
||||||
match sqlx::query(
|
match sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
UPDATE "salt" SET salt = $1 WHERE id = $2
|
UPDATE "salt" SET salt = $1 WHERE id = $2
|
||||||
@@ -228,16 +237,13 @@ pub mod salt {
|
|||||||
.bind(updated_salt)
|
.bind(updated_salt)
|
||||||
.bind(salt.id)
|
.bind(salt.id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
.await {
|
.await
|
||||||
|
{
|
||||||
Ok(row) => {
|
Ok(row) => {
|
||||||
let result = if row.rows_affected() > 0 {
|
let result = if row.rows_affected() > 0 { true } else { false };
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
},
|
}
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user