2024-10-07 16:48:50 -04:00
|
|
|
use std::fmt::Debug;
|
2024-10-03 15:27:30 -04:00
|
|
|
|
2024-10-07 16:48:50 -04:00
|
|
|
use sqlx::prelude::*;
|
2024-10-03 15:27:30 -04:00
|
|
|
use tracing::error;
|
2024-09-29 09:57:02 -04:00
|
|
|
|
|
|
|
use crate::models::AppError;
|
2024-08-06 11:08:15 -04:00
|
|
|
|
|
|
|
use super::DbPool;
|
|
|
|
|
2024-10-03 15:27:30 -04:00
|
|
|
#[derive(Clone)]
|
2024-08-06 11:08:15 -04:00
|
|
|
pub struct NewUserEntity {
|
|
|
|
pub email: String,
|
2024-10-31 14:37:55 -04:00
|
|
|
pub password: String,
|
2024-08-06 11:08:15 -04:00
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2024-10-03 15:27:30 -04:00
|
|
|
impl Debug for NewUserEntity {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("NewUserEntity")
|
|
|
|
.field("email", &self.email)
|
2024-10-31 14:37:55 -04:00
|
|
|
.field("password", &"********")
|
2024-10-03 15:27:30 -04:00
|
|
|
.field("name", &self.name)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 11:08:15 -04:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug, FromRow)]
|
|
|
|
pub struct UserEntity {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2024-08-22 17:29:24 -04:00
|
|
|
pub email: String,
|
2024-08-06 11:08:15 -04:00
|
|
|
pub status_id: i32,
|
|
|
|
}
|
|
|
|
|
2024-09-29 09:57:02 -04:00
|
|
|
pub async fn insert_new_user(
|
|
|
|
pool: &DbPool,
|
|
|
|
new_user: NewUserEntity,
|
|
|
|
) -> Result<UserEntity, AppError> {
|
|
|
|
let NewUserEntity {
|
|
|
|
email,
|
2024-10-31 14:37:55 -04:00
|
|
|
password,
|
2024-09-29 09:57:02 -04:00
|
|
|
name,
|
2024-10-03 15:27:30 -04:00
|
|
|
} = new_user.clone();
|
2024-08-06 11:08:15 -04:00
|
|
|
|
2024-10-31 14:37:55 -04:00
|
|
|
sqlx::query_as::<_, UserEntity>("INSERT INTO public.user (email, password, name) VALUES ($1, $2, $3) RETURNING id, email, name, status_id;")
|
2024-09-29 09:57:02 -04:00
|
|
|
.bind(email)
|
|
|
|
.bind(password)
|
|
|
|
.bind(name)
|
|
|
|
.fetch_one(pool).await
|
|
|
|
.map_err(|err| {
|
2024-10-06 07:14:44 -04:00
|
|
|
error!(?err, record = ?new_user, "Cannot insert new user record");
|
2024-10-03 15:27:30 -04:00
|
|
|
|
2024-09-29 09:57:02 -04:00
|
|
|
AppError::from(err)
|
|
|
|
})
|
|
|
|
}
|
2024-08-06 11:08:15 -04:00
|
|
|
|
2024-10-05 08:09:46 -04:00
|
|
|
#[derive(Debug, FromRow)]
|
2024-10-06 14:08:26 -04:00
|
|
|
pub struct UserIdAndHashedPasswordEntity {
|
2024-10-05 08:09:46 -04:00
|
|
|
pub id: i32,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:37:55 -04:00
|
|
|
pub async fn get_username_and_password_by_email(
|
2024-10-05 08:09:46 -04:00
|
|
|
pool: &DbPool,
|
2024-10-31 14:37:55 -04:00
|
|
|
email: String,
|
2024-10-06 14:08:26 -04:00
|
|
|
) -> Result<UserIdAndHashedPasswordEntity, AppError> {
|
|
|
|
sqlx::query_as::<_, UserIdAndHashedPasswordEntity>(
|
2024-10-31 14:37:55 -04:00
|
|
|
"SELECT id, password FROM public.user WHERE email = $1;",
|
2024-10-05 08:09:46 -04:00
|
|
|
)
|
2024-10-31 14:37:55 -04:00
|
|
|
.bind(email)
|
2024-10-05 08:09:46 -04:00
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
2024-10-06 07:14:44 -04:00
|
|
|
error!(?err, "Unable to find user");
|
2024-10-05 08:09:46 -04:00
|
|
|
AppError::from(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-29 09:57:02 -04:00
|
|
|
pub async fn verify_user(pool: &DbPool, user_id: i32) -> Result<(), AppError> {
|
|
|
|
sqlx::query("UPDATE public.user SET status_id = 1, updated_at = now() WHERE id = $1;")
|
|
|
|
.bind(user_id)
|
|
|
|
.execute(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
2024-10-06 07:14:44 -04:00
|
|
|
error!(?err, user_id, "Error verifying user");
|
2024-09-29 09:57:02 -04:00
|
|
|
AppError::from(err)
|
|
|
|
})
|
|
|
|
.map(|_| ())
|
|
|
|
}
|