diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index ef959e4..8d95291 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -9,4 +9,5 @@ pub mod http_error; pub mod login_success; -pub mod current_user_id; \ No newline at end of file +pub mod current_user_id; +pub mod user_info; \ No newline at end of file diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs new file mode 100644 index 0000000..cebb350 --- /dev/null +++ b/src/api_data/user_info.rs @@ -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, + } + } +} \ No newline at end of file diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index d3d326b..232d0b0 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -2,4 +2,5 @@ pub mod routes; pub mod server; pub mod server_controller; -pub mod account_controller; \ No newline at end of file +pub mod account_controller; +pub mod user_controller; \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index fbd45af..efd1bc4 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -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::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)), ] } \ No newline at end of file diff --git a/src/controllers/user_controller.rs b/src/controllers/user_controller.rs new file mode 100644 index 0000000..93cae7d --- /dev/null +++ b/src/controllers/user_controller.rs @@ -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)) +} \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 0d857a2..3b90d58 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -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(&mut self, res: ResultBoxError, msg: &str) -> ResultBoxError { 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 { + Ok(self.post_string(name)?.parse::()?) + } + /// Get user ID. This function assess that a user ID is available to continue pub fn user_id(&self) -> ResultBoxError { match self.curr_user_id { diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index bbc0c71..a0ff4ad 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -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 { + 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 { exec_get_user_query( @@ -14,14 +20,14 @@ pub fn find_user_by_email(email: &str) -> ResultBoxError { } /// Execute query & return result -fn exec_get_user_query(query : database::QueryInfo) -> ResultBoxError { - database::query_row(query, |res|{ +fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { + 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")?, }) }) } \ No newline at end of file