From 250544acbde1bdfe2c23b3289b2eb4ba59201292 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 27 May 2020 13:20:08 +0200 Subject: [PATCH] Can get information about multiple users --- src/controllers/routes.rs | 3 +++ src/controllers/user_controller.rs | 21 ++++++++++++++++++++ src/data/http_request_handler.rs | 32 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index efd1bc4..5387f17 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -81,5 +81,8 @@ pub fn get_routes() -> Vec { // User controller 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/getInfoMultiple", Box::new(user_controller::get_multiple)), + Route::post_without_login("/user/getInfosMultiple", Box::new(user_controller::get_multiple)), ] } \ No newline at end of file diff --git a/src/controllers/user_controller.rs b/src/controllers/user_controller.rs index 4a5406c..72099f7 100644 --- a/src/controllers/user_controller.rs +++ b/src/controllers/user_controller.rs @@ -8,6 +8,9 @@ use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::user_helper; 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 pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult { @@ -22,4 +25,22 @@ pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult { }; 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 = 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) } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index dcf8cd2..6169306 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -137,6 +137,18 @@ impl HttpRequestHandler { } } + /// If result is not OK, return a 404 not found error + pub fn ok_or_not_found(&mut self, res: ResultBoxError, msg: &str) -> ResultBoxError { + 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 pub fn request_path(&self) -> String { self.request.path().to_string() @@ -271,4 +283,24 @@ impl HttpRequestHandler { Ok(mail) } + + /// Get a list of integers included in the request + pub fn post_numbers_list(&mut self, name: &str, min_len: usize) -> ResultBoxError> { + 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::()?); + } + + if list.len() < min_len { + self.bad_request(format!("Not enough entries in '{}'!", name))?; + } + + Ok(list) + } } \ No newline at end of file