diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 336114f..9a2b282 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -5,4 +5,5 @@ pub mod server_controller; pub mod account_controller; pub mod user_controller; pub mod conversations_controller; +pub mod search_controller; pub mod virtual_directory_controller; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index a4f990d..bee43ec 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ 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::data::http_request_handler::HttpRequestHandler; @@ -119,6 +119,12 @@ pub fn get_routes() -> Vec { 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 Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/controllers/search_controller.rs b/src/controllers/search_controller.rs new file mode 100644 index 0000000..6f2f620 --- /dev/null +++ b/src/controllers/search_controller.rs @@ -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) +} \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 3f11c4a..4bc0bbb 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -340,6 +340,15 @@ impl HttpRequestHandler { Ok(self.post_string(name)?.parse::()?) } + /// 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 { + if self.has_post_parameter(name) { + Ok(self.post_string(name)?.parse::()?) + } else { + Ok(default) + } + } + pub fn post_u64(&mut self, name: &str) -> ResultBoxError { Ok(self.post_string(name)?.parse::()?) } diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 022d4d6..ee3269b 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -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 ProcessRowResult>(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 ProcessRowResult>(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); diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index d6f86b6..2761449 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -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 { 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 ResultBoxError> { + 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")) } \ No newline at end of file