|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Departments; |
|
6
|
|
|
use App\Http\Requests; |
|
7
|
|
|
use League\Fractal\Manager; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use App\Http\Controllers\Controller; |
|
10
|
|
|
use League\Fractal\Pagination\Cursor; |
|
11
|
|
|
use App\Http\TransformersApi\DepartmentOutput; |
|
12
|
|
|
use League\Fractal\Resource\Collection; |
|
13
|
|
|
use League\Fractal\Resource\Item; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ApiDepartmentsController |
|
17
|
|
|
* @package App\Http\Controllers |
|
18
|
|
|
*/ |
|
19
|
|
|
class ApiDepartmentsController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
private $transformer; |
|
22
|
|
|
private $fractal; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ApiDepartmentsController constructor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->transformer = new DepartmentOutput(); |
|
30
|
|
|
$this->fractal = new Manager(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Display a listing of the resource. |
|
35
|
|
|
* |
|
36
|
|
|
* @TODO Needs test |
|
37
|
|
|
* @TODO Needs documentation |
|
38
|
|
|
* @param \Illuminate\Http\Request $request |
|
39
|
|
|
* @return \Illuminate\Http\Response |
|
40
|
|
|
*/ |
|
41
|
|
|
public function index(Request $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$currentCursor = $request->get('cursor', null); |
|
44
|
|
|
$previousCursor = $request->get('previous', null); |
|
45
|
|
|
$limit = $request->get('limit', 10); |
|
46
|
|
|
|
|
47
|
|
|
if ($currentCursor) { |
|
48
|
|
|
$departments = Departments::with('managers')->where('id', '>', $currentCursor)->take($limit)->get(); |
|
49
|
|
|
} else { |
|
50
|
|
|
$departments = Departments::with('managers')->take($limit)->get(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$newCursor = $departments->last()->id; |
|
54
|
|
|
$cursor = new Cursor($currentCursor, $previousCursor, $newCursor, $departments->count()); |
|
55
|
|
|
$resource = new Collection($departments, $this->transformer); |
|
56
|
|
|
|
|
57
|
|
|
$resource->setCursor($cursor); |
|
58
|
|
|
$outputBody = $this->fractal->createData($resource)->toJson(); |
|
59
|
|
|
|
|
60
|
|
|
return response($outputBody, 200) |
|
|
|
|
|
|
61
|
|
|
->header('Content-Type', 'application/json'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Display the specified resource. |
|
66
|
|
|
* |
|
67
|
|
|
* @TODO needs tests |
|
68
|
|
|
* @TODO needs documentation |
|
69
|
|
|
* @param int $id |
|
70
|
|
|
* @return \Illuminate\Http\Response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function show($id) |
|
73
|
|
|
{ |
|
74
|
|
|
$departments = Departments::find($id); |
|
75
|
|
|
|
|
76
|
|
|
if (count($departments) > 0) { |
|
77
|
|
|
$resource = new Item($departments, $this->transformer); |
|
78
|
|
|
$outputBody = $this->fractal->createData($resource)->toJson(); |
|
79
|
|
|
} else { |
|
80
|
|
|
$outputBody = []; |
|
81
|
|
|
$outputBody['message'] = 'Could not found a department with that ID.'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return response($outputBody, 200) |
|
|
|
|
|
|
85
|
|
|
->header('Content-Type', 'application/json'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Store a newly created resource in storage. |
|
90
|
|
|
* |
|
91
|
|
|
* @TODO needs test |
|
92
|
|
|
* @TODO Needs documentation |
|
93
|
|
|
* @param Requests\DepartmentsValidator $input the input bag from the request. |
|
94
|
|
|
* @return \Illuminate\Http\Response |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function store(Requests\DepartmentsValidator $input) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
Departments::create($input->all()); |
|
99
|
|
|
|
|
100
|
|
|
$output = []; |
|
101
|
|
|
$output['message'] = 'Department created'; |
|
102
|
|
|
$output['data'] = $input->all(); |
|
103
|
|
|
|
|
104
|
|
|
return response($output, 201) |
|
|
|
|
|
|
105
|
|
|
->header('Content-Type', 'application/json'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Update the specified resource in storage. |
|
110
|
|
|
* |
|
111
|
|
|
* @TODO Needs documentation |
|
112
|
|
|
* @TODO Needs tests |
|
113
|
|
|
* @param Requests\DepartmentsValidator $input the input bag from the request. |
|
114
|
|
|
* @param \Illuminate\Http\Request $request |
|
115
|
|
|
* @param int $id |
|
116
|
|
|
* @return \Illuminate\Http\Response |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function update(Requests\DepartmentsValidator $input, Request $request, $id) |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
Departments::find($id)->update($input->all()); |
|
121
|
|
|
|
|
122
|
|
|
$output = []; |
|
123
|
|
|
$output['message'] = 'Department updated'; |
|
124
|
|
|
$output['new_data'] = $input->all(); |
|
125
|
|
|
|
|
126
|
|
|
return response($output, 200) |
|
|
|
|
|
|
127
|
|
|
->header('Content-Type', 'application/json'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Remove the specified resource from storage. |
|
132
|
|
|
* |
|
133
|
|
|
* @TODO need tests |
|
134
|
|
|
* #TODO needs documentation |
|
135
|
|
|
* @param int $id |
|
136
|
|
|
* @return \Illuminate\Http\Response |
|
137
|
|
|
*/ |
|
138
|
|
|
public function destroy($id) |
|
139
|
|
|
{ |
|
140
|
|
|
if (! empty($id)) { |
|
141
|
|
|
Departments::destroy($id); |
|
142
|
|
|
|
|
143
|
|
|
$data = []; |
|
144
|
|
|
$data['message'] = 'Department deleted'; |
|
145
|
|
|
} else { |
|
146
|
|
|
$data = []; |
|
147
|
|
|
$data['message'] = 'Could not delete the department'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return response($data, 200) |
|
|
|
|
|
|
151
|
|
|
->header('Content-Type', 'application/json'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.