Skip to content

Commit 965f5cd

Browse files
committed
wip
1 parent 0890079 commit 965f5cd

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"laravel/sanctum": "^4.0",
3535
"nesbot/carbon": "^2.70|^3.0",
3636
"spatie/laravel-data": "^4.11",
37-
"spatie/laravel-query-builder": "^6.3.1",
37+
"spatie/laravel-query-builder": "^6.3",
3838
"spatie/laravel-settings": "^3.2",
3939
"spatie/laravel-webhook-server": "^3.8",
4040
"timacdonald/json-api": "^1.0.0-beta.4",
@@ -46,10 +46,10 @@
4646
"laravel/pail": "^1.1",
4747
"laravel/pint": "^1.21",
4848
"orchestra/testbench": "^9.5.1|^10.0",
49-
"pestphp/pest": "^3.2",
50-
"pestphp/pest-plugin-laravel": "^3.0",
51-
"pestphp/pest-plugin-livewire": "*",
52-
"pestphp/pest-plugin-type-coverage": "^3.3"
49+
"pestphp/pest": "^3.8",
50+
"pestphp/pest-plugin-laravel": "^3.7",
51+
"pestphp/pest-plugin-livewire": "^v3.0",
52+
"pestphp/pest-plugin-type-coverage": "^3.5"
5353
},
5454
"minimum-stability": "dev",
5555
"prefer-stable": true,

src/Http/Controllers/Api/IncidentController.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Dedoc\Scramble\Attributes\Group;
1414
use Dedoc\Scramble\Attributes\QueryParameter;
1515
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Http\Request;
1617
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
1718
use Illuminate\Http\Response;
1819
use Illuminate\Pagination\Paginator;
@@ -41,22 +42,24 @@ class IncidentController extends Controller
4142
*/
4243
#[QueryParameter('per_page', 'How many items to show per page.', type: 'int', default: 15, example: 20)]
4344
#[QueryParameter('page', 'Which page to show.', type: 'int', example: 2)]
44-
public function index()
45+
public function index(Request $request)
4546
{
46-
$query = Incident::query()
47-
->when(! request('sort'), function (Builder $builder) {
48-
$builder->orderByDesc('created_at');
49-
});
47+
// $query = Incident::query()
48+
// ->when(!$request->has('sort'), function (Builder $builder) {
49+
// $builder->orderByDesc('created_at');
50+
// });
5051

51-
$incidents = QueryBuilder::for($query)
52+
$incidents = QueryBuilder::for(Incident::query())
5253
->allowedIncludes(self::ALLOWED_INCLUDES)
5354
->allowedFilters([
5455
'name',
5556
AllowedFilter::exact('status'),
56-
'occurred_at',
57+
AllowedFilter::scope('occurs_after'),
58+
AllowedFilter::scope('occurs_before'),
59+
AllowedFilter::scope('occurs_on'),
5760
])
5861
->allowedSorts(['name', 'status', 'id'])
59-
->simplePaginate(request('per_page', 15));
62+
->simplePaginate($request->input('per_page', 15));
6063

6164
return IncidentResource::collection($incidents);
6265
}

src/Models/Incident.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ public function scopeStickied(Builder $query): void
171171
$query->where('stickied', true);
172172
}
173173

174+
public function scopeOccursAfter(Builder $query, $date): void
175+
{
176+
$query->where('occurred_at', '>=', $date);
177+
}
178+
179+
public function scopeOccursBefore(Builder $query, $date): void
180+
{
181+
$query->where('occurred_at', '<=', $date);
182+
}
183+
184+
public function scopeOccursOn(Builder $query, $date): void
185+
{
186+
$query->whereDate('occurred_at', $date);
187+
}
188+
174189
/**
175190
* @return Attribute<Carbon, never>
176191
*/

tests/Feature/Api/IncidentTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function Pest\Laravel\getJson;
1111
use function Pest\Laravel\postJson;
1212
use function Pest\Laravel\putJson;
13+
use function Pest\Laravel\withoutExceptionHandling;
1314

1415
it('can list incidents', function () {
1516
Incident::factory(2)->create();
@@ -144,19 +145,26 @@
144145

145146
it('can filter incidents by occurred at date', function () {
146147
Incident::factory(20)->create([
147-
'occurred_at' => '2019-01-01',
148+
'occurred_at' => '2019-01-01 00:00:00',
148149
]);
149150
$incident = Incident::factory()->create([
150-
'occurred_at' => '2023-01-01',
151+
'occurred_at' => '2025-01-01 00:00:00',
151152
]);
152153

153-
$query = http_build_query([
154+
withoutExceptionHandling();
155+
156+
dd(route('cachet.api.incidents.index', [
154157
'filter' => [
155-
'occurred_at' => '2023-01-01',
158+
'occurs_after' => '2024-12-31',
156159
],
157-
]);
160+
]));
158161

159-
$response = getJson('/status/api/incidents?'.$query);
162+
$response = getJson(route('cachet.api.incidents.index', [
163+
'filter' => [
164+
'occurs_after' => '2024-12-31',
165+
],
166+
]))
167+
->assertOk();
160168

161169
$response->assertJsonCount(1, 'data');
162170
$response->assertJsonPath('data.0.attributes.id', $incident->id);

0 commit comments

Comments
 (0)