1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-23 22:09:22 +00:00
comunicapiv3/src/controllers/user_controller.rs

46 lines
1.5 KiB
Rust
Raw Normal View History

2020-05-25 11:25:51 +00:00
//! # User controller
//!
//! This controller handles all the routes related about getting user information
//!
//! @author Pierre Hubert
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;
2020-05-25 11:25:51 +00:00
/// Get information about a single user
pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult {
let user_id = request.post_i64("userID")?;
let user = match user_helper::find_user_by_id(user_id) {
Ok(user) => user,
Err(e) => {
println!("Error while getting user info: {}", e);
request.not_found("Could not get user information!".to_string())?;
unreachable!();
}
};
2020-05-26 16:45:27 +00:00
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)
2020-05-25 11:25:51 +00:00
}