1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Http\Controllers\Employees; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use plunner\Group; |
8
|
|
|
use plunner\Employee; |
9
|
|
|
use plunner\Http\Requests; |
10
|
|
|
use plunner\Http\Controllers\Controller; |
11
|
|
|
|
12
|
|
|
class GroupsController extends Controller |
13
|
|
|
{ |
14
|
|
|
private $responder; |
15
|
|
|
|
16
|
|
|
public function __create(ReponseCreator $responder) |
17
|
|
|
{ |
18
|
|
|
$this->responder = $responder; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\Http\Response |
25
|
|
|
*/ |
26
|
|
|
public function index() |
27
|
|
|
{ |
28
|
|
|
// |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the form for creating a new resource. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Http\Response |
35
|
|
|
*/ |
36
|
|
|
public function create() |
37
|
|
|
{ |
38
|
|
|
// |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Store a newly created resource in storage. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Http\Request $request |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
|
public function store(Request $request) |
48
|
|
|
{ |
49
|
|
|
$this->validateGroupUpdates($request); |
50
|
|
|
$input = $request->all(); |
51
|
|
|
|
52
|
|
|
$g = Group::create([ |
53
|
|
|
'name' => $input['group_name'], |
54
|
|
|
'description' => isset($input['description']) ? $input['description'] : '' |
55
|
|
|
]); |
56
|
|
|
$employees = Employee::whereIn('name', $input['employees'])->get(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$g->addEmployees($employees); |
59
|
|
|
|
60
|
|
|
return $this->respondOK(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Display the specified resource. |
65
|
|
|
* |
66
|
|
|
* @param int $id |
67
|
|
|
* @return \Illuminate\Http\Response |
68
|
|
|
*/ |
69
|
|
|
public function show($id) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
// |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Show the form for editing the specified resource. |
76
|
|
|
* |
77
|
|
|
* @param int $id |
78
|
|
|
* @return \Illuminate\Http\Response |
79
|
|
|
*/ |
80
|
|
|
public function edit($id) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
// |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Update the specified resource in storage. |
87
|
|
|
* |
88
|
|
|
* @param \Illuminate\Http\Request $request |
89
|
|
|
* @param int $id |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
public function update(Request $request, $id) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$this->validateGroupUpdates($request); |
95
|
|
|
$input = $request->all(); |
96
|
|
|
|
97
|
|
|
$employees = Employee::whereIn('name', $input['employees'])->get(); |
|
|
|
|
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$group = Group::findOrFail($input['group_name']); |
|
|
|
|
101
|
|
|
} catch (ModelNotFoundException $e) { |
|
|
|
|
102
|
|
|
return $this->responder->respond( |
103
|
|
|
[ |
104
|
|
|
'message' => 'Group not found', |
105
|
|
|
'group_name' => $input['group_name'] |
106
|
|
|
], |
107
|
|
|
404 |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
$group->addEmployees($employees); |
111
|
|
|
|
112
|
|
|
return $this->respondOK(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function validateGroupUpdates($request) |
116
|
|
|
{ |
117
|
|
|
$this->validate($request, [ |
118
|
|
|
'group_name' => 'required|max:255', |
119
|
|
|
'employees' => 'required|array', |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function respondOK() |
124
|
|
|
{ |
125
|
|
|
return $this->responder->respond([], 200); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Remove the specified resource from storage. |
130
|
|
|
* |
131
|
|
|
* @param int $id |
132
|
|
|
* @return \Illuminate\Http\Response |
133
|
|
|
*/ |
134
|
|
|
public function destroy($id) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
// |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|