EntriesController.php 2.3 KB
Newer Older
1
2
<?php

3
namespace App\Http\Controllers\Api;
4
5
6
7

use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Entries\APIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\UseCases\Entries\GetEntries\GetEntriesUseCase;
9

10
11
12
13
14
15
/**
 * @group Door Entry Logs
 *
 * These endpoints are for accessing entry attempts. A door entry gets logged when a known user is allowed or denied
 *        from a door.
 */
16
17
class EntriesController extends ApiController
{
18
    /**
19
20
21
22
23
24
25
26
27
     * Get Entries
     *
     * Filters door entries based upon several query parameters. If no parameters are given, all door entries are
     * returned in a paginated fashion.
     *
     * @authenticated
     * @paginated
     * @queryParam start The beginning date to filter entries by. Example: 2000-06-02 08:11:45
     * @queryParam end The ending date to filter entries by. Example: 2920-06-02 08:11:45
28
     * @queryParam door_id The door id to filter entries on. Example: Amazon
29
30
     * @queryParam user_id The user id to filter entries on. Example: 420
     *
31
     * @response   422 {"message":"The given data was invalid.","errors":{"start":["The start is not a valid date."],"end":["The end is not a valid date."],"door_location":["The door location must be a string."],"user_id":["The user id must be an integer."]}}
32
     *
Jacob Priddy's avatar
Jacob Priddy committed
33
     * @param \Source\UseCases\Entries\GetEntries\GetEntriesUseCase $userEntries
34
35
36
37
38
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
Jacob Priddy's avatar
Jacob Priddy committed
39
    public function index(GetEntriesUseCase $userEntries): JsonResponse
Jacob Priddy's avatar
Jacob Priddy committed
40
    {
41
        $this->authorizer->protect(Permissions::LOGS_READ);
Jacob Priddy's avatar
Jacob Priddy committed
42

43
        $this->validate($this->request, [
44
45
            'start' => 'nullable|date',
            'end' => 'nullable|date',
46
            'door_location' => 'nullable|string',
47
            'user_id' => 'nullable|integer',
48
49
50
51
        ]);

        $presenter = new APIPresenter();

Jacob Priddy's avatar
Jacob Priddy committed
52
53
        $userEntries->getEntries(
            $this->request->input('user_id'),
54
            $this->request->input('door_location'),
55
56
57
58
59
            $this->request->input('start'),
            $this->request->input('end'),
            $presenter
        );

Jacob Priddy's avatar
Jacob Priddy committed
60
        return $this->respondWithData($presenter->getViewModel($this->request->all()));
61
62
    }
}