Skip to content

Commit 51fbfa4

Browse files
authored
Applies various fixes to API endoints (#186)
1 parent 97c6822 commit 51fbfa4

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

src/Http/Controllers/Api/ComponentController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cachet\Models\Component;
1313
use Illuminate\Http\Response;
1414
use Illuminate\Routing\Controller;
15+
use Spatie\QueryBuilder\AllowedFilter;
1516
use Spatie\QueryBuilder\QueryBuilder;
1617

1718
/**
@@ -48,7 +49,11 @@ public function index()
4849
{
4950
$components = QueryBuilder::for(Component::class)
5051
->allowedIncludes(self::ALLOWED_INCLUDES)
51-
->allowedFilters(['name', 'status', 'enabled'])
52+
->allowedFilters([
53+
'name',
54+
AllowedFilter::exact('status'),
55+
AllowedFilter::exact('enabled'),
56+
])
5257
->allowedSorts(['name', 'order', 'id'])
5358
->simplePaginate(request('per_page', 15));
5459

src/Http/Controllers/Api/IncidentController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Database\Eloquent\Builder;
1414
use Illuminate\Http\Response;
1515
use Illuminate\Routing\Controller;
16+
use Spatie\QueryBuilder\AllowedFilter;
1617
use Spatie\QueryBuilder\QueryBuilder;
1718

1819
/**
@@ -52,7 +53,11 @@ public function index()
5253

5354
$incidents = QueryBuilder::for($query)
5455
->allowedIncludes(self::ALLOWED_INCLUDES)
55-
->allowedFilters(['name', 'status', 'occurred_at'])
56+
->allowedFilters([
57+
'name',
58+
AllowedFilter::exact('status'),
59+
'occurred_at'
60+
])
5661
->allowedSorts(['name', 'status', 'id'])
5762
->simplePaginate(request('per_page', 15));
5863

src/Http/Controllers/Api/IncidentUpdateController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Cachet\Models\Update;
1414
use Illuminate\Http\Response;
1515
use Illuminate\Routing\Controller;
16+
use Spatie\QueryBuilder\AllowedFilter;
1617
use Spatie\QueryBuilder\AllowedInclude;
1718
use Spatie\QueryBuilder\QueryBuilder;
1819

@@ -42,7 +43,7 @@ public function index(Incident $incident)
4243
->where('updateable_type', 'incident');
4344

4445
$updates = QueryBuilder::for($query)
45-
->allowedFilters(['status'])
46+
->allowedFilters([AllowedFilter::exact('status'),])
4647
->allowedIncludes(['incident'])
4748
->allowedSorts(['status', 'created_at'])
4849
->simplePaginate(request('per_page', 15));

src/Http/Controllers/Api/ScheduleController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cachet\Models\Schedule;
1313
use Illuminate\Http\Response;
1414
use Illuminate\Routing\Controller;
15+
use Spatie\QueryBuilder\AllowedFilter;
1516
use Spatie\QueryBuilder\QueryBuilder;
1617

1718
/**
@@ -32,13 +33,14 @@ class ScheduleController extends Controller
3233
* @queryParam page int Which page to show. Example: 2
3334
* @queryParam sort Field to sort by. Enum: name, id, scheduled_at, completed_at, enabled. Example: name
3435
* @queryParam include Include related resources. Enum: components, updates, user. Example: components
35-
* @queryParam filters[name] string Filter the resources. Example: name=api
36+
* @queryParam filters[name] string Filter the resources by name. Example: api
37+
* @queryParam filters[status] string Filter the resources by status. Example: 1
3638
*/
3739
public function index()
3840
{
3941
$schedules = QueryBuilder::for(Schedule::class)
4042
->allowedIncludes(['components', 'updates', 'user'])
41-
->allowedFilters(['name'])
43+
->allowedFilters(['name', AllowedFilter::exact('status'),])
4244
->allowedSorts(['name', 'id', 'scheduled_at', 'completed_at'])
4345
->simplePaginate(request('per_page', 15));
4446

src/Http/Resources/Component.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public function toAttributes(Request $request): array
1616
'description' => $this->description,
1717
'link' => $this->link,
1818
'order' => $this->order,
19-
'status' => $this->status,
19+
'status' => [
20+
'human' => $this->status?->getLabel(),
21+
'value' => $this->status?->value,
22+
],
2023
'enabled' => $this->enabled,
2124
'meta' => $this->meta,
2225
'created' => [

src/Http/Resources/Incident.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function toAttributes(Request $request): array
2222
'stickied' => $this->stickied,
2323
'notifications' => $this->notifications,
2424
'status' => [
25-
'human' => $this->latestStatus->getLabel(),
26-
'value' => $this->latestStatus->value,
25+
'human' => $this->latestStatus?->getLabel(),
26+
'value' => $this->latestStatus?->value,
2727
],
2828
'occurred' => [
2929
'human' => $this->occurred_at?->diffForHumans(),

0 commit comments

Comments
 (0)