1
|
|
|
<?php namespace App\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\RepositoryException; |
4
|
|
|
use App\Models\Categories; |
5
|
|
|
use App\Repositories\CategoryRepository; |
6
|
|
|
use DB; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Response; |
9
|
|
|
use Validator; |
10
|
|
|
|
11
|
|
|
class CategoryController extends Controller |
12
|
|
|
{ |
13
|
|
|
protected $categoryRepository; |
14
|
|
|
|
15
|
|
|
public function __construct(CategoryRepository $categoryRepository) |
16
|
|
|
{ |
17
|
|
|
$this->categoryRepository = $categoryRepository; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Display a listing of the resource. |
22
|
|
|
* |
23
|
|
|
* @return Response |
24
|
|
|
*/ |
25
|
|
|
public function index() |
26
|
|
|
{ |
27
|
|
|
return response()->json($this->categoryRepository->allAPI()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Store a newly created resource in storage. |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function store(Request $request) |
36
|
|
|
{ |
37
|
|
|
$validator = Validator::make($request->all(), [ |
38
|
|
|
'title' => 'required|name|between:1,50', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
42
|
|
|
return response() |
43
|
|
|
->json(['code' => 1, 'error' => 'Title or description is invalid']) |
44
|
|
|
->setStatusCode(400); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$categoryID = $this->categoryRepository->store($request->all()); |
49
|
|
|
} catch (RepositoryException $e) { |
50
|
|
|
return response() |
51
|
|
|
->json(['code' => 2, 'error' => $e->getMessage()]) |
52
|
|
|
->setStatusCode(400); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return response()->json(['id' => $categoryID]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Display the specified resource. |
60
|
|
|
* |
61
|
|
|
* @param int $id |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function show($id) |
65
|
|
|
{ |
66
|
|
View Code Duplication |
if (!$this->isID($id)) { |
|
|
|
|
67
|
|
|
return response() |
68
|
|
|
->json(['code'=>1, 'error'=>'Parameter must be an integer']) |
69
|
|
|
->setStatusCode(400); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$category = Categories::fromGroup()->find($id); |
|
|
|
|
73
|
|
|
|
74
|
|
|
if ($category == null) { |
75
|
|
|
return response()->json([]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return response()->json($category); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update the specified resource in storage. |
83
|
|
|
* |
84
|
|
|
* @param int $id |
85
|
|
|
* @return Response |
86
|
|
|
*/ |
87
|
|
|
public function update($id, Request $request) |
88
|
|
|
{ |
89
|
|
|
$validator = Validator::make($request->all(), [ |
90
|
|
|
'title' => 'required|name|between:1,50' |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
94
|
|
|
return response() |
95
|
|
|
->json(['code' => 3, 'error' => 'Validation failed']) |
96
|
|
|
->setStatusCode(400); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (!$this->isID($id)) { |
|
|
|
|
100
|
|
|
return response() |
101
|
|
|
->json(['code' => 1, 'error' => 'Parameter must be numeric']) |
102
|
|
|
->setStatusCode(400); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->categoryRepository->contains($request->input('title'))) { |
106
|
|
|
return response() |
107
|
|
|
->json(['code' => 2, 'error' => 'Category name already exists']) |
108
|
|
|
->setStatusCode(400); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$category = Categories::fromGroup()->find($id); |
|
|
|
|
112
|
|
|
|
113
|
|
View Code Duplication |
if ($category == null) { |
|
|
|
|
114
|
|
|
return response() |
115
|
|
|
->json(['code' => 2, 'error' => 'Category not found']) |
116
|
|
|
->setStatusCode(400); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$category->category_title = $request->input('title'); |
120
|
|
|
$category->description = $request->input('description'); |
121
|
|
|
|
122
|
|
View Code Duplication |
if (!$category->save()) { |
|
|
|
|
123
|
|
|
return response() |
124
|
|
|
->json(['code' => 2, 'error' => 'Database error']) |
125
|
|
|
->setStatusCode(400); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return response()->json([]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Remove the specified resource from storage. |
134
|
|
|
* |
135
|
|
|
* @param int $id |
136
|
|
|
* @return Response |
137
|
|
|
*/ |
138
|
|
|
public function destroy($id) |
139
|
|
|
{ |
140
|
|
View Code Duplication |
if (!$this->isID($id)) { |
|
|
|
|
141
|
|
|
return response() |
142
|
|
|
->json(['code' => 1, 'error' => 'Parameter must be numeric']) |
143
|
|
|
->setStatusCode(400); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$category = Categories::fromGroup()->find($id); |
|
|
|
|
147
|
|
|
|
148
|
|
View Code Duplication |
if ($category == null) { |
|
|
|
|
149
|
|
|
return response() |
150
|
|
|
->json(['code' => 2, 'error' => 'Category not found']) |
151
|
|
|
->setStatusCode(400); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$products = Categories::find($id)->products()->get(); |
155
|
|
|
|
156
|
|
View Code Duplication |
if (count($products) != 0) { |
|
|
|
|
157
|
|
|
return response() |
158
|
|
|
->json(['code' => 3, 'error' => 'Category not empty']) |
159
|
|
|
->setStatusCode(400); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
try { |
163
|
|
|
$category->delete(); |
164
|
|
|
} catch (\Exception $e) { |
165
|
|
|
return response() |
166
|
|
|
->json(['code' => 4, 'error' => 'Database error']) |
167
|
|
|
->setStatusCode(400); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return response()->json([]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function isID($id) |
174
|
|
|
{ |
175
|
|
|
return (bool)preg_match('/^[0-9]{1,10}$/', $id); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.