From 9bc436b2a2c183cf7d6b9ba40063e315df06bb7a Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 3 Feb 2018 15:25:58 +0100 Subject: [PATCH] Use friend model. --- RestControllers/friendsController.php | 32 +++++++++++++++++++++++++-- classes/components/friends.php | 28 +++++++++++++++++------ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index a312148..d999675 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -22,11 +22,20 @@ class friendsController{ if($friendsList === false) Rest_fatal_error(500, "Couldn't get friends list !"); + //Process the list + $api_list = array(); + foreach($friendsList as $friend){ + + //Parse friend informations + $api_list[] = $this->parseFriendAPI($friend); + + } + //Update the last activity of the user CS::get()->components->user->updateLastActivity(userID); - + //Return list - return $friendsList; + return $api_list; } /** @@ -213,4 +222,23 @@ class friendsController{ //Success return array("success" => "Friendship status has been updated!"); } + + /** + * Convert a friend object into an object readable by the api + * + * @param Friend $friend The input friend + * @return array Informations about the friend readable by the api + */ + private function parseFriendAPI(Friend $friend) : array { + + //Parse informations about the friend + $data = array( + "ID_friend" => $friend->getFriendID(), + "accepted" => $friend->isAccepted() ? 1 : 0, + "following" => $friend->isFollowing() ? 1 : 0, + "time_last_activity" => $friend->getLastActivityTime() + ); + + return $data; + } } \ No newline at end of file diff --git a/classes/components/friends.php b/classes/components/friends.php index 6f57a9a..da3f55c 100644 --- a/classes/components/friends.php +++ b/classes/components/friends.php @@ -28,7 +28,7 @@ class friends { * Get and returns the list of the friends of a user * * @param int $userID The ID of the user - * @return array The list of the friends of the user + * @return array The list of the friends of the user (Friend objects) */ public function getList(int $userID) : array { @@ -51,12 +51,7 @@ class friends { //Process results $friendsList = array(); foreach($results as $process){ - $friendsList[] = array( - "ID_friend" => $process["ID_amis"], - "accepted" => $process["actif"], - "following" => $process["abonnement"], - "time_last_activity" => $process["last_activity"] - ); + $friendsList[] = $this->parse_friend_db_infos($process); } //Return result @@ -353,6 +348,25 @@ class friends { return CS::get()->db->count($tableName, $conditions, $condValues); } + + /** + * Parse friend informations from the database + * + * @param array $data Informations about the friend from the database + * @return Friend Parsed friend object + */ + private function parse_friend_db_infos(array $data) : Friend { + + $friend = new Friend(); + + //Parse informations + $friend->setFriendID($data["ID_amis"]); + $friend->setAccepted($data["actif"] == 1); + $friend->setFollowing($data["abonnement"] == 1); + $friend->setLastActivityTime($data["last_activity"]); + + return $friend; + } } //Register component