diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 8c164cc..bdc00d1 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -34,8 +34,9 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult { /// Get information about a single group pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult { let group_id = r.post_group_id_with_access("id", GroupAccessLevel::LIMITED_ACCESS)?; + let group = groups_helper::get_info(&group_id)?; - println!("Group to get: {:?}", group_id); + println!("Group info: {:#?}", group); r.success("continue implementation") } \ No newline at end of file diff --git a/src/data/group.rs b/src/data/group.rs index 7b90108..9bd991c 100644 --- a/src/data/group.rs +++ b/src/data/group.rs @@ -2,14 +2,34 @@ //! //! Group visibility level +use crate::data::group_id::GroupID; + #[allow(non_camel_case_types)] -#[derive(Eq, PartialEq, Hash)] +#[derive(Eq, PartialEq, Hash, Debug)] pub enum GroupVisibilityLevel { OPEN_GROUP, PRIVATE_GROUP, SECRETE_GROUP, } +#[allow(non_camel_case_types)] +#[derive(Eq, PartialEq, Hash, Debug)] +pub enum GroupRegistrationLevel { + OPEN_REGISTRATION = 0, + MODERATED_REGISTRATION = 1, + CLOSED_REGISTRATION = 2, +} + +#[allow(non_camel_case_types)] +#[derive(Eq, PartialEq, Hash, Debug)] +pub enum GroupPostsCreationLevel { + //Only the moderators and the administrator can create posts + POSTS_LEVEL_MODERATORS = 0, + + //All the members of the group can create posts + POSTS_LEVEL_ALL_MEMBERS = 1, +} + #[allow(non_camel_case_types)] #[derive(Eq, PartialEq, Hash, Debug, PartialOrd)] pub enum GroupAccessLevel { @@ -32,6 +52,22 @@ pub enum GroupAccessLevel { ADMIN_ACCESS = 5, } +/// Group information +#[derive(Debug)] +pub struct Group { + pub id: GroupID, + pub name: String, + pub members_count: u64, + pub visibility: GroupVisibilityLevel, + pub registration_level: GroupRegistrationLevel, + pub posts_creation_level: GroupPostsCreationLevel, + pub logo: Option, + pub virtual_directory: Option, + pub time_create: u64, + pub description: Option, + pub url: Option, +} + #[cfg(test)] mod tests { use crate::data::group::GroupAccessLevel; diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 7ecd218..5d68a3a 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -4,7 +4,7 @@ use crate::constants::database_tables_names::{GROUPS_LIST_TABLE, GROUPS_MEMBERS_TABLE}; use crate::data::error::{ExecError, ResultBoxError}; -use crate::data::group::{GroupAccessLevel, GroupVisibilityLevel}; +use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel}; use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::new_group::NewGroup; @@ -56,6 +56,42 @@ impl GroupMembershipLevel { } } +impl GroupRegistrationLevel { + pub fn to_db(&self) -> u32 { + match self { + GroupRegistrationLevel::OPEN_REGISTRATION => 0, + GroupRegistrationLevel::MODERATED_REGISTRATION => 1, + GroupRegistrationLevel::CLOSED_REGISTRATION => 2, + } + } + + pub fn from_db(level: u32) -> GroupRegistrationLevel { + match level { + 0 => GroupRegistrationLevel::OPEN_REGISTRATION, + 1 => GroupRegistrationLevel::MODERATED_REGISTRATION, + 2 => GroupRegistrationLevel::CLOSED_REGISTRATION, + _ => GroupRegistrationLevel::CLOSED_REGISTRATION, + } + } +} + +impl GroupPostsCreationLevel { + pub fn to_db(&self) -> u32 { + match self { + GroupPostsCreationLevel::POSTS_LEVEL_MODERATORS => 0, + GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS => 1, + } + } + + pub fn from_db(level: u32) -> GroupPostsCreationLevel { + match level { + 0 => GroupPostsCreationLevel::POSTS_LEVEL_MODERATORS, + 1 => GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS, + _ => GroupPostsCreationLevel::POSTS_LEVEL_ALL_MEMBERS, + } + } +} + /// Create a new group. Returns the ID of the new group pub fn create(group: &NewGroup) -> ResultBoxError { // First, create the group @@ -102,6 +138,13 @@ pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError ResultBoxError { + database::QueryInfo::new(GROUPS_LIST_TABLE) + .cond_group_id("id", group_id) + .query_row(db_to_group) +} + /// Check out whether a group exists or not pub fn exists(group_id: &GroupID) -> ResultBoxError { database::QueryInfo::new(GROUPS_LIST_TABLE) @@ -193,4 +236,30 @@ pub fn get_access_level(group_id: &GroupID, user_id: Option) -> ResultBo // Else the user can not see the group // Especially in the case of secrete group Ok(GroupAccessLevel::NO_ACCESS) +} + +/// Count the number of members of a group +pub fn count_members(group_id: &GroupID) -> ResultBoxError { + database::QueryInfo::new(GROUPS_MEMBERS_TABLE) + .cond_group_id("groups_id", group_id) + .exec_count() +} + +/// Turn a database entry into a group object +fn db_to_group(row: &database::RowResult) -> ResultBoxError { + let group_id = row.get_group_id("id")?; + + Ok(Group { + id: group_id.clone(), + name: row.get_str("name")?, + members_count: count_members(&group_id)? as u64, + visibility: GroupVisibilityLevel::from_db(row.get_u32("visibility")?), + registration_level: GroupRegistrationLevel::from_db(row.get_u32("registration_level")?), + posts_creation_level: GroupPostsCreationLevel::from_db(row.get_u32("posts_level")?), + logo: row.get_optional_str("path_logo")?, + virtual_directory: row.get_optional_str("virtual_directory")?, + time_create: row.get_u64("time_create")?, + description: row.get_optional_str("description")?, + url: row.get_optional_str("url")?, + }) } \ No newline at end of file