-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathIncidentController.php
More file actions
114 lines (95 loc) · 3.27 KB
/
IncidentController.php
File metadata and controls
114 lines (95 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Cachet\Http\Controllers\Api;
use Cachet\Actions\Incident\CreateIncident;
use Cachet\Actions\Incident\DeleteIncident;
use Cachet\Actions\Incident\UpdateIncident;
use Cachet\Concerns\GuardsApiAbilities;
use Cachet\Data\Requests\Incident\CreateIncidentRequestData;
use Cachet\Data\Requests\Incident\UpdateIncidentRequestData;
use Cachet\Http\Resources\Incident as IncidentResource;
use Cachet\Models\Incident;
use Dedoc\Scramble\Attributes\Group;
use Dedoc\Scramble\Attributes\QueryParameter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
#[Group('Incidents', weight: 3)]
class IncidentController extends Controller
{
use GuardsApiAbilities;
/**
* The list of allowed includes.
*/
public const ALLOWED_INCLUDES = [
'components',
'updates',
'user',
];
/**
* List Incidents
*/
#[QueryParameter('per_page', 'How many items to show per page.', type: 'int', default: 15, example: 20)]
#[QueryParameter('page', 'Which page to show.', type: 'int', example: 2)]
public function index(Request $request)
{
$query = Incident::query()
->when(!$request->has('sort'), function (Builder $builder) {
$builder->orderByDesc('created_at');
});
$incidents = QueryBuilder::for(Incident::query())
->allowedIncludes(self::ALLOWED_INCLUDES)
->allowedFilters([
'name',
AllowedFilter::exact('status'),
AllowedFilter::scope('occurs_after'),
AllowedFilter::scope('occurs_before'),
AllowedFilter::scope('occurs_on'),
])
->allowedSorts(['name', 'status', 'id'])
->simplePaginate($request->input('per_page', 15));
return IncidentResource::collection($incidents);
}
/**
* Create Incident
*/
public function store(CreateIncidentRequestData $data, CreateIncident $createIncidentAction)
{
$this->guard('incidents.manage');
$incident = $createIncidentAction->handle($data);
return IncidentResource::make($incident);
}
/**
* Get Incident
*/
public function show(Incident $incident)
{
$incidentQuery = QueryBuilder::for(Incident::class)
->allowedIncludes(self::ALLOWED_INCLUDES)
->find($incident->id);
return IncidentResource::make($incidentQuery)
->response()
->setStatusCode(Response::HTTP_OK);
}
/**
* Update Incident
*/
public function update(UpdateIncidentRequestData $data, Incident $incident, UpdateIncident $updateIncidentAction)
{
$this->guard('incidents.manage');
$updateIncidentAction->handle($incident, $data);
return IncidentResource::make($incident->fresh());
}
/**
* Delete Incident
*/
public function destroy(Incident $incident, DeleteIncident $deleteIncidentAction)
{
$this->guard('incidents.delete');
$deleteIncidentAction->handle($incident);
return response()->noContent();
}
}