1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Http\Controllers\Employees\Calendars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use plunner\Calendar; |
7
|
|
|
use plunner\Http\Controllers\Controller; |
8
|
|
|
use plunner\Http\Requests\Employees\Calendar\TimeslotRequest; |
9
|
|
|
use plunner\Timeslot; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TimeslotsController extends Controller |
13
|
|
|
{ |
14
|
24 |
|
public function __construct() |
15
|
|
|
{ |
16
|
24 |
|
config(['auth.model' => \plunner\Employee::class]); |
17
|
24 |
|
config(['jwt.user' => \plunner\Employee::class]); |
18
|
24 |
|
$this->middleware('jwt.authandrefresh:mode-en'); |
19
|
24 |
|
} |
20
|
|
|
|
21
|
|
|
//TODO check that the calendar is not a caldav calendar, maybe future improvement |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Display a listing of the resource. |
25
|
|
|
* |
26
|
|
|
* @param int $calendarId |
27
|
|
|
* @param Request $request needed for get query to get only current timeslots |
28
|
|
|
* @return \Illuminate\Http\Response |
29
|
|
|
*/ |
30
|
6 |
|
public function index($calendarId, Request $request) |
31
|
|
|
{ |
32
|
|
|
//TODO improvement return small period |
33
|
|
|
/** |
34
|
|
|
* @var $employee Employee |
35
|
|
|
*/ |
36
|
6 |
|
$calendar = Calendar::findOrFail($calendarId); |
37
|
6 |
|
$this->authorize($calendar); |
38
|
6 |
|
$timeslots = $calendar->timeslots(); |
39
|
6 |
|
if ($request->query('current')) |
40
|
5 |
|
$timeslots->where('time_start', '>=', new \DateTime()); |
41
|
6 |
|
return $timeslots->get(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Store a newly created resource in storage. |
46
|
|
|
* |
47
|
|
|
* @param TimeslotRequest $request |
48
|
|
|
* @param int $calendarId |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
3 |
|
public function store(TimeslotRequest $request, $calendarId) |
52
|
|
|
{ |
53
|
|
|
//TODO check that end is after start and start is after now |
54
|
|
|
//TODO CHECK |
55
|
3 |
|
$calendar = Calendar::findOrFail($calendarId); |
56
|
3 |
|
$this->authorize($calendar); |
57
|
3 |
|
$input = $request->all(); |
58
|
3 |
|
$timeslot = $calendar->timeslots()->create($input); |
59
|
|
|
// if( $timeslot->time_start > $timeslot->time_end) //IMPROVE THIS,t hsi must be inserted before creation |
60
|
3 |
|
return $timeslot; |
61
|
|
|
//TODO else |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Display the specified resource. |
66
|
|
|
* |
67
|
|
|
* @param int $calendarId |
68
|
|
|
* @param int $timeslotId |
69
|
|
|
* @return \Illuminate\Http\Response |
70
|
|
|
*/ |
71
|
9 |
|
public function show($calendarId, $timeslotId) |
72
|
|
|
{ |
73
|
|
|
// |
74
|
9 |
|
$calendar = Calendar::findOrFail($calendarId); |
75
|
9 |
|
$this->authorize($calendar); |
76
|
9 |
|
$timeslot = Timeslot::findOrFail($timeslotId); |
77
|
9 |
|
$this->authorize($timeslot); |
78
|
6 |
|
return $timeslot; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update the specified resource in storage. |
83
|
|
|
* |
84
|
|
|
* @param TimeslotRequest $request |
85
|
|
|
* @param int $calendarId |
86
|
|
|
* @param int $timeslotId |
87
|
|
|
* @return \Illuminate\Http\Response |
88
|
|
|
*/ |
89
|
3 |
|
public function update(TimeslotRequest $request, $calendarId, $timeslotId) |
90
|
|
|
{ |
91
|
|
|
// |
92
|
3 |
|
$calendar = Calendar::findOrFail($calendarId); |
93
|
3 |
|
$this->authorize($calendar); |
94
|
3 |
|
$timeslot = Timeslot::findOrFail($timeslotId); |
95
|
3 |
|
$this->authorize($timeslot); |
96
|
3 |
|
$input = $request->all(); |
97
|
3 |
|
$timeslot->update($input); |
98
|
3 |
|
return $timeslot; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Remove the specified resource from storage. |
103
|
|
|
* |
104
|
|
|
* @param int $calendarId |
105
|
|
|
* @param int $timeslotId |
106
|
|
|
* @return \Illuminate\Http\Response |
107
|
|
|
*/ |
108
|
3 |
|
public function destroy($calendarId, $timeslotId) |
109
|
|
|
{ |
110
|
|
|
// |
111
|
3 |
|
$calendar = Calendar::findOrFail($calendarId); |
112
|
3 |
|
$this->authorize($calendar); |
113
|
3 |
|
$timeslot = Timeslot::findOrFail($timeslotId); |
114
|
3 |
|
$this->authorize($timeslot); |
115
|
3 |
|
$timeslot->delete(); |
116
|
3 |
|
return $timeslot; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|