Explorar o código

feat: Kitechen Route creation and Menu Route using Natural Join

Fernando hai 4 meses
pai
achega
5636e0481f
Modificáronse 4 ficheiros con 105 adicións e 5 borrados
  1. 39 0
      controllers/KitchenGetController.php
  2. 59 0
      models/KitchenModel.php
  3. 4 5
      models/MenuModel.php
  4. 3 0
      public/index.php

+ 39 - 0
controllers/KitchenGetController.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Psr\Http\Message\ServerRequestInterface;
+use Models\KitchenModel;
+use Respect\Validation\Validator as v;
+use Respect\Validation\Exceptions\ValidationException;
+
+class KitchenGetController
+{
+    private KitchenModel $model;
+
+    public function __construct()
+    {
+        $this->model = new KitchenModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string) $request->getBody(), true) ?? [];
+
+        try {
+            v::key('company_id', v::intType()->positive())->assert($body);
+        } catch (ValidationException $e) {
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(401);
+        }
+
+        $companyId = (int) $body['company_id'];
+        $orders = $this->model->getKitchenOrders($companyId);
+
+        if (!$orders) {
+            return ResponseLib::sendFail("No kitchen orders found", [], "E_NO_DATA")->withStatus(204);
+        }
+
+        return ResponseLib::sendOk($orders);
+    }
+}

+ 59 - 0
models/KitchenModel.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace Models;
+
+class KitchenModel
+{
+    private \PDO $pdo;
+
+    public function __construct()
+    {
+        $dbFile = $_ENV['DB_FILE'];
+        $dbPath = __DIR__ . '/../' . $dbFile;
+        $this->pdo = new \PDO("sqlite:" . $dbPath);
+        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+    }
+
+    public function getKitchenOrders(int $companyId): array
+    {
+        $sql = "
+            SELECT o.order_id,
+                o.table_id,
+                o.order_created_at,
+                p.product_name,
+                oi.kitchen_note
+            FROM 'order' o
+            NATURAL JOIN order_item oi
+            NATURAL JOIN product p
+            WHERE o.company_id = :company_id
+              AND o.order_flag = 'a'
+              AND p.product_is_kitchen = 1
+            ORDER BY o.order_created_at ASC;   
+        ";
+
+        $stmt = $this->pdo->prepare($sql);
+        $stmt->execute(['company_id' => $companyId]);
+        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
+
+        $orders = [];
+        foreach ($rows as $row) {
+            $orderId = $row['order_id'];
+
+            if (!isset($orders[$orderId])) {
+                $orders[$orderId] = [
+                    'order_id'        => $row['order_id'],
+                    'table_id'        => $row['table_id'],
+                    'order_created_at'=> $row['order_created_at'],
+                    'items'           => []
+                ];
+            }
+
+            $orders[$orderId]['items'][] = [
+                'product_name' => $row['product_name'],
+                'kitchen_note' => $row['kitchen_note'] ?? ''
+            ];
+        }
+
+        return array_values($orders);
+    }
+}

+ 4 - 5
models/MenuModel.php

@@ -17,18 +17,17 @@ class MenuModel
     public function getMenu(int $companyId): array
     {
         $sql = "
-            SELECT 
-                c.category_name, 
+            SELECT c.category_name, 
                 p.product_name, 
                 p.product_price,
                 d.description_text
             FROM product p
-            JOIN category c ON c.category_id = p.category_id
-            LEFT JOIN description d ON d.product_id = p.product_id AND d.company_id = p.company_id
+            NATURAL JOIN category c
+            LEFT NATURAL JOIN description d
             WHERE p.company_id = :company_id 
               AND p.product_flag != 'd' 
               AND c.category_flag != 'd'
-            ORDER BY c.category_name, p.product_name
+            ORDER BY c.category_name, p.product_name;
         ";
 
         $stmt = $this->pdo->prepare($sql);

+ 3 - 0
public/index.php

@@ -87,4 +87,7 @@ $app->post('/description/update', $cors, $authJwt, \Controllers\DescriptionUpdat
 //Cardapio Route
 $app->get('/menu/get/{id}', $cors, \Controllers\MenuGetController::class);
 
+//Kitchen Route
+$app->post('/kitchen/get', $cors, \Controllers\KitchenGetController::class);
+
 $app->run();