use crate::data::error::ResultBoxError; use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility}; use crate::helpers::database; use crate::constants::database_tables_names::USERS_TABLE; /// User helper /// /// @author Pierre Hubert /// Get & return information about a user based on its ID pub fn find_user_by_id(id: UserID) -> ResultBoxError { exec_get_user_query( database::QueryInfo::new(USERS_TABLE).cond_i64("ID", id)) } /// Get & return information about a user based on his email pub fn find_user_by_email(email: &str) -> ResultBoxError { exec_get_user_query( database::QueryInfo::new(USERS_TABLE).cond("mail", email)) } /// Execute query & return result fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { database::query_row(query, |res| { // 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 }; // 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!() }; Ok(User { id: res.get_int64("ID")?, email: res.get_str("mail")?, password: res.get_str("password")?, first_name: res.get_str("prenom")?, last_name: res.get_str("nom")?, status: page_status, virtual_directory: res.get_optional_str("sous_repertoire")?, account_image_path: res.get_optional_str("account_image_path")?, account_image_visibility, }) }) }