OverridesController.php 5.95 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace App\Http\Controllers\Web\Admin;

use Source\Entities\Override;
7
use Illuminate\Contracts\View\View;
Jacob Priddy's avatar
Jacob Priddy committed
8
9
10
11
12
13
14
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\ValidationException;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase;
use Source\UseCases\Overrides\OverridesGet\OverridesGetUseCase;
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
15
16
use Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase;
use Source\UseCases\Doors\GetDoors\MePresenter as GetDoorsPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
17
use Source\UseCases\Overrides\OverridesGet\WebPresenter as OverridesPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
18
use Source\UseCases\Overrides\OverrideGet\WebPresenter as OverrideGetPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
19
use Source\UseCases\Overrides\OverrideCreate\WebPresenter as OverrideCreatePresenter;
Jacob Priddy's avatar
Jacob Priddy committed
20
use Source\UseCases\Overrides\OverrideUpdate\WebPresenter as OverrideUpdatePresenter;
Jacob Priddy's avatar
Jacob Priddy committed
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

class OverridesController extends Controller
{
    /**
     * @param \Source\UseCases\Overrides\OverridesGet\OverridesGetUseCase $overrides
     * @return \Illuminate\View\View
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Exception
     */
    public function index(OverridesGetUseCase $overrides): View
    {
        $this->validate($this->request, [
            'start' => 'nullable|date',
            'end' => 'nullable|date',
            'door_id' => 'nullable|integer',
            'user_id' => 'nullable|integer',
        ]);

        $presenter = new OverridesPresenter();

        $overrides->filter(
            $this->request->input('door_id'),
            $this->request->input('user_id'),
            $this->request->input('start'),
            $this->request->input('end'),
            $presenter
        );

        return view('admin.overrides', $presenter->getViewModel($this->request->all()));
    }

    /**
     * @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase $doors
     * @return \Illuminate\View\View
     */
    public function create(GetDoorsUseCase $doors): View
    {
        $presenter = new GetDoorsPresenter();

60
        $doors->query([], false, $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
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

        return view('admin.entities.override', ['doors' => $presenter->getViewModel()]);
    }

    /**
     * @param \Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase $createOverride
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Exception
     */
    public function store(OverrideCreateUseCase $createOverride): RedirectResponse
    {
        $this->validate($this->request, [
            'reason' => 'required|string|max:1024',
            'door_id' => 'required|integer',
            'type' => 'required|integer|between:' . Override::TYPE_OPEN . ',' . Override::TYPE_LOCKED,
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'start_time' => 'required|date_format:H:i',
            'end_time' => 'required|date_format:H:i',
        ]);

        $presenter = new OverrideCreatePresenter();

        $createOverride->create([
            'reason' => $this->request->input('reason'),
            'door_id' => $this->request->input('door_id'),
            'type' => $this->request->input('type'),
            'start' => $this->request->input('start_date') . ' ' . $this->request->input('start_time'),
            'end' => $this->request->input('end_date') . ' ' . $this->request->input('end_time'),
            'user_id' => $this->authorizer->getCurrentUserId(),
        ], $presenter);

        if ($presenter->hasError()) {
            throw ValidationException::withMessages($presenter->getViewModel());
        }

        return redirect(route('web.admin.overrides.index'))->with($presenter->getViewModel());
    }

    /**
     * @param string                                                    $overrideId
     * @param \Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase $overrideGet
     * @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase           $doors
     * @return \Illuminate\View\View
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function edit(string $overrideId, OverrideGetUseCase $overrideGet, GetDoorsUseCase $doors): View
    {
        $doorsPresenter = new GetDoorsPresenter();

112
        $doors->query([], false, $doorsPresenter);
Jacob Priddy's avatar
Jacob Priddy committed
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
142
143
144
145
146

        $presenter = new OverrideGetPresenter();

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

        $override = $presenter->getViewModel();

        $this->request->session()->flashInput($override);

        return view('admin.entities.override', [
            'doors' => $doorsPresenter->getViewModel(),
            'id' => $override['id'],
        ]);
    }

    /**
     * @param string                                                          $overrideId
     * @param \Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase $overrideUpdate
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function update(string $overrideId, OverrideUpdateUseCase $overrideUpdate): RedirectResponse
    {
        $this->validate($this->request, [
            'reason' => 'required|string|max:1024',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
            'start_time' => 'required|date_format:H:i',
            'end_time' => 'required|date_format:H:i',
        ]);

        $presenter = new OverrideUpdatePresenter();

147
148
149
150
151
152
153
154
        $overrideUpdate->update(
            $overrideId,
            array_merge(
                $this->request->all(),
                ['user_id' => $this->authorizer->getCurrentUserId()]
            ),
            $presenter
        );
Jacob Priddy's avatar
Jacob Priddy committed
155
156
157
158
159
160
161
162

        if ($presenter->hasError()) {
            throw ValidationException::withMessages($presenter->getViewModel());
        }

        return redirect(route('web.admin.overrides.index'))->with($presenter->getViewModel());
    }
}