1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00

Can search for virtual directory

This commit is contained in:
Pierre HUBERT 2020-06-03 13:28:28 +02:00
parent d30b1ea776
commit 8745f8363c
6 changed files with 67 additions and 4 deletions

View File

@ -13,3 +13,4 @@ pub mod current_user_id;
pub mod user_info; pub mod user_info;
pub mod custom_emoji; pub mod custom_emoji;
pub mod res_find_user_by_virtual_directory; pub mod res_find_user_by_virtual_directory;
pub mod res_find_virtual_directory;

View File

@ -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<User>, group: ResultBoxError<u64>) -> 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!()
}
}
}

View File

@ -23,6 +23,9 @@ pub mod database_tables_names {
/// Likes table /// Likes table
pub const LIKES_TABLE: &str = "aime"; 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 /// The account image to show for user who do not have any

View File

@ -4,8 +4,9 @@
use crate::data::http_request_handler::HttpRequestHandler; use crate::data::http_request_handler::HttpRequestHandler;
use crate::controllers::routes::RequestResult; 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_user_by_virtual_directory::FindUserByVirtualDirectoryAPIResult;
use crate::api_data::res_find_virtual_directory::ResultFindVirtualDirectory;
/// Find a user by its virtual directory /// Find a user by its virtual directory
pub fn find_user(r: &mut HttpRequestHandler) -> RequestResult { 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 /// Search for user / group with a given virtual directory
pub fn find(r: &mut HttpRequestHandler) -> RequestResult { 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))
}
} }

View File

@ -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<u64> {
database::QueryInfo::new(GROUPS_LIST_TABLE)
.cond("virtual_directory", dir)
.add_field("id")
.query_row(|res| res.get_u64("id"))
}

View File

@ -7,3 +7,4 @@ pub mod friends_helper;
pub mod custom_emojies_helper; pub mod custom_emojies_helper;
pub mod background_image_helper; pub mod background_image_helper;
pub mod likes_helper; pub mod likes_helper;
pub mod groups_helper;