2020-05-23 17:17:48 +00:00
|
|
|
use crate::data::error::ResultBoxError;
|
2020-05-26 11:15:39 +00:00
|
|
|
use crate::data::user::{User, UserID, UserPageStatus};
|
2020-05-23 17:17:48 +00:00
|
|
|
use crate::helpers::database;
|
|
|
|
use crate::database_structure::USERS_TABLE;
|
|
|
|
|
|
|
|
/// User helper
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
2020-05-25 11:25:51 +00:00
|
|
|
/// Get & return information about a user based on its ID
|
|
|
|
pub fn find_user_by_id(id: UserID) -> ResultBoxError<User> {
|
|
|
|
exec_get_user_query(
|
|
|
|
database::QueryInfo::new(USERS_TABLE).cond_i64("ID", id))
|
|
|
|
}
|
|
|
|
|
2020-05-23 17:17:48 +00:00
|
|
|
/// Get & return information about a user based on his email
|
|
|
|
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
|
|
|
|
exec_get_user_query(
|
|
|
|
database::QueryInfo::new(USERS_TABLE).cond("mail", email))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute query & return result
|
2020-05-25 11:25:51 +00:00
|
|
|
fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
|
|
|
|
database::query_row(query, |res| {
|
2020-05-26 11:15:39 +00:00
|
|
|
|
|
|
|
// Page status
|
|
|
|
let page_status = if res.get_int64("pageouverte")? == 1 {
|
|
|
|
UserPageStatus::OPEN
|
|
|
|
} else if res.get_int64("public")? == 1 {
|
|
|
|
UserPageStatus::PUBLIC
|
|
|
|
} else {
|
|
|
|
UserPageStatus::PRIVATE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-05-23 17:17:48 +00:00
|
|
|
Ok(User {
|
|
|
|
id: res.get_int64("ID")?,
|
|
|
|
email: res.get_str("mail")?,
|
|
|
|
password: res.get_str("password")?,
|
|
|
|
first_name: res.get_str("prenom")?,
|
2020-05-25 11:25:51 +00:00
|
|
|
last_name: res.get_str("nom")?,
|
2020-05-26 11:15:39 +00:00
|
|
|
status: page_status,
|
2020-05-23 17:17:48 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|