2023-01-26 22:29:16 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
2023-01-28 16:52:32 +00:00
|
|
|
use tracing::instrument;
|
2023-01-26 22:29:16 +00:00
|
|
|
|
2023-01-28 19:22:29 +00:00
|
|
|
use crate::api::auth::RegistrationRequet;
|
2023-01-26 22:29:16 +00:00
|
|
|
use crate::database;
|
|
|
|
use crate::types::response::{Result, ResponseCode};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct User {
|
|
|
|
pub user_id: u64,
|
|
|
|
pub firstname: String,
|
|
|
|
pub lastname: String,
|
|
|
|
pub email: String,
|
|
|
|
pub password: String,
|
|
|
|
pub gender: String,
|
|
|
|
pub date: u64,
|
|
|
|
pub day: u8,
|
|
|
|
pub month: u8,
|
|
|
|
pub year: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-26 22:29:16 +00:00
|
|
|
pub fn from_user_id(user_id: u64, hide_password: bool) -> Result<Self> {
|
|
|
|
let Ok(Some(user)) = database::users::get_user_by_id(user_id, hide_password) else {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-26 22:29:16 +00:00
|
|
|
pub fn from_user_ids(user_ids: Vec<u64>) -> Vec<Self> {
|
2023-01-28 19:22:29 +00:00
|
|
|
user_ids.iter().filter_map(|user_id| {
|
2023-01-26 22:29:16 +00:00
|
|
|
let Ok(Some(user)) = database::users::get_user_by_id(*user_id, true) else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
Some(user)
|
2023-01-28 19:22:29 +00:00
|
|
|
}).collect()
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-26 22:29:16 +00:00
|
|
|
pub fn from_user_page(page: u64) -> Result<Vec<Self>> {
|
|
|
|
let Ok(users) = database::users::get_user_page(page, true) else {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("Failed to fetch users"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
Ok(users)
|
|
|
|
}
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-26 22:29:16 +00:00
|
|
|
pub fn from_email(email: &str) -> Result<Self> {
|
|
|
|
let Ok(Some(user)) = database::users::get_user_by_email(email, false) else {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-26 22:29:16 +00:00
|
|
|
pub fn from_password(password: &str) -> Result<Self> {
|
|
|
|
let Ok(Some(user)) = database::users::get_user_by_password(password, true) else {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
#[instrument()]
|
2023-01-28 19:22:29 +00:00
|
|
|
pub fn new(request: RegistrationRequet) -> Result<Self> {
|
|
|
|
if User::from_email(&request.email).is_ok() {
|
|
|
|
return Err(ResponseCode::BadRequest.text(&format!("Email is already in use by {}", &request.email)))
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 19:22:29 +00:00
|
|
|
if let Ok(user) = User::from_password(&request.password) {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text(&format!("Password is already in use by {}", user.email)))
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 19:22:29 +00:00
|
|
|
let Ok(user) = database::users::add_user(request) else {
|
2023-01-28 16:52:32 +00:00
|
|
|
tracing::error!("Failed to create new user");
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to create new uesr"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|