public function acceptFriendRequest($token, $requestId) { $user = new User(); $meId = $user->getMeId($token); try { $requestData = Capsule::table('friendrequests')->select('userFrom', 'userTo')->where('id', $requestId)->first(); if ($requestData->userFrom == $meId) { $newFriendId = $requestData->userTo; } else { $newFriendId = $requestData->userFrom; } } catch (\Exception $e) { throw new \Exception('Wystąpił błąd przy pobieraniu przyjaciół'); } if ($user->isUserMyFriend($newFriendId, $token)) { throw new \Exception('Ten użytkownik jest już Twoim znajomym'); } try { Capsule::table('friendrequests')->where('id', $requestId)->where('userFrom', $meId)->orWhere('userTo', $meId)->where('accepted', 0)->update(array('accepted' => 1)); Capsule::table('friends')->insert(array('userA' => $meId, 'userB' => $newFriendId)); } catch (\Exception $e) { throw new \Exception('Wystąpił błąd przy pobieraniu przyjaciół'); } $newFriendData = $user->getUser($newFriendId, $token); return $newFriendData; }
public function getNotifications($token) { $user = new User(); $userId = $user->getMeId($token); $array = array(); try { $notifications = Capsule::table('friendrequests')->select('*')->where('userTo', $userId)->where('accepted', 0)->get(); } catch (\Exception $e) { throw new \Exception('Problem z probraniem wiadmości'); } foreach ($notifications as $notification) { if ($notification->userFrom == $userId) { $notification->firstName = $user->getUser($notification->userTo, $token)->firstName; } array_push($array, $notification); } return $array; }
}); /* zmiana hasla */ $app->post('/users/me/change-password', function () use($app) { $token = $app->request->headers("Authorization"); $json = $app->request->getBody(); $data = json_decode($json, true); $user = new User(); if ($user->changePassword($token, $data['oldPass'], $data['newPass'], $data['repPass'])) { $app->render(200, array('msg' => 'Hasło zostało zmienione')); } $app->render(401, array('msg' => 'Coś poszło nie tak')); }); $app->get('/users/:user_id', function ($user_id) use($app) { $token = $app->request->headers("Authorization"); $user = new User(); $userData = $user->getUser($user_id, $token); $app->render(200, array('msg' => $userData)); }); /* edytuj uzytkownika */ $app->put('/users/:user_id', function ($user_id) use($app) { $token = $app->request->headers("Authorization"); $user = new User(); $json = $app->request->getBody(); $data = json_decode($json, true); if ($user->updateUser($token, $user_id, $data)) { $app->render(200, array('msg' => 'Pomyślnie wyedytowano użytkownika')); } $app->render(400, array('error' => true, 'msg' => 'Wystąpił problem z edycją użytkownika')); }); /* usun uzytkownika */ $app->delete('/users/:user_id', function ($user_id) use($app) {