2020-05-25 11:25:51 +00:00
|
|
|
//! # User controller
|
|
|
|
//!
|
|
|
|
//! This controller handles all the routes related about getting user information
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-05-27 11:30:54 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::api_data::user_info::APIUserInfo;
|
2020-05-25 11:25:51 +00:00
|
|
|
use crate::controllers::routes::RequestResult;
|
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-05-27 11:20:08 +00:00
|
|
|
use crate::data::user::UserID;
|
2020-05-27 11:30:54 +00:00
|
|
|
use crate::helpers::user_helper;
|
2020-05-27 11:20:08 +00:00
|
|
|
use crate::helpers::user_helper::find_user_by_id;
|
2020-05-25 11:25:51 +00:00
|
|
|
|
|
|
|
/// Get information about a single user
|
|
|
|
pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-25 08:08:34 +00:00
|
|
|
let user_id = UserID::new(request.post_u64("userID")?);
|
|
|
|
let user = match user_helper::find_user_by_id(&user_id) {
|
2020-05-25 11:25:51 +00:00
|
|
|
Ok(user) => user,
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error while getting user info: {}", e);
|
|
|
|
request.not_found("Could not get user information!".to_string())?;
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
request.set_response(APIUserInfo::new(&request.user_id_opt(), &user)?)
|
2020-05-27 11:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get information about several users
|
|
|
|
pub fn get_multiple(request: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let user_ids = request.post_numbers_list("usersID", 1)?;
|
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
let mut map: HashMap<u64, APIUserInfo> = HashMap::new();
|
2020-05-27 11:20:08 +00:00
|
|
|
|
|
|
|
for user_id in user_ids {
|
2020-06-25 08:08:34 +00:00
|
|
|
let user_id = UserID::new(user_id);
|
2020-05-27 11:28:00 +00:00
|
|
|
let user = request.ok_or_not_found(
|
2020-06-25 08:08:34 +00:00
|
|
|
find_user_by_id(&user_id),
|
2020-05-27 11:28:00 +00:00
|
|
|
"At least one user was not found!",
|
2020-05-27 11:20:08 +00:00
|
|
|
)?;
|
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
map.insert(user_id.id(), APIUserInfo::new(&request.user_id_opt(), &user)?);
|
2020-05-27 11:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request.set_response(map)
|
2020-05-27 11:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get advanced information about a user
|
|
|
|
pub fn get_advanced_info(request: &mut HttpRequestHandler) -> RequestResult {
|
2020-05-29 16:15:24 +00:00
|
|
|
let user_id = request.post_user_id("userID")?;
|
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
if !user_helper::can_see_user_page(&request.user_id_opt().unwrap_or(UserID::new(0)), &user_id)? {
|
2020-05-29 16:26:45 +00:00
|
|
|
request.forbidden("You are not allowed to see this user page!".to_string())?;
|
|
|
|
}
|
2020-05-29 16:15:24 +00:00
|
|
|
|
2020-06-25 08:08:34 +00:00
|
|
|
let user = user_helper::find_user_by_id(&user_id)?;
|
|
|
|
request.set_response(APIUserInfo::new_advanced_info(&request.user_id_opt(), &user)?)
|
2020-05-25 11:25:51 +00:00
|
|
|
}
|