Skip to content

Commit 6824f33

Browse files
committed
Resolved - A guest or an authenticated user can list questions (laravel-portugal#26) - Refactoring
1 parent 0079b48 commit 6824f33

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

domains/Discussions/Controllers/QuestionsGetController.php

+17-25
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
use Domains\Discussions\Models\Question;
77
use Domains\Discussions\Resources\QuestionResource;
88
use Illuminate\Contracts\Auth\Factory as Auth;
9+
use Illuminate\Database\Eloquent\Builder;
910
use Illuminate\Http\Request;
1011
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
1112

1213
class QuestionsGetController extends Controller
1314
{
14-
protected $query;
15+
protected Question $question;
1516

1617
public function __construct(Auth $auth, Question $question)
1718
{
1819
if ($auth->guard()->guest()) {
1920
$this->middleware('throttle:30,1');
2021
}
2122

22-
$this->query = $question::query();
23+
$this->question = $question;
2324
}
2425

2526
public function __invoke(Request $request): AnonymousResourceCollection
@@ -33,28 +34,19 @@ public function __invoke(Request $request): AnonymousResourceCollection
3334
'resolved' => 'sometimes|boolean'
3435
]);
3536

36-
$author = $request->get('author');
37-
if ($author != '') {
38-
$this->query->where('author_id', $author);
39-
}
40-
41-
$title = strtoupper($request->get('title'));
42-
if ($title != '') {
43-
$this->query->where('title', 'like', '%'.$title.'%');
44-
}
45-
46-
$created = $request->get('created');
47-
if ($created != null) {
48-
$this->query->whereBetween('created_at', [$created['from'], $created['to']]);
49-
}
50-
51-
$resolved = $request->get('resolved');
52-
if ($resolved == true) {
53-
$this->query->whereNotNull('resolved_at');
54-
} elseif ($resolved == false) {
55-
$this->query->whereNull('resolved_at');
56-
}
57-
58-
return QuestionResource::collection($this->query->paginate(15));
37+
$question = $this->question
38+
->when($authorId = $request->get('author'),
39+
fn(Builder $query, int $authorId) => $query->where('author_id', $authorId))
40+
->when($request->get('title'),
41+
fn(Builder $query, string $title) => $query->where('title', 'like', '%'.strtoupper($title).'%'))
42+
->when($request->get('created'),
43+
fn(Builder $query, array $created) => $query->whereBetween('created_at', [$created['from'], $created['to']]))
44+
->when($request->get('resolved'),
45+
fn(Builder $query, bool $resolved) => $query->whereNotNull('resolved_at'))
46+
->when(!$request->get('resolved'),
47+
fn(Builder $query, bool $resolved) => $query->whereNull('resolved_at'))
48+
->simplePaginate(15);
49+
50+
return QuestionResource::collection($question);
5951
}
6052
}

domains/Discussions/Tests/Feature/QuestionsGetTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public function it_supports_pagination_navigation(): void
8686
{
8787
$response = $this->get(route('discussions.questions.index', ['page' => 2]));
8888
$response->assertResponseOk();
89-
9089
self::assertEquals(2, $response->decodedJsonResponse()['meta']['current_page']);
9190
}
9291

@@ -98,7 +97,7 @@ public function it_searchable_by_author(): void
9897

9998
$response = $this->get(route('discussions.questions.index', ['author' => $user->id]));
10099

101-
$this->assertEquals(1, $response->decodedJsonResponse()['meta']['total']);
100+
$this->assertEquals(1, count($response->decodedJsonResponse()['data']));
102101
}
103102

104103
/** @test */
@@ -109,7 +108,7 @@ public function it_searchable_by_title(): void
109108

110109
$response = $this->get(route('discussions.questions.index', ['title' => 'LArAvEL-pt']));
111110

112-
$this->assertEquals(2, $response->decodedJsonResponse()['meta']['total']);
111+
$this->assertEquals(2, count($response->decodedJsonResponse()['data']));
113112
}
114113

115114
/** @test */
@@ -122,13 +121,13 @@ public function it_searchable_by_created_at(): void
122121
'created[from]' => Carbon::now()->subMonth()->subYears(2)->toDateString(),
123122
'created[to]' => Carbon::now()->addMonth()->subYears(2)->toDateString()
124123
]));
125-
$this->assertEquals(1, $response->decodedJsonResponse()['meta']['total']);
124+
$this->assertEquals(1, count($response->decodedJsonResponse()['data']));
126125

127126
$response = $this->get(route('discussions.questions.index', [
128127
'created[from]' => Carbon::now()->subMonth()->subYears(3)->toDateString(),
129128
'created[to]' => Carbon::now()->addMonth()->subYears(2)->toDateString()
130129
]));
131-
$this->assertEquals(2, $response->decodedJsonResponse()['meta']['total']);
130+
$this->assertEquals(2, count($response->decodedJsonResponse()['data']));
132131
}
133132

134133
/** @test */
@@ -137,7 +136,7 @@ public function it_searchable_by_resolved(): void
137136
QuestionFactory::new(['resolved_at' => Carbon::now()->toDateString()])->create();
138137

139138
$response = $this->get(route('discussions.questions.index', ['resolved' => true]));
140-
$this->assertEquals(1, $response->decodedJsonResponse()['meta']['total']);
139+
$this->assertEquals(1, count($response->decodedJsonResponse()['data']));
141140
}
142141

143142
/** @test */

0 commit comments

Comments
 (0)