OverridesController.php 5.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php


namespace App\Http\Controllers;

use App\Guards\ApiGuard;
use Source\Entities\Override;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Overrides\ApiPresenter;
11
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase;
12
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase;
13
use Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase;
14
15
16
17
18
19
20
21
use Source\UseCases\Overrides\OverridesForDoor\OverridesForDoorUseCase;
use Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCase;
use Source\UseCases\Overrides\OverrideCreate\ApiPresenter as OverrideCreateApiPresenter;

class OverridesController extends ApiController
{
    /**
     * @param \Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase $overrideCreate
22
     * @param \App\Guards\ApiGuard                                            $apiGuard
23
24
25
26
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
27
     * @throws \Exception
28
29
30
31
32
33
34
     */
    public function create(OverrideCreateUseCase $overrideCreate, ApiGuard $apiGuard): JsonResponse
    {
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
        $this->validate($this->request, [
            'reason' => 'required|string|max:1024',
            'door_id' => 'required|integer',
35
            'type' => 'required|integer|between:' . Override::TYPE_OPEN . ',' . Override::TYPE_LOCKED,
36
37
38
39
40
41
42
43
44
45
46
47
            'start' => 'required|date|before:end',
            'end' => 'required|date|after:start',
        ]);

        $presenter = new OverrideCreateApiPresenter();

        $attributes = $this->request->all();
        $attributes['user_id'] = $apiGuard->id();

        $overrideCreate->create($attributes, $presenter);

        $this->setStatusCode($presenter->getStatusCode());
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        return $this->respondWithData($presenter->getViewModel());
    }

    /**
     * @param string                                                          $overrideId
     * @param \Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase $overrideUpdate
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Exception
     */
    public function update(string $overrideId, OverrideUpdateUseCase $overrideUpdate): JsonResponse
    {
        $this->authorizer->protect(Permissions::MANAGE_DOORS);
        $this->validate($this->request, [
            'reason' => 'string|max:1024',
            'start' => 'date|required_if:end|before:end',
            'end' => 'date|required_if:start|after:start',
        ]);

        $presenter = new OverrideCreateApiPresenter();

        $overrideUpdate->update($overrideId, $this->request->all(), $presenter);
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        $this->setStatusCode($presenter->getStatusCode());
        return $this->respondWithData($presenter->getViewModel());
    }

    /**
     * @param string                                                    $overrideId
     * @param \Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase $overrideGet
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function get(string $overrideId, OverrideGetUseCase $overrideGet): JsonResponse
    {
        $this->authorizer->protect(Permissions::LOGS_READ);

        $presenter = new OverrideCreateApiPresenter();

        $overrideGet->get($overrideId, $presenter);

        $this->setStatusCode($presenter->getStatusCode());
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        return $this->respondWithData($presenter->getViewModel());
    }

    /**
     * @param string                                                              $doorId
     * @param \Source\UseCases\Overrides\OverridesForDoor\OverridesForDoorUseCase $overridesForDoor
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function forDoor(string $doorId, OverridesForDoorUseCase $overridesForDoor): JsonResponse
    {
        $this->authorizer->protect(Permissions::LOGS_READ);

        $presenter = new ApiPresenter();

        $overridesForDoor->getOverridesForDoor($doorId, $presenter);

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

    /**
     * @param \Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCase $overridesInDateRange
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Exception
     */
    public function index(OverridesForDateRangeUseCase $overridesInDateRange): JsonResponse
    {
        $this->authorizer->protect(Permissions::LOGS_READ);

        $this->validate($this->request, [
            'start' => 'required|date|before:end',
            'end' => 'required|date|after:start',
        ]);

        $presenter = new ApiPresenter();

        $overridesInDateRange->getOverridesForDateRange(
            $this->request->input('start'),
            $this->request->input('end'),
            $presenter
        );

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