This repository was archived by the owner on Jun 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Resolves #32 - A guest or an authenticated user can list answers of a question #53
Merged
josepostiga
merged 12 commits into
laravel-portugal:master
from
ana-lisboa:FEAT/#32/GetAnswersFromAQuestion
Nov 22, 2020
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
760a92e
Merge remote-tracking branch 'upstream/master'
ana-lisboa 6a629cc
#32 gets answers from a question
ana-lisboa c4ca3cb
Merge branch 'master' into FEAT/#32/GetAnswersFromAQuestion
ana-lisboa 95e010f
#32 fix repeated route name
ana-lisboa 7f9ffb8
Merge branch 'FEAT/#32/GetAnswersFromAQuestion' of https://github.com…
ana-lisboa db7d17f
#32 code review improvements
ana-lisboa 3b63fd2
#32 adds second round of code review
ana-lisboa bf69829
#32 adds minor code reviews
ana-lisboa 6fa1863
#32 adds final code review
ana-lisboa 43dd4e8
#32 removes unneeded index
ana-lisboa 282c217
#32 edits changelog
ana-lisboa fd4a6fc
Merge branch 'master' into FEAT/#32/GetAnswersFromAQuestion
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
domains/Discussions/Controllers/AnswersIndexController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace Domains\Discussions\Controllers; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use App\Http\Controllers\Controller; | ||
| use Domains\Discussions\Models\Question; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Contracts\Auth\Factory as Auth; | ||
| use Domains\Discussions\Resources\AnswerResource; | ||
| use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
|
|
||
| class AnswersIndexController extends Controller | ||
| { | ||
| private Question $question; | ||
|
|
||
| public function __construct(Auth $auth, Question $question) | ||
| { | ||
| if ($auth->guest()) { | ||
| $this->middleware('throttle:30,1'); | ||
| } | ||
|
|
||
| $this->question = $question; | ||
| } | ||
|
|
||
| public function __invoke(Request $request, int $questionId): AnonymousResourceCollection | ||
| { | ||
| $this->validate($request, [ | ||
| 'author' => ['sometimes', 'integer', 'exists:users,id'], | ||
| 'created' => ['sometimes', 'array', 'size:2'], | ||
| 'created.from' => ['required_with:created', 'date'], | ||
| 'created.to' => ['required_with:created', 'date', 'afterOrEqual:created.from'] | ||
| ]); | ||
|
|
||
| $answers = $this->question | ||
| ->findOrFail($questionId) | ||
| ->answers() | ||
| ->when($authorId = $request->input('author'), | ||
| static fn(Builder $answers, int $authorId) => $answers->whereAuthorId($authorId)) | ||
| ->when($created = $request->input('created'), | ||
| static fn(Builder $answers, array $created) => $answers->whereBetween('created_at', [$created['from'], $created['to']])) | ||
| ->latest() | ||
| ->simplePaginate(15); | ||
|
|
||
| return AnswerResource::collection($answers); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace Domains\Discussions\Resources; | ||
|
|
||
| use Domains\Accounts\Resources\UserResource; | ||
| use Illuminate\Http\Resources\Json\JsonResource; | ||
|
|
||
| class AnswerResource extends JsonResource | ||
| { | ||
| public function toArray($request): array | ||
| { | ||
| return [ | ||
| 'id' => $this->id, | ||
| 'content' => $this->content, | ||
| 'question' => QuestionResource::make($this->question), | ||
| 'author' => UserResource::make($this->author), | ||
josepostiga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'created_at' => $this->created_at, | ||
| 'updated_at' => $this->updated_at, | ||
| 'deleted_at' => $this->deleted_at, | ||
| ]; | ||
| } | ||
| } | ||
174 changes: 174 additions & 0 deletions
174
domains/Discussions/Tests/Feature/AnswersIndexControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| <?php | ||
|
|
||
| namespace Domains\Discussions\Tests\Feature; | ||
|
|
||
| use Carbon\Carbon; | ||
| use Illuminate\Http\Response; | ||
| use Tests\TestCase; | ||
| use Domains\Accounts\Models\User; | ||
| use Domains\Discussions\Models\Answer; | ||
| use Domains\Discussions\Models\Question; | ||
| use Laravel\Lumen\Testing\DatabaseMigrations; | ||
| use Domains\Accounts\Database\Factories\UserFactory; | ||
| use Domains\Discussions\Database\Factories\AnswerFactory; | ||
| use Domains\Discussions\Database\Factories\QuestionFactory; | ||
|
|
||
| class AnswersIndexControllerTest extends TestCase | ||
| { | ||
| use DatabaseMigrations; | ||
|
|
||
| private User $user; | ||
| private User $secondUser; | ||
| private Question $question; | ||
| private Answer $answer; | ||
| private Answer $secondAnswer; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->user = UserFactory::new()->create(); | ||
| $this->secondUser = UserFactory::new()->create(); | ||
|
|
||
| $this->question = QuestionFactory::new([ | ||
| 'author_id' => $this->user->id | ||
| ])->create(); | ||
|
|
||
| $this->answer = AnswerFactory::new([ | ||
| 'question_id' => $this->question->id, | ||
| 'author_id' => $this->user->id, | ||
| 'created_at' => Carbon::now()->subWeek()->toDateTimeString() | ||
| ])->create(); | ||
|
|
||
| $this->secondAnswer = AnswerFactory::new([ | ||
| 'question_id' => $this->question->id, | ||
| 'author_id' => $this->secondUser->id, | ||
| 'created_at' => Carbon::now()->toDateTimeString() | ||
| ])->create(); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question(): void | ||
| { | ||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id])) | ||
| ->seeJsonStructure([ | ||
| "data" => [ | ||
| 0 => [ | ||
josepostiga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "id", | ||
| "content", | ||
| "question" => [ | ||
| "id", | ||
| "title", | ||
| "slug", | ||
| "description", | ||
| "author" => [ | ||
| "id", | ||
| "name", | ||
| "email", | ||
| "trusted", | ||
| "created_at", | ||
| "updated_at", | ||
| "deleted_at", | ||
| ], | ||
| "created_at", | ||
| "updated_at", | ||
| "resolved_at", | ||
| "deleted_at", | ||
| ], | ||
| "author" => [ | ||
| "id", | ||
| "name", | ||
| "email", | ||
| "trusted", | ||
| "created_at", | ||
| "updated_at", | ||
| "deleted_at", | ||
| ], | ||
| "created_at", | ||
| "updated_at", | ||
| "deleted_at", | ||
| ] | ||
| ] | ||
| ]) | ||
| // answer 1 | ||
josepostiga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ->seeJsonContains(['id' => $this->answer->id]) | ||
| ->seeJsonContains(['content' => $this->answer->content]) | ||
| // answer 2 | ||
| ->seeJsonContains(['id' => $this->secondAnswer->id]) | ||
| ->seeJsonContains(['content' => $this->secondAnswer->content]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question_from_a_particular_author(): void | ||
| { | ||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id, 'author' => $this->user->id])) | ||
| ->seeJson(['id' => $this->answer->id]) | ||
| ->dontSeeJson(['id' => $this->secondAnswer->id]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question_from_a_particular_time_frame(): void | ||
| { | ||
| $aWeekAgo = Carbon::now()->subDays(8); | ||
| $yesterday = Carbon::yesterday(); | ||
|
|
||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id]) . '?created[from]=' . $aWeekAgo->format('Y-m-d') . '&created[to]=' . $yesterday->format('Y-m-d')) | ||
| // answer 1 | ||
| ->seeJsonContains(['id' => $this->answer->id]) | ||
| ->seeJsonContains(['content' => $this->answer->content]) | ||
| // answer 2 | ||
| ->seeJsonDoesntContains([ | ||
| "id" => $this->secondAnswer->id | ||
| ]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question_from_a_particular_time_frame_and_user(): void | ||
| { | ||
| $thirdAnswer = AnswerFactory::new([ | ||
| 'question_id' => $this->question->id, | ||
| 'author_id' => $this->user->id, | ||
| 'created_at' => Carbon::now()->subWeek()->toDateTimeString() | ||
| ])->create(); | ||
|
|
||
| $aWeekAgo = Carbon::now()->subDays(8); | ||
| $yesterday = Carbon::yesterday(); | ||
|
|
||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id]) . '?created[from]=' . $aWeekAgo->format('Y-m-d') . '&created[to]=' . $yesterday->format('Y-m-d') . '&author=1') | ||
| // answer 1 | ||
| ->seeJsonContains(['id' => $this->answer->id]) | ||
| ->seeJsonContains(['content' => $this->answer->content]) | ||
| // answer 3 | ||
| ->seeJsonContains(['id' => $thirdAnswer->id]) | ||
| ->seeJsonContains(['content' => $thirdAnswer->content]) | ||
| // answer 2 | ||
| ->dontSeeJson([ | ||
| "id" => $this->secondAnswer->id | ||
| ]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_blocks_guest_for_many_attempts(): void | ||
| { | ||
| for ($attempt = 0; $attempt < 30; ++$attempt) { | ||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id])) | ||
| ->assertResponseStatus(Response::HTTP_OK); | ||
| } | ||
|
|
||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id])) | ||
| ->assertResponseStatus(Response::HTTP_TOO_MANY_REQUESTS); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_not_blocks_authenticated_user_for_many_attempts(): void | ||
| { | ||
| $this->actingAs($this->user); | ||
|
|
||
| for ($attempt = 0; $attempt < 30; ++$attempt) { | ||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id])); | ||
| } | ||
|
|
||
| $this->get(route('discussions.questions.answersList', ['questionId' => $this->question->id])) | ||
| ->assertResponseStatus(Response::HTTP_OK); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.