SchedulesController.php 6.12 KB
Newer Older
1
2
3
4
5
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
Jacob Priddy's avatar
Jacob Priddy committed
6
use Source\Authorization\Permissions;
7
use Source\UseCases\Schedules\ScheduleGet\ScheduleGetUseCase;
8
use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCase;
9
use Source\UseCases\Schedules\APIPresenter as ScheduleApiPresenter;
10
use Source\UseCases\Schedules\ScheduleCreate\ScheduleCreateUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
11
use Source\UseCases\Schedules\ScheduleUpdate\ScheduleUpdateUseCase;
12
13
use Source\UseCases\GroupSchedule\SchedulesForGroup\SchedulesForGroupUseCase;
use Source\UseCases\GroupSchedule\APIPresenter as SchedulesForGroupApiPresenter;
14
use Source\UseCases\Schedules\SchedulesGet\APIPresenter as SchedulesGetApiPresenter;
15
use Source\UseCases\GroupSchedule\ActiveSchedulesForGroup\ActiveSchedulesForGroupUseCase;
16
17
18
19

class SchedulesController extends ApiController
{
    /**
20
     * @param \Source\UseCases\Schedules\ScheduleCreate\ScheduleCreateUseCase $scheduleCreate
21
22
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
Jacob Priddy's avatar
Jacob Priddy committed
23
24
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
25
26
27
     */
    public function create(ScheduleCreateUseCase $scheduleCreate): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
28
29
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

30
31
32
33
34
        $this->validate($this->request, [
            'duration' => 'required|integer',
            'group_id' => 'required|integer',
            'type' => 'required|integer',
            'rset' => 'required|string',
35
            'description' => 'required|string|max:1024',
36
37
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
38
        $presenter = new ScheduleApiPresenter();
39
40
41
42
43
44
45

        $scheduleCreate->create($this->request->all(), $presenter);

        $this->setStatusCode($presenter->getStatusCode());

        return $this->respondWithData($presenter->getViewModel());
    }
46
47
48
49
50

    /**
     * @param string                                                    $scheduleId
     * @param \Source\UseCases\Schedules\ScheduleGet\ScheduleGetUseCase $scheduleGet
     * @return \Illuminate\Http\JsonResponse
Jacob Priddy's avatar
Jacob Priddy committed
51
     * @throws \Source\Exceptions\AuthorizationException
52
53
54
55
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function get(string $scheduleId, ScheduleGetUseCase $scheduleGet): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
56
57
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

Jacob Priddy's avatar
Jacob Priddy committed
58
        $presenter = new ScheduleApiPresenter();
59
60
61
62
63

        $scheduleGet->getSchedule($scheduleId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
67
68
69

    /**
     * @param \Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCase $schedulesBetween
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Exception
Jacob Priddy's avatar
Jacob Priddy committed
70
71
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
72
73
74
     */
    public function between(SchedulesGetUseCase $schedulesBetween): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
75
76
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

Jacob Priddy's avatar
Jacob Priddy committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        $this->validate($this->request, [
            'start' => 'required|date|before:end',
            'end' => 'required|date|after:start',
        ]);

        $presenter = new SchedulesGetApiPresenter();

        $schedulesBetween->getBetweenDates(
            $this->request->input('start'),
            $this->request->input('end'),
            $presenter
        );

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

    /**
     * @param string                                                          $scheduleId
     * @param \Source\UseCases\Schedules\ScheduleUpdate\ScheduleUpdateUseCase $scheduleUpdate
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
Jacob Priddy's avatar
Jacob Priddy committed
98
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
99
100
101
102
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function update(string $scheduleId, ScheduleUpdateUseCase $scheduleUpdate): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
103
104
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

Jacob Priddy's avatar
Jacob Priddy committed
105
106
107
108
109
        $this->validate($this->request, [
            'duration' => 'integer',
            'group_id' => 'integer',
            'type' => 'integer',
            'rset' => 'string',
110
            'description' => 'string|max:1024',
Jacob Priddy's avatar
Jacob Priddy committed
111
112
        ]);

Jacob Priddy's avatar
Jacob Priddy committed
113
        $presenter = new ScheduleApiPresenter();
Jacob Priddy's avatar
Jacob Priddy committed
114
115
116
117
118
119
120

        $scheduleUpdate->update($scheduleId, $this->request->all(), $presenter);

        $this->setStatusCode($presenter->getStatusCode());

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

    /**
     * @param string                                                                    $groupId
     * @param \Source\UseCases\GroupSchedule\SchedulesForGroup\SchedulesForGroupUseCase $forGroup
     * @return \Illuminate\Http\JsonResponse
Jacob Priddy's avatar
Jacob Priddy committed
126
     * @throws \Source\Exceptions\AuthorizationException
127
128
129
130
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function getForGroup(string $groupId, SchedulesForGroupUseCase $forGroup): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
131
132
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

133
134
135
136
137
138
139
140
141
142
143
144
        $presenter = new SchedulesForGroupApiPresenter();

        $forGroup->getForGroup($groupId, $presenter);

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

    /**
     * @param string                                                                                $groupId
     * @param \Source\UseCases\GroupSchedule\ActiveSchedulesForGroup\ActiveSchedulesForGroupUseCase $activeForGroup
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
Jacob Priddy's avatar
Jacob Priddy committed
145
146
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
147
148
149
     */
    public function activeForGroup(string $groupId, ActiveSchedulesForGroupUseCase $activeForGroup): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
150
151
        $this->authorizer->protect(Permissions::MANAGE_DOORS);

152
153
154
155
156
157
158
159
160
161
        $this->validate($this->request, [
            'date' => 'date',
        ]);

        $presenter = new SchedulesForGroupApiPresenter();

        $activeForGroup->activeForGroup($groupId, $this->request->input('date'), $presenter);

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