-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathIncidentUpdateController.php
More file actions
117 lines (99 loc) · 3.45 KB
/
IncidentUpdateController.php
File metadata and controls
117 lines (99 loc) · 3.45 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
115
116
117
<?php
namespace Cachet\Http\Controllers\Api;
use Cachet\Actions\Incident\SetLatestStatusIncident;
use Cachet\Actions\Update\CreateUpdate;
use Cachet\Actions\Update\DeleteUpdate;
use Cachet\Actions\Update\EditUpdate;
use Cachet\Concerns\GuardsApiAbilities;
use Cachet\Data\Requests\IncidentUpdate\CreateIncidentUpdateRequestData;
use Cachet\Data\Requests\IncidentUpdate\EditIncidentUpdateRequestData;
use Cachet\Http\Resources\Update as UpdateResource;
use Cachet\Models\Incident;
use Cachet\Models\Update;
use Dedoc\Scramble\Attributes\Group;
use Dedoc\Scramble\Attributes\QueryParameter;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
use Illuminate\Pagination\Paginator;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedInclude;
use Spatie\QueryBuilder\QueryBuilder;
#[Group('Incident Updates', weight: 4)]
class IncidentUpdateController extends Controller
{
use GuardsApiAbilities;
/**
* List Incident Updates
*
* @response AnonymousResourceCollection<Paginator<UpdateResource>>
*/
#[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(Incident $incident)
{
$query = Update::query()
->where('updateable_id', $incident->id)
->where('updateable_type', 'incident');
$updates = QueryBuilder::for($query)
->allowedFilters([AllowedFilter::exact('status')])
->allowedIncludes(['incident'])
->allowedSorts(['status', 'created_at'])
->simplePaginate(request('per_page', 15));
return UpdateResource::collection($updates);
}
/**
* Create Incident Update
*/
public function store(CreateIncidentUpdateRequestData $data, Incident $incident, CreateUpdate $createUpdateAction)
{
$this->guard('incident-updates.manage');
$update = $createUpdateAction->handle($incident, $data);
return UpdateResource::make($update);
}
/**
* Get Incident Update
*/
public function show(Incident $incident, Update $update)
{
$updateQuery = QueryBuilder::for(Update::class)
->allowedIncludes([
AllowedInclude::relationship('incident', 'updateable'),
])
->find($update->id);
return UpdateResource::make($updateQuery)
->response()
->setStatusCode(Response::HTTP_OK);
}
/**
* Update Incident Update
*/
public function update(
EditIncidentUpdateRequestData $data,
Incident $incident,
Update $update,
EditUpdate $editUpdateAction,
SetLatestStatusIncident $latestStatusIncident,
)
{
$this->guard('incident-updates.manage');
$editUpdateAction->handle($update, $data);
$latestStatusIncident->handle($incident);
return UpdateResource::make($update->fresh());
}
/**
* Delete Incident Update
*/
public function destroy(
Incident $incident,
Update $update,
DeleteUpdate $deleteUpdateAction,
SetLatestStatusIncident $latestStatusIncident,
)
{
$this->guard('incident-updates.delete');
$deleteUpdateAction->handle($update);
$latestStatusIncident->handle($incident);
return response()->noContent();
}
}