DoorsController.php 11.3 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Doors\GetDoor\GetDoorUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase;
11
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase;
12
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
13
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
14
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
15
use Source\UseCases\Doors\GetDoor\APIPresenter as GetDoorAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
16
use Source\UseCases\Doors\GetDoors\APIPresenter as GetAllDoorsAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
17
18
19
use Source\UseCases\Doors\CreateDoor\APIPresenter as CreateDoorAPIPresenter;
use Source\UseCases\Doors\DeleteDoor\APIPresenter as DeleteDoorAPIPresenter;
use Source\UseCases\Doors\UpdateDoor\APIPresenter as UpdateDoorAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
20
21
22
23
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase;
use Source\UseCases\DoorGroup\GetDoorGroups\APIPresenter as GetDoorGroupsAPIPresenter;
use Source\UseCases\DoorGroup\AddDoorToGroup\APIPresenter as AddDoorToGroupAPIPresenter;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\APIPresenter as RemoveDoorFromGroupAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
24

25
26
27
28
29
30
/**
 * @group Door Management
 *
 * This set of endpoints deals with the management of doors in the system. These route require the
 *        manage doors permission.
 */
Jacob Priddy's avatar
Jacob Priddy committed
31
32
33
class DoorsController extends ApiController
{
    /**
34
35
36
37
38
39
40
     * List/Search Doors
     *
     * This endpoint can list/search/query the list of doors. If the parameter is not given, it returns all doors
     * paginated.
     *
     * @authenticated
     * @paginated
41
     * @queryParam query Searches doors for location, name, and version. Example: bat
42
     *
43
     * @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase $getDoors
Jacob Priddy's avatar
Jacob Priddy committed
44
45
46
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
47
     * @throws \Illuminate\Validation\ValidationException
Jacob Priddy's avatar
Jacob Priddy committed
48
     */
49
    public function index(GetDoorsUseCase $getDoors): JsonResponse
Jacob Priddy's avatar
Jacob Priddy committed
50
    {
51
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
52

53
        $this->validate($this->request, [
54
            'query' => 'string',
55
56
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
57
58
        $presenter = new GetAllDoorsAPIPresenter();

59
        $getDoors->query($this->request->input('query'), $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
60

61
62
63
        return $this->respondWithData($presenter->getViewModel([
            'query' => $this->request->input('query'),
        ]));
Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
    }

    /**
67
68
69
70
71
72
     * Get Door
     *
     * This endpoint gets a specific door and returns information relating directly to the door.
     *
     * @authenticated
     *
Jacob Priddy's avatar
Jacob Priddy committed
73
74
     * @response 404 {"status":"error","code":404,"message":"Entity not found"}
     *
Jacob Priddy's avatar
Jacob Priddy committed
75
76
77
78
79
80
81
82
     * @param \Source\UseCases\Doors\GetDoor\GetDoorUseCase $getDoor
     * @param string                                        $doorId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function get(GetDoorUseCase $getDoor, string $doorId): JsonResponse
    {
83
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
84
85
86
87
88
89
90

        $presenter = new GetDoorAPIPresenter();

        $getDoor->get($doorId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
Jacob Priddy's avatar
Jacob Priddy committed
91
92

    /**
93
94
95
96
97
     * Create Door
     *
     * This endpoint creates a new door in the system.
     *
     * @authenticated
98
     * @bodyParam location string required Door Number or location describing where it is such as `CSP165`. Example: CSP165
99
     * @bodyParam name string required The unique name for the door. Must be unique. Example: Engineering Lecture Hall
100
     *
Jacob Priddy's avatar
Jacob Priddy committed
101
102
103
     * @param \Source\UseCases\Doors\CreateDoor\CreateDoorUseCase $createDoor
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
Jacob Priddy's avatar
Jacob Priddy committed
104
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
105
     * @throws \Source\Exceptions\EntityExistsException
Jacob Priddy's avatar
Jacob Priddy committed
106
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
107
108
109
     */
    public function store(CreateDoorUseCase $createDoor): JsonResponse
    {
110
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
111

Jacob Priddy's avatar
Jacob Priddy committed
112
113
114
115
116
117
118
        $this->validate($this->request, [
            'location' => 'required|string|max:255',
            'name' => 'required|string|max:255',
        ]);

        $presenter = new CreateDoorAPIPresenter();

119
        $createDoor->create($this->request->all(), $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
120
121
122

        return $this->respondWithData($presenter->getViewModel());
    }
123
124

    /**
125
126
     * Update Door
     *
127
     * This route updates a door in the system. It does not modify the associated door token.
128
129
130
     *
     * @authenticated
     * @urlParam doorId required The application ID of the door to update. Example: 2
131
132
     * @bodyParam location string  Door Number or location describing where it is such as `CSP165`.  Example: CSP166
     * @bodyParam name string The unique name for the door. Must be unique. Example: Not the Engineering Lecture Hall
133
     *
134
135
136
137
138
     * @param \Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase $updateDoor
     * @param string                                              $doorId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
139
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
140
     * @throws \Source\Exceptions\EntityExistsException
141
142
143
     */
    public function update(UpdateDoorUseCase $updateDoor, string $doorId): JsonResponse
    {
144
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
145

146
147
148
149
150
151
152
153
154
155
156
157
        $this->validate($this->request, [
            'location' => 'string|max:255',
            'name' => 'string|max:255',
        ]);

        $presenter = new UpdateDoorAPIPresenter();
        $attributes = $this->request->all();

        $updateDoor->update($doorId, $attributes, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
Jacob Priddy's avatar
Jacob Priddy committed
158
159

    /**
160
161
162
163
164
165
166
167
168
     * Regenerate Door Token
     *
     * This endpoint regenerates the door token as it is stored hashed in the database and cannot be retrieved once
     * generated. If for some reason it gets lost, or compromised, this endpoint can be used to regenerate the
     * associated door token.
     *
     * @authenticated
     * @urlParam doorId required The application ID of the door to update. Example: 2
     *
Jacob Priddy's avatar
Jacob Priddy committed
169
170
171
172
     * @param \Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase $tokenGenerator
     * @param string                                                            $doorId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
173
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
174
     * @throws \Source\Exceptions\EntityExistsException
Jacob Priddy's avatar
Jacob Priddy committed
175
176
177
     */
    public function regenerateToken(GenerateDoorTokenUseCase $tokenGenerator, string $doorId): JsonResponse
    {
178
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
179

Jacob Priddy's avatar
Jacob Priddy committed
180
181
        $presenter = new CreateDoorAPIPresenter();

182
        $tokenGenerator->generate($doorId, $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
183
184
185

        return $this->respondWithData($presenter->getViewModel());
    }
Jacob Priddy's avatar
Jacob Priddy committed
186
187

    /**
188
189
190
191
192
193
194
195
     * Delete Door
     *
     * This endpoint deletes a door in the system. Once deleted it cannot be recovered and the door
     * will no longer be able to make requests.
     *
     * @authenticated
     * @urlParam doorId required The application ID of the door to update. Example: 2
     *
Jacob Priddy's avatar
Jacob Priddy committed
196
197
198
199
     * @param \Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase $doorDelyeeter
     * @param string                                              $doorId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
200
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
201
202
203
     */
    public function delete(DeleteDoorUseCase $doorDelyeeter, string $doorId): JsonResponse
    {
204
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
Jacob Priddy's avatar
Jacob Priddy committed
205

Jacob Priddy's avatar
Jacob Priddy committed
206
207
208
209
210
211
        $presenter = new DeleteDoorAPIPresenter();

        $doorDelyeeter->delete($doorId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
212
213

    /**
214
215
216
217
218
219
220
221
     * Door Groups
     *
     * This endpoint gets the list of groups that a door is apart of.
     *
     * @paginated
     * @authenticated
     * @urlParam doorId required The door ID to get the groups for. Example: 1
     *
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
     * @param \Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase $doorGroups
     * @param string                                                        $doorId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function getGroupsForDoor(GetDoorGroupsUseCase $doorGroups, string $doorId): JsonResponse
    {
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

        $presenter = new GetDoorGroupsAPIPresenter();

        $doorGroups->getGroupsForDoor($doorId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
238
239

    /**
240
241
242
243
244
245
246
247
     * Add Door to Group
     *
     * This endpoint attaches a door to a group.
     *
     * @authenticated
     * @urlParam doorId required the door ID to attach. Example: 2
     * @urlParam groupId required the group ID to attach to. Example: 8
     *
248
249
250
251
252
253
254
     * @param \Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase $addDoorToGroup
     * @param string                                                          $doorId
     * @param string                                                          $groupId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
Jacob Priddy's avatar
Jacob Priddy committed
255
256
    public function addDoorToGroup(AddDoorToGroupUseCase $addDoorToGroup, string $doorId, string $groupId): JsonResponse
    {
257
258
259
260
261
262
263
264
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

        $presenter = new AddDoorToGroupAPIPresenter();

        $addDoorToGroup->addDoorToGroup($doorId, $groupId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
265
266

    /**
267
268
269
270
271
272
273
274
     * Remove Door from Group
     *
     * This endpoint removes a door from a group.
     *
     * @authenticated
     * @urlParam doorId required the door to detach. Example: 2
     * @urlParam groupId required the group to detach from. Example: 8
     *
275
276
277
278
279
280
281
     * @param \Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase $removeDoorFromGroup
     * @param string                                                                    $doorId
     * @param string                                                                    $groupId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
Jacob Priddy's avatar
Jacob Priddy committed
282
283
    public function removeDoorFromGroup(RemoveDoorFromGroupUseCase $removeDoorFromGroup, string $doorId, string $groupId): JsonResponse
    {
284
285
286
287
288
289
290
291
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

        $presenter = new RemoveDoorFromGroupAPIPresenter();

        $removeDoorFromGroup->removeDoorFromGroup($doorId, $groupId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
Jacob Priddy's avatar
Jacob Priddy committed
292
}