From 41f6e94c19394844d25d8e172267c0f967481a27 Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 29 Dec 2017 09:49:44 +0100 Subject: [PATCH] Can update user page like status --- RestControllers/likesController.php | 59 +++++++++++++++++++++++++++++ classes/components/likes.php | 42 ++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 RestControllers/likesController.php diff --git a/RestControllers/likesController.php b/RestControllers/likesController.php new file mode 100644 index 0000000..80ef304 --- /dev/null +++ b/RestControllers/likesController.php @@ -0,0 +1,59 @@ +components->user->userAllowed(userID, $id)) + Rest_fatal_error(401, "You can not access this user information !"); + + break; + + + //Default case : error + default: + Rest_fatal_error(404, "Didn't find specified component type !"); + } + + //Update like status + CS::get()->components->likes->update(userID, $like, $id, $componentType); + + //Success + return array("success" => "Like status updated."); + } + +} \ No newline at end of file diff --git a/classes/components/likes.php b/classes/components/likes.php index 4ce4d32..052bde9 100644 --- a/classes/components/likes.php +++ b/classes/components/likes.php @@ -69,6 +69,48 @@ class Likes { } + /** + * Update like status + * + * @param int $userID The ID of the user + * @param bool $like New like status + * @param int $id The ID of the component element to update + * @param string $kind The kind of component + */ + public function update(int $userID, bool $like, int $id, string $kind){ + + //Check if the request is not to like anymore + if(!$like){ + + //Delete on the database + $conditions = "ID_personne = ? AND ID_type = ? AND type = ?"; + $values = array($userID, $id, $this::KINDS_DB[$kind]); + CS::get()->db->deleteEntry($this::LIKES_TABLE, $conditions, $values); + + } + + //We have to like component + else { + + //Check if user is already liking component + if($this->is_liking($userID, $id, $kind)) + return; //Nothing to be done + + //Insert in the database + $values = array( + "ID_type" => $id, + "ID_personne" => $userID, + "Date_envoi" => mysql_date(), + "type" => $this::KINDS_DB[$kind] + ); + + //Insert in the database + CS::get()->db->addLine($this::LIKES_TABLE, $values); + + } + + } + } //Register class