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