From cf2d9606d947570984f09e57667d7b569f97efb4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 29 May 2020 18:15:24 +0200 Subject: [PATCH] Can get a user id included in a POST request --- src/controllers/user_controller.rs | 3 +++ src/data/http_request_handler.rs | 17 ++++++++++++++++- src/helpers/database.rs | 5 +++++ src/helpers/user_helper.rs | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/controllers/user_controller.rs b/src/controllers/user_controller.rs index 86b6701..0c8589a 100644 --- a/src/controllers/user_controller.rs +++ b/src/controllers/user_controller.rs @@ -48,5 +48,8 @@ pub fn get_multiple(request: &mut HttpRequestHandler) -> RequestResult { /// Get advanced information about a user pub fn get_advanced_info(request: &mut HttpRequestHandler) -> RequestResult { + let user_id = request.post_user_id("userID")?; + + request.success("get user info") } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 6169306..063c75c 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -12,7 +12,7 @@ use crate::data::api_client::APIClient; use crate::data::config::conf; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper}; +use crate::helpers::{account_helper, api_helper, user_helper}; /// Http request handler /// @@ -303,4 +303,19 @@ impl HttpRequestHandler { Ok(list) } + + /// Get the ID of a user included in a POST request + pub fn post_user_id(&mut self, name: &str) -> ResultBoxError { + let user_id = self.post_i64(name)?; + + if user_id < 1 { + self.bad_request(format!("Invalid user specified in '{}'!", name))?; + } + + if !user_helper::exists(user_id)? { + self.not_found(format!("User with ID {} not found!", user_id))?; + } + + Ok(user_id) + } } \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 1066b8a..99dba3a 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -107,6 +107,11 @@ impl QueryInfo { -> Result, Box> { query(self, process_function) } + + /// Execute count query + pub fn exec_count(self) -> ResultBoxError { + count(self) + } } /// Struct used to read the result of a request diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 7ece49d..cbf2687 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -52,4 +52,11 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { account_image_visibility, }) }) +} + +/// Check out whether a given id maps to a user or not +pub fn exists(id: UserID) -> ResultBoxError { + Ok(database::QueryInfo::new(USERS_TABLE) + .cond_i64("ID", id) + .exec_count()? > 0) } \ No newline at end of file