forked from laravel-portugal/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'FEAT/laravel-portugal#32/GetAnswersFromAQuestion' of ht…
…tps://github.com/ana-lisboa/api into FEAT/laravel-portugal#32/GetAnswersFromAQuestion
- Loading branch information
Showing
15 changed files
with
887 additions
and
737 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
29 changes: 29 additions & 0 deletions
29
domains/Discussions/Controllers/QuestionsDeleteController.php
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Domains\Discussions\Models\Question; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class QuestionsDeleteController extends Controller | ||
{ | ||
private Question $questions; | ||
|
||
public function __construct(Question $questions) | ||
{ | ||
$this->questions = $questions; | ||
} | ||
|
||
public function __invoke(Request $request, int $questionId): Response | ||
{ | ||
$question = $this->questions->findOrFail($questionId); | ||
|
||
$this->authorize('delete', $question); | ||
|
||
$question->delete(); | ||
|
||
return new Response('', Response::HTTP_NO_CONTENT); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
domains/Discussions/Controllers/QuestionsIndexController.php
This file contains 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,53 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Domains\Discussions\Models\Question; | ||
use Domains\Discussions\Resources\QuestionResource; | ||
use Illuminate\Contracts\Auth\Factory as Auth; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
|
||
class QuestionsIndexController extends Controller | ||
{ | ||
protected Question $question; | ||
|
||
public function __construct(Auth $auth, Question $question) | ||
{ | ||
if ($auth->guard()->guest()) { | ||
$this->middleware('throttle:30,1'); | ||
} | ||
|
||
$this->question = $question; | ||
} | ||
|
||
public function __invoke(Request $request): AnonymousResourceCollection | ||
{ | ||
$this->validate($request, [ | ||
'author' => ['sometimes', 'integer'], | ||
'title' => ['sometimes', 'string'], | ||
'created' => ['sometimes', 'array', 'size:2'], | ||
'created.from' => ['required_with:created', 'date'], | ||
'created.to' => ['required_with:created', 'date', 'afterOrEqual:created.from'], | ||
'resolved' => ['sometimes', 'boolean'], | ||
]); | ||
|
||
$question = $this->question | ||
->when($request->input('author'), | ||
fn(Builder $query, int $authorId) => $query->findByAuthorId($authorId)) | ||
->when($request->input('title'), | ||
fn(Builder $query, string $title) => $query->findByTitle($title)) | ||
->when($request->input('created'), | ||
fn(Builder $query, array $created) => $query->findByCreatedDate([$created['from'], $created['to']])) | ||
->when($request->boolean('resolved'), | ||
fn(Builder $query) => $query->resolved()) | ||
->when(!$request->boolean('resolved') && $request->input('resolved') != null, | ||
fn(Builder $query) => $query->nonResolved()) | ||
->latest() | ||
->simplePaginate(15); | ||
|
||
return QuestionResource::collection($question); | ||
} | ||
} |
This file contains 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 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 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 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 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 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,79 @@ | ||
<?php | ||
|
||
namespace Domains\Discussions\Tests\Feature; | ||
|
||
use Domains\Accounts\Database\Factories\UserFactory; | ||
use Domains\Accounts\Enums\AccountTypeEnum; | ||
use Domains\Accounts\Models\User; | ||
use Domains\Discussions\Database\Factories\QuestionFactory; | ||
use Domains\Discussions\Models\Question; | ||
use Faker\Factory; | ||
use Faker\Generator; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Carbon; | ||
use Laravel\Lumen\Testing\DatabaseMigrations; | ||
use Tests\TestCase; | ||
|
||
class QuestionsDeleteTest extends TestCase | ||
{ | ||
use DatabaseMigrations; | ||
|
||
private Generator $faker; | ||
private User $user; | ||
private Question $question; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->faker = Factory::create(); | ||
$this->user = UserFactory::new()->create(); | ||
$this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); | ||
|
||
Carbon::setTestNow(); | ||
} | ||
|
||
/** @test */ | ||
public function it_soft_deletes_a_question_i_own(): void | ||
{ | ||
$response = $this->actingAs($this->user) | ||
->delete(route('discussions.questions.delete', ['questionId' => $this->question->id])); | ||
|
||
$this->assertResponseStatus(Response::HTTP_NO_CONTENT); | ||
self::assertTrue($response->response->isEmpty()); | ||
$this->seeInDatabase('questions', [ | ||
'id' => $this->question->id, | ||
'updated_at' => Carbon::now(), | ||
'deleted_at' => Carbon::now(), | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function it_allows_admin_to_soft_delete_another_users_question(): void | ||
{ | ||
$response = $this->actingAs(UserFactory::new()->withRole(AccountTypeEnum::ADMIN)->make()) | ||
->delete(route('discussions.questions.update', ['questionId' => $this->question->id])); | ||
|
||
$this->assertResponseStatus(Response::HTTP_NO_CONTENT); | ||
self::assertTrue($response->response->isEmpty()); | ||
$this->seeInDatabase('questions', [ | ||
'id' => $this->question->id, | ||
'updated_at' => Carbon::now(), | ||
'deleted_at' => Carbon::now(), | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function it_forbids_a_non_admin_to_soft_delete_a_question_he_doesnt_own(): void | ||
{ | ||
$this->actingAs(UserFactory::new()->make()) | ||
->delete(route('discussions.questions.update', ['questionId' => $this->question->id])); | ||
|
||
$this->assertResponseStatus(Response::HTTP_FORBIDDEN); | ||
$this->seeInDatabase('questions', [ | ||
'id' => $this->question->id, | ||
'updated_at' => $this->question->updated_at, | ||
'deleted_at' => null, | ||
]); | ||
} | ||
} |
Oops, something went wrong.