|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WagnerMontanini\GoomerApi\Api; |
|
4
|
|
|
|
|
5
|
|
|
use WagnerMontanini\GoomerApi\Support\Pager; |
|
6
|
|
|
use WagnerMontanini\GoomerApi\Models\Product; |
|
7
|
|
|
use WagnerMontanini\GoomerApi\Models\Restaurant; |
|
8
|
|
|
use WagnerMontanini\GoomerApi\Models\ProductCategory; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Products |
|
12
|
|
|
* @package WagnerMontanini\GoomerApi |
|
13
|
|
|
*/ |
|
14
|
|
|
class Products extends GoomerApi |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Products constructor. |
|
18
|
|
|
* @throws \Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* list all products |
|
27
|
|
|
* @param array $data |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
public function index(array $data): void |
|
31
|
|
|
{ |
|
32
|
|
|
if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT) ) { |
|
33
|
|
|
$this->call( |
|
34
|
|
|
400, |
|
35
|
|
|
"invalid_data", |
|
36
|
|
|
"É preciso informar o ID do restaurante para verificar os produtos" |
|
37
|
|
|
)->back(); |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
//get products |
|
42
|
|
|
$products = (new Product())->find("restaurant_id=:restaurant_id","restaurant_id={$restaurant_id}"); |
|
43
|
|
|
|
|
44
|
|
|
if (!$products->count()) { |
|
45
|
|
|
$this->call( |
|
46
|
|
|
404, |
|
47
|
|
|
"not_found", |
|
48
|
|
|
"Nada encontrado para sua busca." |
|
49
|
|
|
)->back(["results" => 0]); |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$page = (!empty($data["page"]) ? $data["page"] : 1); |
|
54
|
|
|
$pager = new Pager(url("/{restaurant_id}/products")); |
|
55
|
|
|
$pager->pager($products->count(), 10, $page); |
|
56
|
|
|
|
|
57
|
|
|
$response["results"] = $products->count(); |
|
|
|
|
|
|
58
|
|
|
$response["page"] = $pager->page(); |
|
59
|
|
|
$response["pages"] = $pager->pages(); |
|
60
|
|
|
|
|
61
|
|
|
foreach ($products->limit($pager->limit())->offset($pager->offset())->order("name ASC")->fetch(true) as $product) { |
|
62
|
|
|
$response["products"][] = $product->restaurant()->category()->data(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->back($response); |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
*/ |
|
73
|
|
|
public function create(array $data): void |
|
74
|
|
|
{ |
|
75
|
|
|
$request = $this->requestLimit("productsCreate", 5, 60); |
|
76
|
|
|
if (!$request) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT) ) { |
|
81
|
|
|
$this->call( |
|
82
|
|
|
400, |
|
83
|
|
|
"invalid_data", |
|
84
|
|
|
"É preciso informar o ID do restaurante para criar os produtos" |
|
85
|
|
|
)->back(); |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$restaurant = (new Restaurant())->findById($restaurant_id); |
|
90
|
|
|
|
|
91
|
|
|
if (!$restaurant) { |
|
92
|
|
|
$this->call( |
|
93
|
|
|
404, |
|
94
|
|
|
"not_found", |
|
95
|
|
|
"Você tentou cadastrar um produto em um restaurante que não existe" |
|
96
|
|
|
)->back(); |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( empty($data["category_id"]) || !$category_id = filter_var($data["category_id"], FILTER_VALIDATE_INT) || empty($data["name"]) || empty($data["price"]) ) { |
|
|
|
|
|
|
101
|
|
|
$this->call( |
|
102
|
|
|
400, |
|
103
|
|
|
"empty_data", |
|
104
|
|
|
"Para criar informe o ID da categoria do produto, o nome do produto e o preço do produto" |
|
105
|
|
|
)->back(); |
|
106
|
|
|
return ; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$category = (new ProductCategory())->findById($category_id); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
if (!$category) { |
|
112
|
|
|
$this->call( |
|
113
|
|
|
404, |
|
114
|
|
|
"not_found", |
|
115
|
|
|
"Você tentou cadastrar um produto em uma categoria que não existe" |
|
116
|
|
|
)->back(); |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$product = new Product(); |
|
121
|
|
|
$product->restaurant_id = $restaurant_id; |
|
|
|
|
|
|
122
|
|
|
$product->category_id = $category_id; |
|
|
|
|
|
|
123
|
|
|
$product->name = filter_var($data["name"], FILTER_SANITIZE_STRIPPED); |
|
|
|
|
|
|
124
|
|
|
$product->price = filter_var($data["price"], FILTER_SANITIZE_STRIPPED); |
|
|
|
|
|
|
125
|
|
|
$product->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : null; |
|
|
|
|
|
|
126
|
|
|
$product->description = (!empty($data["description"])) ? filter_var($data["description"], FILTER_SANITIZE_STRIPPED) : null; |
|
|
|
|
|
|
127
|
|
|
$product->old_price = (!empty($data["old_price"])) ? filter_var($data["old_price"], FILTER_SANITIZE_STRIPPED) : 0.00; |
|
|
|
|
|
|
128
|
|
|
$product->save(); |
|
129
|
|
|
|
|
130
|
|
|
if($product->fail()){ |
|
131
|
|
|
$this->call( |
|
132
|
|
|
400, |
|
133
|
|
|
"empty_data", |
|
134
|
|
|
$product->fail()->getMessage() |
|
135
|
|
|
)->back(); |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->back(["product" => $product->restaurant()->category()->data()]); |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $data |
|
145
|
|
|
*/ |
|
146
|
|
|
public function read(array $data): void |
|
147
|
|
|
{ |
|
148
|
|
|
if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT) ) { |
|
149
|
|
|
$this->call( |
|
150
|
|
|
400, |
|
151
|
|
|
"invalid_data", |
|
152
|
|
|
"É preciso informar o ID do restaurante para verificar o produto" |
|
153
|
|
|
)->back(); |
|
154
|
|
|
return; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
if (empty($data["product_id"]) || !$product_id = filter_var($data["product_id"], FILTER_VALIDATE_INT) ) { |
|
159
|
|
|
$this->call( |
|
160
|
|
|
400, |
|
161
|
|
|
"invalid_data", |
|
162
|
|
|
"É preciso informar o ID do produto que deseja consultar" |
|
163
|
|
|
)->back(); |
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$product = (new Product())->find("restaurant_id = :restaurant_id AND id = :id", |
|
168
|
|
|
"restaurant_id={$restaurant_id}&id=$product_id")->fetch(); |
|
169
|
|
|
|
|
170
|
|
|
if (!$product) { |
|
171
|
|
|
$this->call( |
|
172
|
|
|
404, |
|
173
|
|
|
"not_found", |
|
174
|
|
|
"Você tentou acessar um produto que não existe neste restaurante" |
|
175
|
|
|
)->back(); |
|
176
|
|
|
return; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$response["product"] = $product->restaurant()->category()->data(); |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
$this->back($response); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param array $data |
|
186
|
|
|
*/ |
|
187
|
|
|
public function update(array $data): void |
|
188
|
|
|
{ |
|
189
|
|
|
if ( empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) { |
|
190
|
|
|
$this->call( |
|
191
|
|
|
400, |
|
192
|
|
|
"invalid_data", |
|
193
|
|
|
"É preciso informar o ID do restaurante para atualizar o produto" |
|
194
|
|
|
)->back(); |
|
195
|
|
|
return; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if (empty($data["product_id"]) || !$product_id = filter_var($data["product_id"], FILTER_VALIDATE_INT) ) { |
|
199
|
|
|
$this->call( |
|
200
|
|
|
400, |
|
201
|
|
|
"invalid_data", |
|
202
|
|
|
"É preciso informar o ID do produto que deseja atualizar" |
|
203
|
|
|
)->back(); |
|
204
|
|
|
return; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$product = (new Product())->find("restaurant_id = :restaurant_id AND id = :id", |
|
208
|
|
|
"restaurant_id={$restaurant_id}&id=$product_id")->fetch(); |
|
209
|
|
|
|
|
210
|
|
|
if (!$product) { |
|
211
|
|
|
$this->call( |
|
212
|
|
|
404, |
|
213
|
|
|
"not_found", |
|
214
|
|
|
"Você tentou atualizar um produto de um restaurante que não existe" |
|
215
|
|
|
)->back(); |
|
216
|
|
|
return; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$product->name = (!empty($data["name"])) ? filter_var($data["name"], FILTER_SANITIZE_STRIPPED) : $product->name; |
|
220
|
|
|
$product->price = (!empty($data["price"])) ? filter_var($data["price"], FILTER_SANITIZE_STRIPPED) : $product->price; |
|
221
|
|
|
$product->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : $product->image; |
|
222
|
|
|
$product->description = (!empty($data["description"])) ? filter_var($data["description"], FILTER_SANITIZE_STRIPPED) : $product->description; |
|
223
|
|
|
$product->old_price = (!empty($data["old_price"])) ? filter_var($data["old_price"], FILTER_SANITIZE_STRIPPED) : $product->old_price; |
|
224
|
|
|
$product->save(); |
|
225
|
|
|
|
|
226
|
|
|
if($product->fail()){ |
|
227
|
|
|
$this->call( |
|
228
|
|
|
400, |
|
229
|
|
|
"empty_data", |
|
230
|
|
|
$product->fail()->getMessage() |
|
231
|
|
|
)->back(); |
|
232
|
|
|
return; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$this->back(["product" => $product->restaurant()->category()->data()]); |
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param array $data |
|
241
|
|
|
*/ |
|
242
|
|
|
public function delete(array $data): void |
|
243
|
|
|
{ |
|
244
|
|
|
if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) { |
|
245
|
|
|
$this->call( |
|
246
|
|
|
400, |
|
247
|
|
|
"invalid_data", |
|
248
|
|
|
"É preciso informar o ID do restaurante para deletar o produto" |
|
249
|
|
|
)->back(); |
|
250
|
|
|
return; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if (empty($data["product_id"]) || !$product_id = filter_var($data["product_id"], FILTER_VALIDATE_INT) ) { |
|
254
|
|
|
$this->call( |
|
255
|
|
|
400, |
|
256
|
|
|
"invalid_data", |
|
257
|
|
|
"É preciso informar o ID do produto que deseja deletar" |
|
258
|
|
|
)->back(); |
|
259
|
|
|
return; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
$product = (new Product())->find("restaurant_id = :restaurant_id AND id = :id", |
|
263
|
|
|
"restaurant_id={$restaurant_id}&id=$product_id")->fetch(); |
|
264
|
|
|
|
|
265
|
|
|
if (!$product) { |
|
266
|
|
|
$this->call( |
|
267
|
|
|
404, |
|
268
|
|
|
"not_found", |
|
269
|
|
|
"Você tentou excluir um produto de um restaurante que não existe" |
|
270
|
|
|
)->back(); |
|
271
|
|
|
return; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$product->destroy(); |
|
275
|
|
|
$this->call( |
|
276
|
|
|
200, |
|
277
|
|
|
"success", |
|
278
|
|
|
"O produto foi excluído com sucesso", |
|
279
|
|
|
"accepted" |
|
280
|
|
|
)->back(); |
|
281
|
|
|
} |
|
282
|
|
|
} |