1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-07 11:22:36 +00:00
comunicapiv3/src/controllers/search_controller.rs

42 lines
1.3 KiB
Rust
Raw Normal View History

2020-06-23 13:06:26 +00:00
//! # Search controller
//!
//! @author Pierre Hubert
2020-06-23 16:55:23 +00:00
use crate::api_data::global_search_result_api::GlobalSearchResultAPI;
2021-02-13 15:15:25 +00:00
use crate::routes::RequestResult;
2021-02-05 08:11:30 +00:00
use crate::data::base_request_handler::BaseRequestHandler;
2020-06-23 16:55:23 +00:00
use crate::data::global_search_result::GlobalSearchResult;
2020-06-23 13:06:26 +00:00
use crate::data::http_request_handler::HttpRequestHandler;
2020-06-23 16:55:23 +00:00
use crate::helpers::{groups_helper, user_helper};
2020-06-23 13:06:26 +00:00
/// 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)?;
2020-06-25 08:08:34 +00:00
let list = user_helper::search_user(&query, limit)?
.iter()
.map(|f| f.id())
.collect::<Vec<u64>>();
2020-06-23 13:06:26 +00:00
r.set_response(list)
2020-06-23 13:14:17 +00:00
}
/// Perform a global search
pub fn search_global(r: &mut HttpRequestHandler) -> RequestResult {
2020-06-23 16:55:23 +00:00
let query = r.post_string("query")?;
let limit = 10;
let mut list = user_helper::search_user(&query, limit)?
.iter()
.map(|f| GlobalSearchResult::User(f.clone()))
.collect::<Vec<GlobalSearchResult>>();
list.append(&mut groups_helper::search_group(&query, limit)?
.iter()
.map(|f| GlobalSearchResult::Group(f.clone()))
.collect::<Vec<GlobalSearchResult>>());
r.set_response(GlobalSearchResultAPI::for_list(&list))
2020-06-23 13:06:26 +00:00
}