1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scool\Timetables\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use Prettus\Validator\Contracts\ValidatorInterface; |
8
|
|
|
use Prettus\Validator\Exceptions\ValidatorException; |
9
|
|
|
use Scool\Timetables\Http\ControllersHttp\Requests\DesiratumCreateRequest; |
10
|
|
|
use Scool\Timetables\Http\ControllersHttp\Requests\DesiratumUpdateRequest; |
11
|
|
|
use Scool\Timetables\Repositories\DesiratumRepository; |
12
|
|
|
use Scool\Timetables\Validators\DesiratumValidator; |
13
|
|
|
|
14
|
|
|
|
15
|
|
View Code Duplication |
class DesirataController extends Controller |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var DesiratumRepository |
20
|
|
|
*/ |
21
|
|
|
protected $repository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DesiratumValidator |
25
|
|
|
*/ |
26
|
|
|
protected $validator; |
27
|
|
|
|
28
|
|
|
public function __construct(DesiratumRepository $repository, DesiratumValidator $validator) |
29
|
|
|
{ |
30
|
|
|
$this->repository = $repository; |
31
|
|
|
$this->validator = $validator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Display a listing of the resource. |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Http\Response |
39
|
|
|
*/ |
40
|
|
|
public function index() |
41
|
|
|
{ |
42
|
|
|
$this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria')); |
43
|
|
|
$desirata = $this->repository->all(); |
44
|
|
|
|
45
|
|
|
if (request()->wantsJson()) { |
46
|
|
|
|
47
|
|
|
return response()->json([ |
48
|
|
|
'data' => $desirata, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return view('desirata.index', compact('desirata')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Store a newly created resource in storage. |
57
|
|
|
* |
58
|
|
|
* @param DesiratumCreateRequest $request |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Http\Response |
61
|
|
|
*/ |
62
|
|
|
public function store(DesiratumCreateRequest $request) |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
|
67
|
|
|
$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE); |
68
|
|
|
|
69
|
|
|
$desiratum = $this->repository->create($request->all()); |
70
|
|
|
|
71
|
|
|
$response = [ |
72
|
|
|
'message' => 'Desiratum created.', |
73
|
|
|
'data' => $desiratum->toArray(), |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if ($request->wantsJson()) { |
77
|
|
|
|
78
|
|
|
return response()->json($response); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return redirect()->back()->with('message', $response['message']); |
82
|
|
|
} catch (ValidatorException $e) { |
83
|
|
|
if ($request->wantsJson()) { |
84
|
|
|
return response()->json([ |
85
|
|
|
'error' => true, |
86
|
|
|
'message' => $e->getMessageBag() |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return redirect()->back()->withErrors($e->getMessageBag())->withInput(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Display the specified resource. |
97
|
|
|
* |
98
|
|
|
* @param int $id |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Http\Response |
101
|
|
|
*/ |
102
|
|
|
public function show($id) |
103
|
|
|
{ |
104
|
|
|
$desiratum = $this->repository->find($id); |
105
|
|
|
|
106
|
|
|
if (request()->wantsJson()) { |
107
|
|
|
|
108
|
|
|
return response()->json([ |
109
|
|
|
'data' => $desiratum, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return view('desirata.show', compact('desiratum')); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Show the form for editing the specified resource. |
119
|
|
|
* |
120
|
|
|
* @param int $id |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Http\Response |
123
|
|
|
*/ |
124
|
|
|
public function edit($id) |
125
|
|
|
{ |
126
|
|
|
|
127
|
|
|
$desiratum = $this->repository->find($id); |
128
|
|
|
|
129
|
|
|
return view('desirata.edit', compact('desiratum')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Update the specified resource in storage. |
135
|
|
|
* |
136
|
|
|
* @param DesiratumUpdateRequest $request |
137
|
|
|
* @param string $id |
138
|
|
|
* |
139
|
|
|
* @return Response |
140
|
|
|
*/ |
141
|
|
|
public function update(DesiratumUpdateRequest $request, $id) |
142
|
|
|
{ |
143
|
|
|
|
144
|
|
|
try { |
145
|
|
|
|
146
|
|
|
$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE); |
147
|
|
|
|
148
|
|
|
$desiratum = $this->repository->update($request->all(), $id); |
149
|
|
|
|
150
|
|
|
$response = [ |
151
|
|
|
'message' => 'Desiratum updated.', |
152
|
|
|
'data' => $desiratum->toArray(), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
if ($request->wantsJson()) { |
156
|
|
|
|
157
|
|
|
return response()->json($response); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return redirect()->back()->with('message', $response['message']); |
161
|
|
|
} catch (ValidatorException $e) { |
162
|
|
|
|
163
|
|
|
if ($request->wantsJson()) { |
164
|
|
|
|
165
|
|
|
return response()->json([ |
166
|
|
|
'error' => true, |
167
|
|
|
'message' => $e->getMessageBag() |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return redirect()->back()->withErrors($e->getMessageBag())->withInput(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Remove the specified resource from storage. |
178
|
|
|
* |
179
|
|
|
* @param int $id |
180
|
|
|
* |
181
|
|
|
* @return \Illuminate\Http\Response |
182
|
|
|
*/ |
183
|
|
|
public function destroy($id) |
184
|
|
|
{ |
185
|
|
|
$deleted = $this->repository->delete($id); |
186
|
|
|
|
187
|
|
|
if (request()->wantsJson()) { |
188
|
|
|
|
189
|
|
|
return response()->json([ |
190
|
|
|
'message' => 'Desiratum deleted.', |
191
|
|
|
'deleted' => $deleted, |
192
|
|
|
]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return redirect()->back()->with('message', 'Desiratum deleted.'); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.