From 30d6a1fd9d1df033fcd180c0e69293a3849a1043 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 2 Jul 2018 12:03:33 +0200 Subject: [PATCH] Can create groups --- RestControllers/GroupsController.php | 43 +++++++++++++++ classes/components/GroupsComponent.php | 72 ++++++++++++++++++++++++++ classes/models/GroupMember.php | 47 +++++++++++++++++ classes/models/NewGroup.php | 25 +++++++++ helpers/database.php | 15 ++++++ 5 files changed, 202 insertions(+) create mode 100644 RestControllers/GroupsController.php create mode 100644 classes/components/GroupsComponent.php create mode 100644 classes/models/GroupMember.php create mode 100644 classes/models/NewGroup.php create mode 100644 helpers/database.php diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php new file mode 100644 index 0000000..49b451c --- /dev/null +++ b/RestControllers/GroupsController.php @@ -0,0 +1,43 @@ +set_name($name); + $newGroup->set_userID(userID); + $newGroup->set_time_sent(time()); + + //Try to create the group + $groupID = components()->groups->create($newGroup); + + //Check for errors + if($groupID < 1) + Rest_fatal_error(500, "An error occurred while trying to create the group!"); + + //Success + return array( + "success" => "The group has been successfully created!", + "id" => $groupID + ); + } + +} \ No newline at end of file diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php new file mode 100644 index 0000000..29b057d --- /dev/null +++ b/classes/components/GroupsComponent.php @@ -0,0 +1,72 @@ +addLine(self::GROUPS_LIST_TABLE, array( + "time_create" => $newGroup->get_time_sent(), + "userid_create" => $newGroup->get_userID(), + "name" => $newGroup->get_name() + )); + + //Get the ID of the last inserted group + $groupID = db()->getLastInsertedID(); + + //Check for errors + if(!$groupID > 0) + return -1; + + //Register the user who created the group as an admin of the group + $member = new GroupMember; + $member->set_group_id($groupID); + $member->set_userID($newGroup->get_userID()); + $member->set_time_sent($newGroup->get_time_sent()); + $member->set_level(GroupMember::ADMINISTRATOR); + $this->insertMember($member); + + return $groupID; + } + + /** + * Insert a new group member + * + * @param GroupMember $member Information about the member to insert + * @return bool TRUE for a success / FALSE else + */ + private function insertMember(GroupMember $member) : bool { + return db()->addLine(self::GROUPS_MEMBERS_TABLE, array( + "groups_id" => $member->get_group_id(), + "user_id" => $member->get_userID(), + "time_create" => $member->get_time_sent(), + "level" => $member->get_level() + )); + } + +} + +//Register component +Components::register("groups", new GroupsComponent()); \ No newline at end of file diff --git a/classes/models/GroupMember.php b/classes/models/GroupMember.php new file mode 100644 index 0000000..ad57ecd --- /dev/null +++ b/classes/models/GroupMember.php @@ -0,0 +1,47 @@ +group_id = $group_id; + } + + public function has_group_id() : bool { + return $this->group_id > -1; + } + + public function get_group_id() : int { + return $this->group_id; + } + + //Set and get user membership level + public function set_level(int $level){ + $this->level = $level; + } + + public function has_level() : bool { + return $this->level > -1; + } + + public function get_level() : int { + return $this->level; + } +} \ No newline at end of file diff --git a/classes/models/NewGroup.php b/classes/models/NewGroup.php new file mode 100644 index 0000000..3cf0fee --- /dev/null +++ b/classes/models/NewGroup.php @@ -0,0 +1,25 @@ +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"; + } +} \ No newline at end of file diff --git a/helpers/database.php b/helpers/database.php new file mode 100644 index 0000000..0c63bc3 --- /dev/null +++ b/helpers/database.php @@ -0,0 +1,15 @@ +db; +} \ No newline at end of file