|
@@ -0,0 +1,51 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace Controllers;
|
|
|
|
|
+
|
|
|
|
|
+use Libs\ResponseLib;
|
|
|
|
|
+use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
+use Models\MenuModel;
|
|
|
|
|
+
|
|
|
|
|
+class MenuGetController
|
|
|
|
|
+{
|
|
|
|
|
+ private MenuModel $model;
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->model = new MenuModel();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function __invoke(ServerRequestInterface $request)
|
|
|
|
|
+ {
|
|
|
|
|
+ $companyId = $request->getAttribute('id');
|
|
|
|
|
+
|
|
|
|
|
+ if (!is_numeric($companyId) || (int)$companyId <= 0) {
|
|
|
|
|
+ return ResponseLib::sendFail("Parâmetro company_id inválido na URL", [], "E_VALIDATE")->withStatus(400);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $menuItems = $this->model->getMenu((int)$companyId);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$menuItems) {
|
|
|
|
|
+ return ResponseLib::sendFail("Failed to retrieve menu", [], "E_DATABASE")->withStatus(500);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $cardapio = [];
|
|
|
|
|
+ foreach ($menuItems as $item) {
|
|
|
|
|
+ $categoria = $item['category_name'];
|
|
|
|
|
+ if (!isset($cardapio[$categoria])) {
|
|
|
|
|
+ $cardapio[$categoria] = [
|
|
|
|
|
+ 'categoria' => $categoria,
|
|
|
|
|
+ 'produtos' => []
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $cardapio[$categoria]['produtos'][] = [
|
|
|
|
|
+ 'nome' => $item['product_name'],
|
|
|
|
|
+ 'preco' => 'R$ ' . number_format((float)$item['product_price'], 2, ',', ''),
|
|
|
|
|
+ 'descricao' => $item['description_text'] ?? ''
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseLib::sendOk(array_values($cardapio));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|