mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 21:39:21 +00:00
Start to return user information
This commit is contained in:
parent
97d9adcc03
commit
be4d1befcc
@ -10,3 +10,4 @@
|
|||||||
pub mod http_error;
|
pub mod http_error;
|
||||||
pub mod login_success;
|
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
24
src/api_data/user_info.rs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,3 +3,4 @@ pub mod server;
|
|||||||
|
|
||||||
pub mod server_controller;
|
pub mod server_controller;
|
||||||
pub mod account_controller;
|
pub mod account_controller;
|
||||||
|
pub mod user_controller;
|
@ -1,7 +1,7 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use crate::controllers::routes::Method::{GET, POST};
|
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;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
|
|
||||||
/// Project routes
|
/// Project routes
|
||||||
@ -77,5 +77,9 @@ pub fn get_routes() -> Vec<Route> {
|
|||||||
|
|
||||||
Route::post("/account/id", Box::new(account_controller::user_id)),
|
Route::post("/account/id", Box::new(account_controller::user_id)),
|
||||||
Route::post("/user/getCurrentUserID", 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)),
|
||||||
]
|
]
|
||||||
}
|
}
|
25
src/controllers/user_controller.rs
Normal file
25
src/controllers/user_controller.rs
Normal 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))
|
||||||
|
}
|
@ -118,6 +118,13 @@ impl HttpRequestHandler {
|
|||||||
Err(Box::new(ExecError::new(&message)))
|
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
|
/// 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> {
|
pub fn ok_or_bad_request<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
|
||||||
match res {
|
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
|
/// Get user ID. This function assess that a user ID is available to continue
|
||||||
pub fn user_id(&self) -> ResultBoxError<UserID> {
|
pub fn user_id(&self) -> ResultBoxError<UserID> {
|
||||||
match self.curr_user_id {
|
match self.curr_user_id {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::data::error::ResultBoxError;
|
use crate::data::error::ResultBoxError;
|
||||||
use crate::data::user::User;
|
use crate::data::user::{User, UserID};
|
||||||
use crate::helpers::database;
|
use crate::helpers::database;
|
||||||
use crate::database_structure::USERS_TABLE;
|
use crate::database_structure::USERS_TABLE;
|
||||||
|
|
||||||
@ -7,6 +7,12 @@ use crate::database_structure::USERS_TABLE;
|
|||||||
///
|
///
|
||||||
/// @author Pierre Hubert
|
/// @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
|
/// Get & return information about a user based on his email
|
||||||
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
|
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
|
||||||
exec_get_user_query(
|
exec_get_user_query(
|
||||||
@ -21,7 +27,7 @@ fn exec_get_user_query(query : database::QueryInfo) -> ResultBoxError<User> {
|
|||||||
email: res.get_str("mail")?,
|
email: res.get_str("mail")?,
|
||||||
password: res.get_str("password")?,
|
password: res.get_str("password")?,
|
||||||
first_name: res.get_str("prenom")?,
|
first_name: res.get_str("prenom")?,
|
||||||
last_name: res.get_str("nom")?
|
last_name: res.get_str("nom")?,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user