2020-05-23 17:17:48 +00:00
|
|
|
use crate::data::error::ResultBoxError;
|
2020-05-26 11:53:24 +00:00
|
|
|
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
|
2020-05-23 17:17:48 +00:00
|
|
|
use crate::helpers::database;
|
2020-05-26 15:51:11 +00:00
|
|
|
use crate::constants::database_tables_names::USERS_TABLE;
|
2020-05-23 17:17:48 +00:00
|
|
|
|
|
|
|
/// 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-26 11:53:24 +00:00
|
|
|
// Account image visibility
|
|
|
|
let account_image_visibility = match res.get_str("account_image_visibility")?.as_ref() {
|
|
|
|
"friends" => AccountImageVisibility::FRIENDS,
|
|
|
|
"comunic_users" => AccountImageVisibility::COMUNIC_USERS,
|
|
|
|
"everyone" => AccountImageVisibility::EVERYONE,
|
|
|
|
_ => unreachable!()
|
|
|
|
};
|
2020-05-26 11:15:39 +00:00
|
|
|
|
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-26 11:18:38 +00:00
|
|
|
virtual_directory: res.get_optional_str("sous_repertoire")?,
|
2020-05-26 11:53:24 +00:00
|
|
|
account_image_path: res.get_optional_str("account_image_path")?,
|
|
|
|
account_image_visibility,
|
2020-05-23 17:17:48 +00:00
|
|
|
})
|
|
|
|
})
|
2020-05-29 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check out whether a given id maps to a user or not
|
|
|
|
pub fn exists(id: UserID) -> ResultBoxError<bool> {
|
|
|
|
Ok(database::QueryInfo::new(USERS_TABLE)
|
|
|
|
.cond_i64("ID", id)
|
|
|
|
.exec_count()? > 0)
|
2020-05-23 17:17:48 +00:00
|
|
|
}
|