1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Can search users

This commit is contained in:
2020-06-23 15:06:26 +02:00
parent abf9a7fa82
commit 6fd5d01550
6 changed files with 66 additions and 8 deletions

View File

@ -164,6 +164,12 @@ impl QueryInfo {
self
}
/// Add a custom string WHERE value
pub fn add_custom_where_argument_str(mut self, val: &str) -> QueryInfo {
self.custom_where_ars.push(mysql::Value::from(val));
self
}
/// Append a field to the list of selected fields
pub fn add_field(mut self, key: &str) -> QueryInfo {
self.fields.push(key.to_string());
@ -367,7 +373,7 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
if !info.conditions.is_empty() {
let mut where_args = vec![];
for (k, v) in info.conditions {
for (k, v) in &info.conditions {
where_args.push(format!("{} = ?", k));
params.push(mysql::Value::from(v));
}
@ -378,7 +384,11 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
// Custom WHERE clause
if let Some(custom_where) = info.custom_where {
query = query.add(format!(" AND ({})", custom_where).as_str());
if !info.conditions.is_empty() {
query = query.add(format!(" AND ({})", custom_where).as_str());
} else {
query = query.add(format!(" WHERE ({})", custom_where).as_str());
}
let mut custom_args = info.custom_where_ars;
params.append(&mut custom_args);

View File

@ -1,8 +1,8 @@
use crate::data::error::ResultBoxError;
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
use crate::helpers::{database, friends_helper};
use crate::constants::database_tables_names::USERS_TABLE;
use crate::data::error::ResultBoxError;
use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus};
use crate::data::user::UserPageStatus::PUBLIC;
use crate::helpers::{database, friends_helper};
use crate::helpers::friends_helper::are_friend;
/// User helper
@ -63,7 +63,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
public_note: res.get_optional_str("public_note")?,
block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?,
allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?,
account_creation_time: res.get_date_as_time("date_creation")?
account_creation_time: res.get_date_as_time("date_creation")?,
})
})
}
@ -84,7 +84,7 @@ pub fn can_see_user_page(user_id: UserID, target_user: UserID) -> ResultBoxError
let visibility = find_user_by_id(target_user)?.status;
// Open page = OK
if visibility == UserPageStatus::OPEN {
if visibility == UserPageStatus::OPEN {
return Ok(true);
}
@ -140,4 +140,19 @@ pub fn can_create_posts(user_id: UserID, target_id: UserID) -> ResultBoxError<bo
}
Ok(true)
}
/// Search for user in the database
pub fn search_user(query: &str, limit: u64) -> ResultBoxError<Vec<UserID>> {
let query = format!("%{}%", query.replace(" ", "%"));
database::QueryInfo::new(USERS_TABLE)
.add_field("ID")
.set_custom_where("(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)")
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.add_custom_where_argument_str(&query)
.set_limit(limit)
.exec(|row| row.get_user_id("ID"))
}