Skip to content

Commit 0d02a64

Browse files
committed
feat: add filters support in BaseFormRequest and update related DTOs and requests
1 parent 6f26cad commit 0d02a64

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

app/Http/Requests/BaseFormRequest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@
66

77
use Illuminate\Foundation\Http\FormRequest;
88

9-
class BaseFormRequest extends FormRequest
9+
abstract class BaseFormRequest extends FormRequest
1010
{
1111
protected function prepareForValidation()
1212
{
1313
parent::prepareForValidation();
1414
$this->route('id') && $this->merge(['id' => $this->route('id')]);
1515
$this->autoCast();
16+
$this->filtersCast();
17+
}
18+
19+
private function filtersCast()
20+
{
21+
if ($this->has('filters')) {
22+
$inputFilters = $this->input('filters');
23+
24+
$filters = $inputFilters[0];
25+
$filters = json_decode($inputFilters[0], true);
26+
27+
if (json_last_error() === JSON_ERROR_NONE) {
28+
$this->merge(['filters' => $filters]);
29+
}
30+
}
1631
}
1732

1833
protected function idRule(): array
@@ -22,6 +37,15 @@ protected function idRule(): array
2237
];
2338
}
2439

40+
protected function filtersRule(): array
41+
{
42+
return [
43+
'filters' => ['sometimes', 'array'],
44+
'filters.*.id' => ['required_with:filters', 'string'],
45+
'filters.*.value' => ['required_with:filters'],
46+
];
47+
}
48+
2549
private function autoCast(): void
2650
{
2751
$casts = $this->inferCastsFromRules();

contexts/Authorization/Application/DTOs/Role/GetRoleListDTO.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,32 @@ public function __construct(
1717

1818
public static function fromRequest(array $data): self
1919
{
20+
$merged = array_merge($data, self::convertFiltersToCriteria($data['filters'] ?? []));
21+
2022
return new self(
21-
$data['id'] ?? null,
22-
$data['label'] ?? null,
23-
$data['status'] ?? null,
24-
$data['created_at_range'] ?? null,
25-
$data['current_page'] ?? 1,
26-
$data['per_page'] ?? 10,
23+
$merged['id'] ?? null,
24+
$merged['label'] ?? null,
25+
$merged['status'] ?? null,
26+
$merged['created_at_range'] ?? null,
27+
$merged['current_page'] ?? 1,
28+
$merged['per_page'] ?? 10,
2729
);
2830
}
2931

32+
private static function convertFiltersToCriteria(array $filters): array
33+
{
34+
return collect($filters)->mapWithKeys(function ($filter) {
35+
$key = $filter['id'];
36+
if ($key === 'created_at_range') {
37+
$value = json_decode($filter['value'], true);
38+
} else {
39+
$value = $filter['value'];
40+
}
41+
42+
return [$key => $value];
43+
})->toArray();
44+
}
45+
3046
public function toCriteria(): array
3147
{
3248
return [

contexts/Authorization/Presentation/Requests/Role/GetRoleListRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function rules(): array
1818
'created_at_range.*' => ['date'],
1919
'current_page' => ['integer', 'gt:0'],
2020
'per_page' => ['integer', 'gt:0'],
21+
...$this->filtersRule(),
2122
];
2223
}
2324
}

contexts/Authorization/Tests/Feature/RoleTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
declare(strict_types=1);
4+
use Contexts\Authorization\Domain\Role\Models\RoleStatus;
5+
use Contexts\Authorization\Infrastructure\Records\RoleRecord;
46

57
it('can create active roles via api', function () {
68
$response = $this->postJson('roles', [
@@ -52,6 +54,41 @@
5254
$response->assertStatus(200);
5355
});
5456

57+
it('can search for roles', function () {
58+
RoleRecord::factory()->create([
59+
'label' => 'My Role',
60+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
61+
]);
62+
RoleRecord::factory()->create([
63+
'label' => 'Other Role',
64+
'status' => RoleRecord::mapStatusToRecord(RoleStatus::active()),
65+
]);
66+
67+
$response = $this->get('roles?label=My');
68+
69+
$response->assertStatus(200);
70+
$response->assertJsonCount(1, 'data');
71+
$response->assertJson([
72+
'data' => [
73+
[
74+
'label' => 'My Role',
75+
],
76+
],
77+
]);
78+
79+
$response = $this->get('roles?filters=[{"id":"label","value":"Other"}]');
80+
$response->assertStatus(200);
81+
$response->assertJsonCount(1, 'data');
82+
$response->assertJson([
83+
'data' => [
84+
[
85+
'label' => 'Other Role',
86+
],
87+
],
88+
]);
89+
90+
});
91+
5592
it('can update a category', function () {
5693
$response = $this->postJson('roles', [
5794
'label' => 'My Role',

0 commit comments

Comments
 (0)