From 2a9f0ed1a074ec7a82b59176fd9104e307e45e39 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 4 Dec 2017 20:18:30 +0100 Subject: [PATCH] Created remove a friend method --- RestControllers/friendsController.php | 23 +++++++++++++++++++++++ classes/DBLibrary.php | 2 +- classes/components/friends.php | 24 ++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index 14a353d..8b2d674 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -56,4 +56,27 @@ class friendsController{ return array("success" => "A response was given to friendship request !"); } + /** + * Delete a friend from the list + * + * @url POST /friends/remove + */ + public function delete(){ + user_login_required(); //Login required + + //Check input parametres + if(!isset($_POST['friendID'])) + Rest_fatal_error(400, "Please specify the ID of the friend to delete !"); + + //Delete the friend from the list + $friendID = toInt($_POST['friendID']); + $result = CS::get()->components->friends->remove(userID, $friendID); + + //Check if the operation is a result + if(!$result) + Rest_fatal_error(500, "Couldn't remove user from the friendlist for an unexcepted reason !"); + else + return array("success" => "The friend was removed from the list !"); + } + } \ No newline at end of file diff --git a/classes/DBLibrary.php b/classes/DBLibrary.php index 0f7c54a..95fc6c9 100755 --- a/classes/DBLibrary.php +++ b/classes/DBLibrary.php @@ -487,7 +487,7 @@ class DBLibrary { * @param Array $conditionsValues The values of condition * @return Boolean True if succeed */ - public function deleteEntry($tableName, $conditions = false, array $conditionsValues = array()) { + public function deleteEntry(string $tableName, $conditions = false, array $conditionsValues = array()) : bool { //We try to perform the task try{ //We check if any database is opened diff --git a/classes/components/friends.php b/classes/components/friends.php index 80ddcd4..554900c 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -124,7 +124,7 @@ class friends { * @param Integer $friendID The destination of the request * @return Boolean True or false depending of the success of the operation */ - public function checkFriendShipRequestExistence($userID, $friendID){ + public function checkFriendShipRequestExistence($userID, $friendID){ //Perform a request on the database $conditions = "WHERE ID_personne = ? AND ID_amis = ? AND actif = 0"; $dataConditions = array( @@ -143,7 +143,27 @@ class friends { //Else we check the results else return count($results) === 1; - } + } + + /** + * Remove a friend from the firends list + * + * @param int $userID The ID of the user who delete the friend + * @param int $friendID The ID of the friend that is being removed from the list + * @return bool True if the user was successfully removed / false else + */ + public function remove(int $userID, int $friendID) : bool { + + //Delete the friend from the database + $tableName = $this->friendsTable; + $conditions = "(ID_personne = ? AND ID_amis = ?) OR (ID_personne = ? AND ID_amis = ?)"; + $condValues = array($userID, $friendID, $friendID, $userID); + + //Try to perform the request + $success = CS::get()->db->deleteEntry($tableName, $conditions, $condValues); + + return $success; + } } //Register component