1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00

Can search users

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

View File

@ -5,4 +5,5 @@ pub mod server_controller;
pub mod account_controller; pub mod account_controller;
pub mod user_controller; pub mod user_controller;
pub mod conversations_controller; pub mod conversations_controller;
pub mod search_controller;
pub mod virtual_directory_controller; pub mod virtual_directory_controller;

View File

@ -1,6 +1,6 @@
use std::error::Error; use std::error::Error;
use crate::controllers::{account_controller, conversations_controller, server_controller, user_controller, virtual_directory_controller}; use crate::controllers::{account_controller, conversations_controller, search_controller, server_controller, user_controller, virtual_directory_controller};
use crate::controllers::routes::Method::{GET, POST}; use crate::controllers::routes::Method::{GET, POST};
use crate::data::http_request_handler::HttpRequestHandler; use crate::data::http_request_handler::HttpRequestHandler;
@ -119,6 +119,12 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/conversations/deleteMessage", Box::new(conversations_controller::delete_message)), Route::post("/conversations/deleteMessage", Box::new(conversations_controller::delete_message)),
// Search controller
Route::post("/search/user", Box::new(search_controller::search_user)),
Route::post("/user/search", Box::new(search_controller::search_user)),
// Virtual directory controller // Virtual directory controller
Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),

View File

@ -0,0 +1,17 @@
//! # Search controller
//!
//! @author Pierre Hubert
use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::user_helper;
/// Search for user
pub fn search_user(r: &mut HttpRequestHandler) -> RequestResult {
let query = r.post_string_opt("query", 1, true)?;
let limit = r.post_u64_opt("searchLimit", 5)?;
let list = user_helper::search_user(&query, limit)?;
r.set_response(list)
}

View File

@ -340,6 +340,15 @@ impl HttpRequestHandler {
Ok(self.post_string(name)?.parse::<i64>()?) Ok(self.post_string(name)?.parse::<i64>()?)
} }
/// Get an optional number in the request. If none found, return a default value
pub fn post_u64_opt(&mut self, name: &str, default: u64) -> ResultBoxError<u64> {
if self.has_post_parameter(name) {
Ok(self.post_string(name)?.parse::<u64>()?)
} else {
Ok(default)
}
}
pub fn post_u64(&mut self, name: &str) -> ResultBoxError<u64> { pub fn post_u64(&mut self, name: &str) -> ResultBoxError<u64> {
Ok(self.post_string(name)?.parse::<u64>()?) Ok(self.post_string(name)?.parse::<u64>()?)
} }

View File

@ -164,6 +164,12 @@ impl QueryInfo {
self 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 /// Append a field to the list of selected fields
pub fn add_field(mut self, key: &str) -> QueryInfo { pub fn add_field(mut self, key: &str) -> QueryInfo {
self.fields.push(key.to_string()); 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() { if !info.conditions.is_empty() {
let mut where_args = vec![]; let mut where_args = vec![];
for (k, v) in info.conditions { for (k, v) in &info.conditions {
where_args.push(format!("{} = ?", k)); where_args.push(format!("{} = ?", k));
params.push(mysql::Value::from(v)); 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 // Custom WHERE clause
if let Some(custom_where) = info.custom_where { 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; let mut custom_args = info.custom_where_ars;
params.append(&mut custom_args); 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::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::data::user::UserPageStatus::PUBLIC;
use crate::helpers::{database, friends_helper};
use crate::helpers::friends_helper::are_friend; use crate::helpers::friends_helper::are_friend;
/// User helper /// User helper
@ -63,7 +63,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
public_note: res.get_optional_str("public_note")?, public_note: res.get_optional_str("public_note")?,
block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?, block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?,
allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?, 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; let visibility = find_user_by_id(target_user)?.status;
// Open page = OK // Open page = OK
if visibility == UserPageStatus::OPEN { if visibility == UserPageStatus::OPEN {
return Ok(true); return Ok(true);
} }
@ -141,3 +141,18 @@ pub fn can_create_posts(user_id: UserID, target_id: UserID) -> ResultBoxError<bo
Ok(true) 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"))
}