Skip to content

Commit

Permalink
Ignore friend request
Browse files Browse the repository at this point in the history
- Rename FriendRequestResponseValidator to
StoreFriendRequestResponseValidator
- Add destory -ignoring- friend request
- Add TODO refactor code into convention
- Remove extra unneeded comma
  • Loading branch information
AhmedHelalAhmed committed Mar 5, 2022
1 parent d59e49d commit 88e4823
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
26 changes: 24 additions & 2 deletions app/Http/Controllers/FriendRequestResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function toArray($request)
'links' => [
'self' => url('/users/' . $this->id)
]
];;
];
}
}
15 changes: 15 additions & 0 deletions app/Validators/DestroyFriendRequestResponseValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Validators;

class DestroyFriendRequestResponseValidator
{
public function validate(array $attributes): array
{
return validator($attributes,
[
'user_id' => 'required'
]
)->validate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Validators;

class FriendRequestResponseValidator
class StoreFriendRequestResponseValidator
{
public function validate(array $attributes): array
{
Expand Down
62 changes: 60 additions & 2 deletions tests/Feature/FriendsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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'));
}

}

0 comments on commit 88e4823

Please sign in to comment.