diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index 49b451c..585c63c 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -40,4 +40,75 @@ class GroupsController { ); } + /** + * Get information about a group + * + * @url POST /groups/get_info + */ + public function getInfo(){ + + //Get the ID of the requested group + $id = postInt("id"); + + //Get information about the group + $group = components()->groups->get_info($id); + + //Check if the group was not found + if(!$group->isValid()) + Rest_fatal_error(404, "The requested group was not found !"); + + //Parse and return information about the group + return self::GroupInfoToAPI($group); + } + + /** + * Get advanced information about a group + * + * @url POST /groups/get_advanced_info + */ + public function getAdvancedInfo(){ + + //Get the ID of the requested group + $id = postInt("id"); + + //Get information about the group + $group = components()->groups->get_advanced_info($id); + + //Check if the group was not found + if(!$group->isValid()) + Rest_fatal_error(404, "The requested group was not found !"); + + //Parse and return information about the group + return self::AdvancedGroupInfoToAPI($group); + } + + /** + * Parse a GroupInfo object into an array for the API + * + * @param GroupInfo $info Information about the group + * @return array Generated API data + */ + public static function GroupInfoToAPI(GroupInfo $info) : array { + $data = array(); + + $data["id"] = $info->get_id(); + $data["name"] = $info->get_name(); + $data["number_members"] = $info->get_number_members(); + + return $data; + } + + /** + * Parse an AdvancedGroupInfo object into an array for the API + * + * @param AdvancedGroupInfo $info Information about the group + * @return array Generated API data + */ + public static function AdvancedGroupInfoToAPI(AdvancedGroupInfo $info) : array { + $data = self::GroupInfoToAPI($info); + + $data["time_create"] = $info->get_time_create(); + + return $data; + } } \ No newline at end of file diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 29b057d..4f586f3 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -51,6 +51,46 @@ class GroupsComponent { return $groupID; } + /** + * Get and return information about a group + * + * @param int $id The ID of the target group + * @return GroupInfo Information about the group / invalid + * object in case of failure + */ + public function get_info(int $id) : GroupInfo { + + //Query the database + $info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id)); + + //Check for results + if(count($info) == 0) + return new GroupInfo(); //Return invalid object + + //Create and fill GroupInfo object with database entry + return $this->dbToGroupInfo($info[0]); + } + + /** + * Get and return advanced information about a group + * + * @param int $id The ID of the target group + * @return GroupInfo Information about the group / invalid + * object in case of failure + */ + public function get_advanced_info(int $id) : AdvancedGroupInfo { + + //Query the database + $info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id)); + + //Check for results + if(count($info) == 0) + return new AdvancedGroupInfo(); //Return invalid object + + //Create and fill GroupInfo object with database entry + return $this->dbToAdvancedGroupInfo($info[0]); + } + /** * Insert a new group member * @@ -66,6 +106,56 @@ class GroupsComponent { )); } + /** + * Count the number of members of a group + * + * @param int $id The ID of the target group + * @return int The number of members of the group + */ + private function countMembers(int $id) : int { + return db()->count(self::GROUPS_MEMBERS_TABLE, + "WHERE groups_id = ?", + array($id)); + } + + /** + * Turn a database entry into a GroupInfo object + * + * @param array $data Database entry + * @param GroupInfo $group The object to fill with the information (optionnal) + * @return GroupInfo Generated object + */ + private function dbToGroupInfo(array $data, GroupInfo $info = null) : GroupInfo { + + if($info == null) + $info = new GroupInfo(); + + $info->set_id($data["id"]); + $info->set_name($data["name"]); + $info->set_number_members($this->countMembers($info->get_id())); + + return $info; + + } + + /** + * Turn a database entry into AdvancedGroupInfo object entry + * + * @param array $data Database entry + * @return AdvancedGroupInfo Advanced information about the group + */ + private function dbToAdvancedGroupInfo(array $data) : AdvancedGroupInfo { + + //Parse basical information about the group + $info = new AdvancedGroupInfo(); + $this->dbToGroupInfo($data, $info); + + //Parse advanced information + $info->set_time_create($data["time_create"]); + + return $info; + + } } //Register component diff --git a/classes/models/AdvancedGroupInfo.php b/classes/models/AdvancedGroupInfo.php new file mode 100644 index 0000000..0d10f7d --- /dev/null +++ b/classes/models/AdvancedGroupInfo.php @@ -0,0 +1,29 @@ +time_create = $time_create; + } + + public function has_time_create() : bool { + return $this->time_create > -1; + } + + public function get_time_create() : int { + return $this->time_create; + } + +} \ No newline at end of file diff --git a/classes/models/GroupInfo.php b/classes/models/GroupInfo.php new file mode 100644 index 0000000..b244ced --- /dev/null +++ b/classes/models/GroupInfo.php @@ -0,0 +1,41 @@ +name = $name == "" ? null : $name; + } + + public function has_name() : bool { + return $this->name != null; + } + + public function get_name() : string { + return $this->name != null ? $this->name : "null"; + } + + //Get and set the number of members of the group + public function set_number_members(int $number_members){ + $this->number_members = $number_members; + } + + public function has_number_members() : bool { + return $this->number_members > -1; + } + + public function get_number_members() : int { + return $this->number_members; + } +} \ No newline at end of file