mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 13:29:21 +00:00
Can search users
This commit is contained in:
parent
abf9a7fa82
commit
6fd5d01550
@ -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;
|
@ -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> {
|
||||
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)),
|
||||
|
||||
|
17
src/controllers/search_controller.rs
Normal file
17
src/controllers/search_controller.rs
Normal 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)
|
||||
}
|
@ -340,6 +340,15 @@ impl HttpRequestHandler {
|
||||
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> {
|
||||
Ok(self.post_string(name)?.parse::<u64>()?)
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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"))
|
||||
}
|
Loading…
Reference in New Issue
Block a user