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