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 5 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
44 changes: 44 additions & 0 deletions
44
domains/Discussions/Controllers/QuestionsGetAnswersController.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,44 @@ | ||
| <?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 Illuminate\Contracts\Pagination\Paginator; | ||
|
|
||
| class QuestionsGetAnswersController extends Controller | ||
| { | ||
| private Question $question; | ||
ana-lisboa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public function __construct(Auth $auth, Question $question) | ||
| { | ||
| if ($auth->guest()) { | ||
| $this->middleware('throttle:30,1'); | ||
| } | ||
|
|
||
| $this->question = $question; | ||
| } | ||
|
|
||
| public function __invoke(int $id, Request $request): Paginator | ||
ana-lisboa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $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' | ||
| ]); | ||
ana-lisboa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return $this->question | ||
| ->findOrFail($id) | ||
| ->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); | ||
| } | ||
| } | ||
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
115 changes: 115 additions & 0 deletions
115
domains/Discussions/Tests/Feature/QuestionsGetAnswersTest.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,115 @@ | ||
| <?php | ||
|
|
||
| namespace Domains\Discussions\Tests\Feature; | ||
|
|
||
| use Carbon\Carbon; | ||
| use Faker\Factory; | ||
| use Tests\TestCase; | ||
| use Faker\Generator; | ||
| use Illuminate\Http\Response; | ||
| 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 QuestionsGetAnswersTest extends TestCase | ||
| { | ||
| use DatabaseMigrations; | ||
|
|
||
| private User $user; | ||
| private User $secondUser; | ||
| private Question $question; | ||
| private Answer $answer; | ||
| private Answer $secondAnswer; | ||
| private Answer $thirdAnswer; | ||
|
|
||
| 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(); | ||
|
|
||
| $this->thirdAnswer = AnswerFactory::new([ | ||
| 'question_id' => $this->question->id, | ||
| 'author_id' => $this->user->id, | ||
| 'created_at' => Carbon::now()->subWeek()->toDateTimeString() | ||
| ])->create(); | ||
ana-lisboa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question(): void | ||
| { | ||
| $this->get(route('discussions.questions.answersList', ['id' => $this->question->id])) | ||
| ->seeJson([ | ||
| "id" => $this->answer->id, | ||
| "question_id" => '' . $this->question->id | ||
| ]) | ||
| ->seeJson([ | ||
| "id" => $this->secondAnswer->id, | ||
| "question_id" => '' . $this->question->id | ||
| ]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question_from_a_particular_author(): void | ||
| { | ||
| $this->get(route('discussions.questions.answersList', ['id' => $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', ['id' => $this->question->id]) . '?created[from]=' . $aWeekAgo->format('Y-m-d') . '&created[to]=' . $yesterday->format('Y-m-d')) | ||
| ->seeJson([ | ||
| "id" => $this->answer->id, | ||
| "question_id" => '' . $this->question->id | ||
| ]) | ||
| ->dontSeeJson([ | ||
| "id" => $this->secondAnswer->id | ||
| ]); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_paginated_answers_for_a_question_from_a_particular_time_frame_and_user(): void | ||
| { | ||
| $aWeekAgo = Carbon::now()->subDays(8); | ||
| $yesterday = Carbon::yesterday(); | ||
|
|
||
| $this->get(route('discussions.questions.answersList', ['id' => $this->question->id]) . '?created[from]=' . $aWeekAgo->format('Y-m-d') . '&created[to]=' . $yesterday->format('Y-m-d') . '&author=1') | ||
| ->seeJson([ | ||
| "id" => $this->answer->id, | ||
| "question_id" => '' . $this->question->id | ||
| ]) | ||
| ->seeJson([ | ||
| "id" => $this->thirdAnswer->id, | ||
| ]) | ||
| ->dontSeeJson([ | ||
| "id" => $this->secondAnswer->id | ||
| ]); | ||
| } | ||
| } | ||
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.