From 8745f8363cd8461f4334ab413ebddf13ebfb868a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 3 Jun 2020 13:28:28 +0200 Subject: [PATCH] Can search for virtual directory --- src/api_data/mod.rs | 3 +- src/api_data/res_find_virtual_directory.rs | 29 +++++++++++++++++++ src/constants.rs | 3 ++ .../virtual_directory_controller.rs | 18 ++++++++++-- src/helpers/groups_helper.rs | 15 ++++++++++ src/helpers/mod.rs | 3 +- 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/api_data/res_find_virtual_directory.rs create mode 100644 src/helpers/groups_helper.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index fcb399b..1750098 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -12,4 +12,5 @@ pub mod login_success; pub mod current_user_id; pub mod user_info; pub mod custom_emoji; -pub mod res_find_user_by_virtual_directory; \ No newline at end of file +pub mod res_find_user_by_virtual_directory; +pub mod res_find_virtual_directory; \ No newline at end of file diff --git a/src/api_data/res_find_virtual_directory.rs b/src/api_data/res_find_virtual_directory.rs new file mode 100644 index 0000000..0e813d6 --- /dev/null +++ b/src/api_data/res_find_virtual_directory.rs @@ -0,0 +1,29 @@ +//! Find virtual directory result +//! +//! @author Pierre Hubert + +use serde::{Serialize}; +use crate::data::error::ResultBoxError; +use crate::data::user::User; + +#[derive(Serialize)] +pub struct ResultFindVirtualDirectory { + kind: String, + id: u64, +} + +impl ResultFindVirtualDirectory { + /// Construct a new instance + pub fn new(user: ResultBoxError, group: ResultBoxError) -> ResultFindVirtualDirectory { + match (user, group) { + + // User + (Ok(u), _) => ResultFindVirtualDirectory { kind: "user".to_string(), id: u.id as u64 }, + + // Group + (_, Ok(g)) => ResultFindVirtualDirectory { kind: "group".to_string(), id: g }, + + _ => unreachable!() + } + } +} \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index 079f881..5e3d93a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -23,6 +23,9 @@ pub mod database_tables_names { /// Likes table pub const LIKES_TABLE: &str = "aime"; + + /// Groups list table + pub const GROUPS_LIST_TABLE: &str = "comunic_groups"; } /// The account image to show for user who do not have any diff --git a/src/controllers/virtual_directory_controller.rs b/src/controllers/virtual_directory_controller.rs index f767467..473fa64 100644 --- a/src/controllers/virtual_directory_controller.rs +++ b/src/controllers/virtual_directory_controller.rs @@ -4,8 +4,9 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::controllers::routes::RequestResult; -use crate::helpers::user_helper; +use crate::helpers::{user_helper, groups_helper}; use crate::api_data::res_find_user_by_virtual_directory::FindUserByVirtualDirectoryAPIResult; +use crate::api_data::res_find_virtual_directory::ResultFindVirtualDirectory; /// Find a user by its virtual directory pub fn find_user(r: &mut HttpRequestHandler) -> RequestResult { @@ -21,5 +22,18 @@ pub fn find_user(r: &mut HttpRequestHandler) -> RequestResult { /// Search for user / group with a given virtual directory pub fn find(r: &mut HttpRequestHandler) -> RequestResult { - r.success("implement me") + let directory = r.post_virtual_directory("directory")?; + + let user = user_helper::find_user_by_virtual_directory(&directory); + let group = groups_helper::find_by_virtual_directory(&directory); + + if user.is_err() && group.is_err() { + println!("Find virtual directory errors:\n* User: {}\n* Group: {}", + user.unwrap_err(), group.unwrap_err()); + r.not_found("Specified user / group virtual directory not found !".to_string()) + } + + else { + r.set_response(ResultFindVirtualDirectory::new(user, group)) + } } \ No newline at end of file diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs new file mode 100644 index 0000000..c07f5a5 --- /dev/null +++ b/src/helpers/groups_helper.rs @@ -0,0 +1,15 @@ +//! # Groups helper +//! +//! @author Pierre Hubert + +use crate::data::error::ResultBoxError; +use crate::helpers::database; +use crate::constants::database_tables_names::GROUPS_LIST_TABLE; + +/// Find a group id by virtual directory +pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError { + database::QueryInfo::new(GROUPS_LIST_TABLE) + .cond("virtual_directory", dir) + .add_field("id") + .query_row(|res| res.get_u64("id")) +} \ No newline at end of file diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 9e59d49..309f2d0 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -6,4 +6,5 @@ pub mod user_helper; pub mod friends_helper; pub mod custom_emojies_helper; pub mod background_image_helper; -pub mod likes_helper; \ No newline at end of file +pub mod likes_helper; +pub mod groups_helper; \ No newline at end of file