From 235428982d3f5b74cf15213bd80afab9a3178cf7 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 21 Dec 2017 19:24:25 +0100 Subject: [PATCH] Can create a frienship request --- RestControllers/friendsController.php | 37 +++++++++++++++++++++++++++ classes/components/friends.php | 22 ++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index c8c101e..c9a78c8 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -29,6 +29,43 @@ class friendsController{ return $friendsList; } + /** + * Send a friendship request + * + * @url POST /friends/sendRequest + */ + public function sendRequest(){ + user_login_required(); //Login required + + //Check parametres + if(!isset($_POST["friendID"])) + Rest_fatal_error(400, "Please specify a user ID !"); + + //Extract informations and process request + $friendID = toInt($_POST['friendID']); + + //Check if the two persons are already friend + if(CS::get()->components->friends->are_friend(userID, $friendID)) + Rest_fatal_error(401, "The two personns are already friend !"); + + //Check if there is already a pending request + //Check if the current user has sent a request to the other user + if(CS::get()->components->friends->sent_request(userID, $friendID)) + Rest_fatal_error(401, "You have already sent a friendship request to this personn !"); + + //Check if the current user has received a friendship request + if(CS::get()->components->friends->sent_request($friendID, userID)) + Rest_fatal_error(401, "You have already received a friendship request from this personn !"); + + //We can now create the request + if(!CS::get()->components->friends->send_request(userID, $friendID)) + Rest_fatal_error(500, "Couldn't create friendship request !"); + + //Success + return array("success" => "The friendship request has been created !"); + + } + /** * Respond to a friendship request * diff --git a/classes/components/friends.php b/classes/components/friends.php index 102f0f2..cb005b9 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -186,6 +186,28 @@ class friends { return count($response) > 0; } + /** + * Send a friendship request + * + * @param $userID The ID of the user creating the request + * @param $targetID The target of the friendship request + * @return TRUE in case of success / FALSE else + */ + public function send_request(int $userID, int $targetID) : bool { + + //Prepare the insertion + $tableName = $this->friendsTable; + $values = array( + "ID_personne" => $targetID, + "ID_amis" => $userID, + "actif" => 0 + ); + + //Try to perform the request + return CS::get()->db->addLine($tableName, $values) == true; + + } + /** * Check wether a user has sent a friendship * request to another friend