Skip to content

Commit

Permalink
Improve incident API
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk committed Dec 2, 2024
1 parent 10e916f commit 33102cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
*/
class IncidentController extends Controller
{
/**
* The list of allowed includes.
*/
public const ALLOWED_INCLUDES = [
'components',
'incidentUpdates',
'user',
];

/**
* List Incidents
*
Expand All @@ -29,15 +38,15 @@ class IncidentController extends Controller
* @queryParam per_page int How many items to show per page. Example: 20
* @queryParam page int Which page to show. Example: 2
* @queryParam sort string Field to sort by. Enum: name, id, status Example: status
* @queryParam include string Include related resources. Enum: updates Example: updates
* @queryParam include string Include related resources. Enum: components, incidentUpdates, user Example: incidentUpdates
*/
public function index()
{
$incidents = QueryBuilder::for(Incident::class)
->when(! request('sort'), function (Builder $builder) {
$builder->orderByDesc('created_at');
})
->allowedIncludes(['updates'])
->allowedIncludes(self::ALLOWED_INCLUDES)
->allowedFilters(['name', 'status', 'occurred_at'])
->allowedSorts(['name', 'status', 'id'])
->simplePaginate(request('per_page', 15));
Expand Down Expand Up @@ -67,10 +76,16 @@ public function store(CreateIncidentRequest $request, CreateIncident $createInci
* @apiResource \Cachet\Http\Resources\Incident
*
* @apiResourceModel \Cachet\Models\Incident
*
* @queryParam include string Include related resources. Enum: components, incidentUpdates, user Example: incidentUpdates
*/
public function show(Incident $incident)
{
return IncidentResource::make($incident)
$incidentQuery = QueryBuilder::for($incident)
->allowedIncludes(self::ALLOWED_INCLUDES)
->first();

return IncidentResource::make($incidentQuery)
->response()
->setStatusCode(Response::HTTP_OK);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Api/IncidentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
it('can get an incident with updates', function () {
$incident = Incident::factory()->hasIncidentUpdates(2)->create();

$response = getJson('/status/api/incidents/'.$incident->id.'?include=inidcent_updates');
$response = getJson('/status/api/incidents/'.$incident->id.'?include=incidentUpdates');

$response->assertOk();
$response->assertJsonFragment([
Expand Down

0 comments on commit 33102cd

Please sign in to comment.