1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-10 01:22:48 +00:00

Administrators can search for users

This commit is contained in:
2021-07-12 17:18:06 +02:00
parent cb44497fee
commit 82717312e6
7 changed files with 97 additions and 4 deletions

View File

@ -38,8 +38,6 @@ pub fn get_all_users() -> Res<Vec<User>> {
/// Execute query & return result
fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
// Page status
let page_status = if res.get_int64("pageouverte")? == 1 {
UserPageStatus::OPEN
@ -196,4 +194,36 @@ pub fn search_user(query: &str, limit: u64) -> ResultBoxError<Vec<UserID>> {
.add_custom_where_argument_str(&query)
.set_limit(limit)
.exec(|row| row.get_user_id("ID"))
}
/// Search for user in the database / admin version
pub fn search_user_admin(name: &str, email: &str, limit: u64) -> ResultBoxError<Vec<User>> {
let name_query = format!("%{}%", name.replace(" ", "%"));
let mut custom_where = String::new();
let mut query = database::QueryInfo::new(USERS_TABLE);
if !name.is_empty() {
custom_where.push_str("(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)");
query = query
.add_custom_where_argument_str(&name_query)
.add_custom_where_argument_str(&name_query)
.add_custom_where_argument_str(&name_query)
.add_custom_where_argument_str(&name_query);
}
if !email.is_empty() {
if !custom_where.is_empty() {
custom_where.push_str(" OR ");
}
custom_where.push_str("(mail LIKE ?)");
query = query.add_custom_where_argument_str(&format!("%{}%", email))
}
query
.set_custom_where(&custom_where)
.set_limit(limit)
.exec(db_to_user)
}