Skip to content

Commit

Permalink
Applies various fixes to API endoints (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbutcher authored Jan 16, 2025
1 parent 97c6822 commit 51fbfa4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cachet\Models\Component;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

/**
Expand Down Expand Up @@ -48,7 +49,11 @@ public function index()
{
$components = QueryBuilder::for(Component::class)
->allowedIncludes(self::ALLOWED_INCLUDES)
->allowedFilters(['name', 'status', 'enabled'])
->allowedFilters([
'name',
AllowedFilter::exact('status'),
AllowedFilter::exact('enabled'),
])
->allowedSorts(['name', 'order', 'id'])
->simplePaginate(request('per_page', 15));

Expand Down
7 changes: 6 additions & 1 deletion src/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

/**
Expand Down Expand Up @@ -52,7 +53,11 @@ public function index()

$incidents = QueryBuilder::for($query)
->allowedIncludes(self::ALLOWED_INCLUDES)
->allowedFilters(['name', 'status', 'occurred_at'])
->allowedFilters([
'name',
AllowedFilter::exact('status'),
'occurred_at'
])
->allowedSorts(['name', 'status', 'id'])
->simplePaginate(request('per_page', 15));

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/Api/IncidentUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cachet\Models\Update;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedInclude;
use Spatie\QueryBuilder\QueryBuilder;

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

$updates = QueryBuilder::for($query)
->allowedFilters(['status'])
->allowedFilters([AllowedFilter::exact('status'),])
->allowedIncludes(['incident'])
->allowedSorts(['status', 'created_at'])
->simplePaginate(request('per_page', 15));
Expand Down
6 changes: 4 additions & 2 deletions src/Http/Controllers/Api/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cachet\Models\Schedule;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

/**
Expand All @@ -32,13 +33,14 @@ class ScheduleController extends Controller
* @queryParam page int Which page to show. Example: 2
* @queryParam sort Field to sort by. Enum: name, id, scheduled_at, completed_at, enabled. Example: name
* @queryParam include Include related resources. Enum: components, updates, user. Example: components
* @queryParam filters[name] string Filter the resources. Example: name=api
* @queryParam filters[name] string Filter the resources by name. Example: api
* @queryParam filters[status] string Filter the resources by status. Example: 1
*/
public function index()
{
$schedules = QueryBuilder::for(Schedule::class)
->allowedIncludes(['components', 'updates', 'user'])
->allowedFilters(['name'])
->allowedFilters(['name', AllowedFilter::exact('status'),])
->allowedSorts(['name', 'id', 'scheduled_at', 'completed_at'])
->simplePaginate(request('per_page', 15));

Expand Down
5 changes: 4 additions & 1 deletion src/Http/Resources/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function toAttributes(Request $request): array
'description' => $this->description,
'link' => $this->link,
'order' => $this->order,
'status' => $this->status,
'status' => [
'human' => $this->status?->getLabel(),
'value' => $this->status?->value,
],
'enabled' => $this->enabled,
'meta' => $this->meta,
'created' => [
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Resources/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function toAttributes(Request $request): array
'stickied' => $this->stickied,
'notifications' => $this->notifications,
'status' => [
'human' => $this->latestStatus->getLabel(),
'value' => $this->latestStatus->value,
'human' => $this->latestStatus?->getLabel(),
'value' => $this->latestStatus?->value,
],
'occurred' => [
'human' => $this->occurred_at?->diffForHumans(),
Expand Down

0 comments on commit 51fbfa4

Please sign in to comment.