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

Start to return user information

This commit is contained in:
Pierre HUBERT 2020-05-25 13:25:51 +02:00
parent 97d9adcc03
commit be4d1befcc
7 changed files with 80 additions and 7 deletions

View File

@ -9,4 +9,5 @@
pub mod http_error;
pub mod login_success;
pub mod current_user_id;
pub mod current_user_id;
pub mod user_info;

24
src/api_data/user_info.rs Normal file
View File

@ -0,0 +1,24 @@
//! API representation of user information
//!
//! @author Pierre Hubert
use serde::Serialize;
use crate::data::user::User;
#[derive(Serialize)]
#[allow(non_snake_case)]
pub struct APIUserInfo {
userID: i64,
firstName: String,
lastName: String,
}
impl APIUserInfo {
pub fn new(info: User) -> APIUserInfo {
APIUserInfo {
userID: info.id,
firstName: info.first_name,
lastName: info.last_name,
}
}
}

View File

@ -2,4 +2,5 @@ pub mod routes;
pub mod server;
pub mod server_controller;
pub mod account_controller;
pub mod account_controller;
pub mod user_controller;

View File

@ -1,7 +1,7 @@
use std::error::Error;
use crate::controllers::routes::Method::{GET, POST};
use crate::controllers::{server_controller, account_controller};
use crate::controllers::{server_controller, account_controller, user_controller};
use crate::data::http_request_handler::HttpRequestHandler;
/// Project routes
@ -77,5 +77,9 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/account/id", Box::new(account_controller::user_id)),
Route::post("/user/getCurrentUserID", Box::new(account_controller::user_id)),
// 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)),
]
}

View File

@ -0,0 +1,25 @@
//! # 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;
/// 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!();
}
};
request.set_response(APIUserInfo::new(user))
}

View File

@ -118,6 +118,13 @@ impl HttpRequestHandler {
Err(Box::new(ExecError::new(&message)))
}
/// Not found (404)
pub fn not_found(&mut self, message: String) -> RequestResult {
self.response = Some(HttpResponse::NotFound().json(
HttpError::not_found(&message)));
Err(Box::new(ExecError::new(&message)))
}
/// If result is not OK, return a bad request
pub fn ok_or_bad_request<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
match res {
@ -236,6 +243,11 @@ impl HttpRequestHandler {
}
}
/// Get an integer included in the POST request
pub fn post_i64(&mut self, name: &str) -> ResultBoxError<i64> {
Ok(self.post_string(name)?.parse::<i64>()?)
}
/// Get user ID. This function assess that a user ID is available to continue
pub fn user_id(&self) -> ResultBoxError<UserID> {
match self.curr_user_id {

View File

@ -1,5 +1,5 @@
use crate::data::error::ResultBoxError;
use crate::data::user::User;
use crate::data::user::{User, UserID};
use crate::helpers::database;
use crate::database_structure::USERS_TABLE;
@ -7,6 +7,12 @@ use crate::database_structure::USERS_TABLE;
///
/// @author Pierre Hubert
/// Get & return information about a user based on its ID
pub fn find_user_by_id(id: UserID) -> ResultBoxError<User> {
exec_get_user_query(
database::QueryInfo::new(USERS_TABLE).cond_i64("ID", id))
}
/// Get & return information about a user based on his email
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
exec_get_user_query(
@ -14,14 +20,14 @@ pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
}
/// Execute query & return result
fn exec_get_user_query(query : database::QueryInfo) -> ResultBoxError<User> {
database::query_row(query, |res|{
fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
database::query_row(query, |res| {
Ok(User {
id: res.get_int64("ID")?,
email: res.get_str("mail")?,
password: res.get_str("password")?,
first_name: res.get_str("prenom")?,
last_name: res.get_str("nom")?
last_name: res.get_str("nom")?,
})
})
}