From a934d2bf8261ddafd57aae15a09a1af18d011278 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 2 Jun 2020 13:26:12 +0200 Subject: [PATCH] Can search for user with a given virtual directory --- src/api_data/mod.rs | 3 ++- .../res_find_user_by_virtual_directory.rs | 22 +++++++++++++++++++ .../virtual_directory_controller.rs | 10 +++++++-- src/helpers/user_helper.rs | 6 +++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/api_data/res_find_user_by_virtual_directory.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 143031b..fcb399b 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -11,4 +11,5 @@ pub mod http_error; pub mod login_success; pub mod current_user_id; pub mod user_info; -pub mod custom_emoji; \ No newline at end of file +pub mod custom_emoji; +pub mod res_find_user_by_virtual_directory; \ No newline at end of file diff --git a/src/api_data/res_find_user_by_virtual_directory.rs b/src/api_data/res_find_user_by_virtual_directory.rs new file mode 100644 index 0000000..02cee19 --- /dev/null +++ b/src/api_data/res_find_user_by_virtual_directory.rs @@ -0,0 +1,22 @@ +//! API output of finding user by virtual directory +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; +use serde::Serialize; + + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct FindUserByVirtualDirectoryAPIResult { + userID: u64 +} + +impl FindUserByVirtualDirectoryAPIResult { + /// Construct a new `FindUserByVirtualDirectoryAPIResult` instance + pub fn new(user_id: UserID) -> FindUserByVirtualDirectoryAPIResult { + FindUserByVirtualDirectoryAPIResult { + userID: user_id as u64 + } + } +} \ No newline at end of file diff --git a/src/controllers/virtual_directory_controller.rs b/src/controllers/virtual_directory_controller.rs index 890919b..d5dad69 100644 --- a/src/controllers/virtual_directory_controller.rs +++ b/src/controllers/virtual_directory_controller.rs @@ -4,11 +4,17 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::controllers::routes::RequestResult; +use crate::helpers::user_helper; +use crate::api_data::res_find_user_by_virtual_directory::FindUserByVirtualDirectoryAPIResult; /// Find a user by its virtual directory pub fn find_user(r: &mut HttpRequestHandler) -> RequestResult { - let directory = r.post_virtual_directory("subfolder")?; - r.success("implement it") + let user = r.ok_or_not_found( + user_helper::find_user_by_virtual_directory(&directory), + "No user was found with this subfolder!" + )?; + + r.set_response(FindUserByVirtualDirectoryAPIResult::new(user.id)) } \ No newline at end of file diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index a9060cb..d6f86b6 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -21,6 +21,12 @@ pub fn find_user_by_email(email: &str) -> ResultBoxError { database::QueryInfo::new(USERS_TABLE).cond("mail", email)) } +/// Get & return information about a user based on his virtual directory +pub fn find_user_by_virtual_directory(dir: &str) -> ResultBoxError { + exec_get_user_query( + database::QueryInfo::new(USERS_TABLE).cond("sous_repertoire", dir)) +} + /// Execute query & return result fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { database::query_row(query, |res| {