use crate::data::error::ResultBoxError; use crate::data::user::{User, UserID, UserPageStatus}; use crate::helpers::database; use crate::database_structure::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 }; 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, }) }) }