diff --git a/app/Http/Controllers/FriendRequestResponseController.php b/app/Http/Controllers/FriendRequestResponseController.php index 345123f..40bd3e4 100644 --- a/app/Http/Controllers/FriendRequestResponseController.php +++ b/app/Http/Controllers/FriendRequestResponseController.php @@ -5,15 +5,17 @@ use App\Exceptions\FriendRequestNotFoundException; use App\Friend; use App\Http\Resources\Friend as FriendResource; -use App\Validators\FriendRequestResponseValidator; +use App\Validators\DestroyFriendRequestResponseValidator; +use App\Validators\StoreFriendRequestResponseValidator; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Http\Response; use Illuminate\Support\Arr; class FriendRequestResponseController extends Controller { public function store() { - $data = (new FriendRequestResponseValidator)->validate(request()->all()); + $data = (new StoreFriendRequestResponseValidator)->validate(request()->all()); try { $friendRequest = Friend::query() ->where('user_id', Arr::get($data, 'user_id')) @@ -32,4 +34,24 @@ public function store() return new FriendResource($friendRequest); } + + /** + * TODO refactor into convention + * @return \Illuminate\Http\JsonResponse + * @throws FriendRequestNotFoundException + */ + public function destroy() + { + $data = (new DestroyFriendRequestResponseValidator)->validate(request()->all()); + try { + Friend::query() + ->where('user_id', Arr::get($data, 'user_id')) + ->where('friend_id', auth()->id()) + ->firstOrFail() + ->delete(); + } catch (ModelNotFoundException $modelNotFoundException) { + throw new FriendRequestNotFoundException; + } + return response()->json([], Response::HTTP_NO_CONTENT); + } } diff --git a/app/Http/Resources/User.php b/app/Http/Resources/User.php index d4e84f4..0531fdb 100644 --- a/app/Http/Resources/User.php +++ b/app/Http/Resources/User.php @@ -28,6 +28,6 @@ public function toArray($request) 'links' => [ 'self' => url('/users/' . $this->id) ] - ];; + ]; } } diff --git a/app/Validators/DestroyFriendRequestResponseValidator.php b/app/Validators/DestroyFriendRequestResponseValidator.php new file mode 100644 index 0000000..659518c --- /dev/null +++ b/app/Validators/DestroyFriendRequestResponseValidator.php @@ -0,0 +1,15 @@ + 'required' + ] + )->validate(); + } +} diff --git a/app/Validators/FriendRequestResponseValidator.php b/app/Validators/StoreFriendRequestResponseValidator.php similarity index 86% rename from app/Validators/FriendRequestResponseValidator.php rename to app/Validators/StoreFriendRequestResponseValidator.php index 2d79ac4..0507490 100644 --- a/app/Validators/FriendRequestResponseValidator.php +++ b/app/Validators/StoreFriendRequestResponseValidator.php @@ -2,7 +2,7 @@ namespace App\Validators; -class FriendRequestResponseValidator +class StoreFriendRequestResponseValidator { public function validate(array $attributes): array { diff --git a/tests/Feature/FriendsTest.php b/tests/Feature/FriendsTest.php index e1ce452..350846c 100644 --- a/tests/Feature/FriendsTest.php +++ b/tests/Feature/FriendsTest.php @@ -171,7 +171,7 @@ public function a_user_id_and_status_is_required_for_friend_request_response() $this->assertArrayHasKey('status', $response->json('errors.meta')); } - /** @test */ + /** @test */ // TODO refactor into convention public function a_friendship_is_retrieved_when_fetching_the_profile() { $this->withoutExceptionHandling(); @@ -204,7 +204,7 @@ public function a_friendship_is_retrieved_when_fetching_the_profile() ]); } - /** @test */ + /** @test */ // TODO refactor into convention public function a_inverse_friendship_is_retrieved_when_fetching_the_profile() { $this->withoutExceptionHandling(); @@ -236,4 +236,62 @@ public function a_inverse_friendship_is_retrieved_when_fetching_the_profile() ] ]); } + + /** @test */ // TODO refactor into convention + public function friend_request_can_be_ignored() + { + $this->actingAs($user = factory(User::class)->create(), 'api'); + $anotherUser = factory(User::class)->create(); + $this->post('/api/friend-request', [ + 'friend_id' => $anotherUser->id + ])->assertOk(); + + $response = $this->actingAs($anotherUser, 'api') + ->delete('/api/friend-request-response/delete', [ + 'user_id' => $user->id, + ])->assertStatus(Response::HTTP_NO_CONTENT); + + $friendRequest = Friend::first(); + $this->assertNull($friendRequest); + $response->assertNoContent(); + } + + /** @test */ // TODO refactor into convention + public function only_the_recipient_can_ignore_a_friend_request() + { + $this->actingAs($user = factory(User::class)->create(), 'api'); + $anotherUser = factory(User::class)->create(); + $this->post('/api/friend-request', [ + 'friend_id' => $anotherUser->id + ])->assertOk(); + + + $response = $this->actingAs(factory(User::class)->create(), 'api') + ->delete('/api/friend-request-response/delete', [ + 'user_id' => $user->id, + ])->assertNotFound(); + + $friendRequest = Friend::first(); + $this->assertNull($friendRequest->confirmed_at); + $this->assertNull($friendRequest->status); + $response->assertJson([ + 'errors' => [ + 'code' => Response::HTTP_NOT_FOUND, + 'title' => 'Friend Request Not Found', + 'detail' => 'Unable to locate the friend request with the given information.' + ] + ]); + } + + /** @test */ // TODO refactor into convention + public function a_user_id_is_required_for_ignoring_a_friend_request_response() + { + $response = $this->actingAs(factory(User::class)->create(), 'api') + ->delete('/api/friend-request-response/delete', [ + 'user_id' => '', + ]); + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); + $this->assertArrayHasKey('user_id', $response->json('errors.meta')); + } + }