AttemptsController.php 2.29 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\Attempts\GetAttempts\GetAttemptsUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\UseCases\Attempts\APIPresenter as GetAttemptsAPIPresenter;
9

10
11
12
13
14
/**
 * @group Door Attempt Logs
 *
 * APIs for viewing attempts on doors. An attempt is a failed code entry that did not match any users.
 */
15
16
17
class AttemptsController extends ApiController
{
    /**
18
19
     * Get Attempts
     *
20
     * This route filters attempts based off of starting date, ending date, or door id.
21
22
23
24
25
26
     * If only start is supplied, all attempts after the start date are given. If only end
     * is supplied, all attempts before the start date are given. If both dates are supplied,
     * all attempts between the given dates are returned. This route is paginated.
     *
     * @authenticated
     * @paginated
27
28
     * @queryParam start The beginning date to filter attempts by. Example: 2000-06-02 08:11:45
     * @queryParam end The ending date to filter attempts by. Example: 2920-06-02 08:11:45
29
     * @queryParam door_id The door id to filter on. Example: 1
30
     *
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_id":["The door id must be an integer."]}}
32
     *
33
34
35
36
37
38
39
40
41
     * @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Exception
     */
    public function index(GetAttemptsUseCase $attempts): JsonResponse
    {
42
        $this->authorizer->protect(Permissions::LOGS_READ);
43
44

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

        $presenter = new GetAttemptsAPIPresenter();

Jacob Priddy's avatar
Jacob Priddy committed
52
        $attempts->getBetweenDates(
53
54
            $this->request->input('start'),
            $this->request->input('end'),
55
            $this->request->input('door_id'),
56
57
58
            $presenter
        );

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