Completed
Pull Request — master (#51)
by claudio
03:06
created

MeetingTimeslotsController::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.4286
cc 2
eloc 10
nc 2
nop 3
crap 2.004
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Planners;
4
5
use Illuminate\Http\Request;
6
use plunner\Employee;
7
use plunner\Http\Requests;
8
use plunner\Meeting;
9
use plunner\Group;
10
use plunner\MeetingTimeslot;
11
use plunner\Http\Controllers\Controller;
12
use plunner\Http\Requests\Employees\MeetingTimeslotRequest;
13
use Illuminate\Support\Facades\Response;
14
15
16
class MeetingTimeslotsController extends Controller
17
{
18
    /**
19
     * ExampleController constructor.
20
     */
21 30
    public function __construct()
22
    {
23 30
        config(['auth.model' => \plunner\Planner::class]);
24 30
        config(['jwt.user' => \plunner\Planner::class]);
25 30
        $this->middleware('jwt.authandrefresh:mode-en');
26 30
    }
27
28
    /**
29
     * @param $groupId
30
     * @param $meetingId
31
     * @return mixed
32
     */
33 6
    public function index($groupId, $meetingId)
34
    {
35 6
        $group = Group::findOrFail($groupId);
36 6
        $this->authorize($group);
37 4
        $meeting = Meeting::findOrFail($meetingId);
38 4
        $this->authorize($meeting);
39
40 4
        if ($meeting->group_id == $groupId)
41 4
            return $meeting->timeslots;
42
        return Response::json(['error' => 'meeting->group_id <> groupId'], 403);
43
    }
44
45
    /**
46
     * Display the specified resource.
47
     *
48
     * @param int $groupId
49
     * @param int $meetingId
50
     *  @param int $timeslotId
51
     * @return mixed
52
     */
53 6
    public function show($groupId, $meetingId, $timeslotId)
54
    {
55 6
        $group = Group::findOrFail($groupId);
56 6
        $this->authorize($group);
57 6
        $meeting = Meeting::findOrFail($meetingId);
58 6
        $this->authorize($meeting);
59 6
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
60 4
        $this->authorize($timeslot);
61
62 4
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId)
63 4
            return $timeslot;
64
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
65
    }
66
67
    /**
68
     * @param MeetingTimeslotRequest $request
69
     * @param $groupId
70
     * @param $meetingId
71
     * @return mixed
72
     */
73 30
    public function store(MeetingTimeslotRequest $request, $groupId, $meetingId)
74
    {
75 30
        $group = Group::findOrFail($groupId);
76 30
        $this->authorize($group);
77 30
        $meeting = Meeting::findOrFail($meetingId);
78 30
        $this->authorize($meeting);
79
80 30
        $input = $request->all();
81
82 30
        if ($meeting->group_id == $groupId) {
83 30
            $timeslot = $meeting->timeslots()->create($input);
84 30
            return $timeslot;
85
        }
86
        return Response::json(['error' => 'meeting->group_id <> groupId'], 403);
87
    }
88
89
    /**
90
     * @param MeetingTimeslotRequest $request
91
     * @param $groupId
92
     * @param $meetingId
93
     * @param $timeslotId
94
     */
95 6
    public function update(MeetingTimeslotRequest $request, $groupId, $meetingId, $timeslotId)
96
    {
97 6
        $group = Group::findOrFail($groupId);
98 6
        $this->authorize($group);
99 6
        $meeting = Meeting::findOrFail($meetingId);
100 6
        $this->authorize($meeting);
101 5
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
102 5
        $this->authorize($timeslot);
103
104 5
        $input = $request->all();
105
106 5
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId) {
107 4
            $timeslot->update($input);
108 4
            return $timeslot;
109
        }
110 1
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param int $groupId
117
     * @param int $meetingId
118
     * @param int $timeslotId
119
     * @return mixed
120
     */
121 4
    public function destroy($groupId, $meetingId, $timeslotId)
122
    {
123 4
        $group = Group::findOrFail($groupId);
124 4
        $this->authorize($group);
125 4
        $meeting = Meeting::findOrFail($meetingId);
126 4
        $this->authorize($meeting);
127 4
        $timeslot = MeetingTimeslot::findOrFail($timeslotId);
128 4
        $this->authorize($timeslot);
129
130 4
        if ($meeting->group_id == $groupId && $timeslot->meeting_id == $meetingId) {
131 4
            $timeslot->delete();
132 4
            return $timeslot;
133
        }
134
        return Response::json(['error' => 'meeting->group_id <> groupId || timeslot->meeting_id <> meetingId'], 403);
135
    }
136
}
137