1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can get information about multiple users

This commit is contained in:
Pierre HUBERT 2020-05-27 13:20:08 +02:00
parent c4f8e5fa97
commit 250544acbd
3 changed files with 56 additions and 0 deletions

View File

@ -81,5 +81,8 @@ pub fn get_routes() -> Vec<Route> {
// User controller // User controller
Route::post_without_login("/user/getInfo", Box::new(user_controller::get_single)), Route::post_without_login("/user/getInfo", Box::new(user_controller::get_single)),
Route::post_without_login("/user/getInfos", Box::new(user_controller::get_single)), Route::post_without_login("/user/getInfos", Box::new(user_controller::get_single)),
Route::post_without_login("/user/getInfoMultiple", Box::new(user_controller::get_multiple)),
Route::post_without_login("/user/getInfosMultiple", Box::new(user_controller::get_multiple)),
] ]
} }

View File

@ -8,6 +8,9 @@ use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler; use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::user_helper; use crate::helpers::user_helper;
use crate::api_data::user_info::APIUserInfo; use crate::api_data::user_info::APIUserInfo;
use std::collections::HashMap;
use crate::data::user::UserID;
use crate::helpers::user_helper::find_user_by_id;
/// Get information about a single user /// Get information about a single user
pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult { pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult {
@ -23,3 +26,21 @@ pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult {
request.set_response(APIUserInfo::new(request.user_id_opt(), &user)?) request.set_response(APIUserInfo::new(request.user_id_opt(), &user)?)
} }
/// Get information about several users
pub fn get_multiple(request: &mut HttpRequestHandler) -> RequestResult {
let user_ids = request.post_numbers_list("usersID", 1)?;
let mut map: HashMap<UserID, APIUserInfo> = HashMap::new();
for user_id in user_ids {
let user = request.ok_or_not_found(
find_user_by_id(user_id),
"At least one user was not found!"
)?;
map.insert(user_id, APIUserInfo::new(request.user_id_opt(), &user)?);
}
request.set_response(map)
}

View File

@ -137,6 +137,18 @@ impl HttpRequestHandler {
} }
} }
/// If result is not OK, return a 404 not found error
pub fn ok_or_not_found<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
match res {
Ok(e) => Ok(e),
Err(err) => {
println!("Error leading to 404 not found: {}", err);
self.not_found(msg.to_string())?;
unreachable!()
}
}
}
/// Get the path of the request /// Get the path of the request
pub fn request_path(&self) -> String { pub fn request_path(&self) -> String {
self.request.path().to_string() self.request.path().to_string()
@ -271,4 +283,24 @@ impl HttpRequestHandler {
Ok(mail) Ok(mail)
} }
/// Get a list of integers included in the request
pub fn post_numbers_list(&mut self, name: &str, min_len: usize) -> ResultBoxError<Vec<i64>> {
let param = self.post_string_opt(name, min_len, min_len != 0)?;
let mut list = vec![];
for split in param.split::<&str>(",") {
if split.is_empty() {
continue;
}
list.push(split.parse::<i64>()?);
}
if list.len() < min_len {
self.bad_request(format!("Not enough entries in '{}'!", name))?;
}
Ok(list)
}
} }