From d7608ec97476f2ca1ce800a86889050393466a1e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 27 Jun 2020 07:23:38 +0200 Subject: [PATCH] Can get the list of members of a group --- src/api_data/group_member_api.rs | 32 ++++++++++++++++++++++++++++ src/api_data/mod.rs | 3 ++- src/controllers/groups_controller.rs | 7 +++++- src/helpers/groups_helper.rs | 7 ++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/api_data/group_member_api.rs diff --git a/src/api_data/group_member_api.rs b/src/api_data/group_member_api.rs new file mode 100644 index 0000000..4ac7045 --- /dev/null +++ b/src/api_data/group_member_api.rs @@ -0,0 +1,32 @@ +//! # Group member API entry +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::group_member::GroupMember; + +#[derive(Serialize)] +pub struct GroupMemberAPI { + user_id: u64, + group_id: u64, + time_create: u64, + level: String, +} + +impl GroupMemberAPI { + /// Turn a `GroupMember` object into an API entry + pub fn new(g: &GroupMember) -> GroupMemberAPI { + GroupMemberAPI { + user_id: g.user_id.id(), + group_id: g.group_id.id(), + time_create: g.time_create, + level: g.level.to_api(), + } + } + + /// Turn a list of GroupMember object into API entries + pub fn for_list(l: &Vec) -> Vec { + l.iter().map(Self::new).collect() + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 76e34df..a393c89 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -26,4 +26,5 @@ pub mod global_search_result_api; pub mod res_create_group; pub mod group_api; pub mod advanced_group_api; -pub mod res_change_group_logo; \ No newline at end of file +pub mod res_change_group_logo; +pub mod group_member_api; \ No newline at end of file diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 7c3e2eb..e1050d0 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use crate::api_data::advanced_group_api::AdvancedGroupApi; use crate::api_data::group_api::GroupApi; +use crate::api_data::group_member_api::GroupMemberAPI; use crate::api_data::res_change_group_logo::ResChangeGroupLogo; use crate::api_data::res_create_group::GroupCreationResult; use crate::constants::{DEFAULT_GROUP_LOGO, PATH_GROUPS_LOGOS}; @@ -149,5 +150,9 @@ pub fn delete_logo(r: &mut HttpRequestHandler) -> RequestResult { /// Get the list of members of a group pub fn get_members(r: &mut HttpRequestHandler) -> RequestResult { - r.success("implement me") + let group_id = r.post_group_id_with_access("id", GroupAccessLevel::MODERATOR_ACCESS)?; + + let members = groups_helper::get_list_members(&group_id)?; + + r.set_response(GroupMemberAPI::for_list(&members)) } \ No newline at end of file diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 4cdaf97..659782b 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -324,6 +324,13 @@ pub fn delete_logo(g: &GroupID) -> ResultBoxError { set_logo_path(g, None) } +/// Get the list of memberships of a group +pub fn get_list_members(g: &GroupID) -> ResultBoxError> { + database::QueryInfo::new(GROUPS_MEMBERS_TABLE) + .cond_group_id("groups_id", g) + .exec(db_to_group_member) +} + /// Turn a database entry into a group struct fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?;