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