Ver código fonte

Table routes(get, create, delete, update)

EduLascala 5 meses atrás
pai
commit
b9ab2123ed

+ 4 - 0
bin/setup

@@ -12,6 +12,10 @@ INSERT OR IGNORE INTO company (company_name, company_flag) VALUES ('Ferlin', 'a'
 
 INSERT OR IGNORE INTO role (role_name, role_permission, role_flag, company_id) VALUES ('admin', 'all', 'a', 1);
 
+INSERT OR IGNORE INTO status (status_status) VALUES ('Livre');
+
+INSERT OR IGNORE INTO status (status_status) VALUES ('Ocupado');
+
 EOF
 
 echo "Banco de dados '$DB_FILE' criado e populado com sucesso! Senhas estão hasheadas."

+ 44 - 0
controllers/TableCreateController.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\TableModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class TableCreateController
+{
+    private TableModel $model;
+
+    public function __construct()
+    {
+        $this->model = new TableModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+
+        $companyId = $body['company_id'] ?? null;
+        $tableNumber = $body['table_number'] ?? null;
+        $statusId = $body['status_id'] ?? null;
+
+        if (!$companyId || !$tableNumber || !$statusId) {
+            return ResponseLib::sendFail("Missing required fields", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        if (!$this->model->companyExists((int)$companyId)) {
+            return ResponseLib::sendFail("Invalid company_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        if (!$this->model->statusExists((int)$statusId)) {
+            return ResponseLib::sendFail("Invalid status_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $created = $this->model->createTable((int)$companyId, (string)$tableNumber, (int)$statusId);
+
+        return $created
+            ? ResponseLib::sendOk(['created' => true])
+            : ResponseLib::sendFail("Failed to create table", [], "E_DATABASE")->withStatus(500);
+    }
+}

+ 35 - 0
controllers/TableDeleteController.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\TableModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class TableDeleteController
+{
+    private TableModel $model;
+
+    public function __construct()
+    {
+        $this->model = new TableModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+
+        $tableNumber = $body['table_number'] ?? null;
+        $companyId = $body['company_id'] ?? null;
+
+        if (!$tableNumber || !$companyId) {
+            return ResponseLib::sendFail("Missing table_number or company_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $deleted = $this->model->deleteTableByNumber((string)$tableNumber, (int)$companyId);
+
+        return $deleted
+            ? ResponseLib::sendOk(['deleted' => true])
+            : ResponseLib::sendFail("Table not found or already deleted", [], "E_DATABASE")->withStatus(404);
+    }
+}

+ 35 - 0
controllers/TableGetController.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\TableModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class TableGetController
+{
+    private TableModel $model;
+
+    public function __construct()
+    {
+        $this->model = new TableModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+
+        if (!$companyId) {
+            return ResponseLib::sendFail("Missing company_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $tables = $this->model->getTables((int)$companyId);
+
+        if ($tables) {
+            return ResponseLib::sendOk($tables);
+        }
+
+        return ResponseLib::sendFail("No tables found", [], "E_DATABASE")->withStatus(404);
+    }
+}

+ 43 - 0
controllers/TableUpdateController.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\TableModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class TableUpdateController
+{
+    private TableModel $model;
+
+    public function __construct()
+    {
+        $this->model = new TableModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+
+        $tableNumber = $body['table_number'] ?? null;
+$companyId = $body['company_id'] ?? null;
+$statusStatus = $body['status_status'] ?? null;
+
+if (!$tableNumber || !$companyId || !$statusStatus) {
+    return ResponseLib::sendFail("Missing required fields", [], "E_VALIDATE")->withStatus(400);
+}
+
+$statusId = $this->model->getStatusIdByName($statusStatus);
+
+if ($statusId === null) {
+    return ResponseLib::sendFail("Invalid status_status: '{$statusStatus}'", [], "E_VALIDATE")->withStatus(400);
+}
+
+$updated = $this->model->updateTableByNumber((string)$tableNumber, (int)$companyId, $statusId);
+
+
+        return $updated
+            ? ResponseLib::sendOk(['updated' => true])
+            : ResponseLib::sendFail("Failed to update status or table not found", [], "E_DATABASE")->withStatus(404);
+    }
+}

+ 130 - 0
models/TableModel.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace Models;
+
+class TableModel
+{
+    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);
+
+        // Prevenção de erros de concorrência
+        $this->pdo->exec('PRAGMA journal_mode = WAL;');
+        $this->pdo->exec('PRAGMA busy_timeout = 5000;');
+    }
+
+    // Retorna todas as mesas ativas de uma empresa
+    public function getTables(int $companyId): array
+    {
+        $stmt = $this->pdo->prepare("
+            SELECT table_id, table_number, status_id
+            FROM `table`
+            WHERE company_id = :company_id AND table_flag = 'a'
+        ");
+        $stmt->execute(['company_id' => $companyId]);
+        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
+    }
+
+    // Cria uma nova mesa
+    public function createTable(int $companyId, string $tableNumber, int $statusId): bool
+    {
+        $stmt = $this->pdo->prepare("
+            INSERT INTO `table` (company_id, table_number, status_id, table_flag)
+            VALUES (:company_id, :table_number, :status_id, 'a')
+        ");
+        return $stmt->execute([
+            'company_id' => $companyId,
+            'table_number' => $tableNumber,
+            'status_id' => $statusId
+        ]);
+    }
+
+    // Atualiza o status de uma mesa
+    public function updateTable(int $tableId, int $companyId, int $statusId): bool
+    {
+        $stmt = $this->pdo->prepare("
+            UPDATE `table`
+            SET status_id = :status_id
+            WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'
+        ");
+        return $stmt->execute([
+            'status_id' => $statusId,
+            'table_id' => $tableId,
+            'company_id' => $companyId
+        ]);
+    }
+
+    // Soft delete de uma mesa POR table_id (mantido para compatibilidade, opcional)
+    public function deleteTable(int $tableId, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("
+            UPDATE `table`
+            SET table_flag = 'd'
+            WHERE table_id = :table_id AND company_id = :company_id AND table_flag = 'a'
+        ");
+        return $stmt->execute([
+            'table_id' => $tableId,
+            'company_id' => $companyId
+        ]);
+    }
+
+    // Soft delete de uma mesa POR table_number (novo método)
+    public function deleteTableByNumber(string $tableNumber, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("
+            UPDATE `table`
+            SET table_flag = 'd'
+            WHERE table_number = :table_number AND company_id = :company_id AND table_flag = 'a'
+        ");
+        return $stmt->execute([
+            'table_number' => $tableNumber,
+            'company_id' => $companyId
+        ]);
+    }
+
+    // Verifica se uma empresa existe e está ativa
+    public function companyExists(int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("SELECT 1 FROM company WHERE company_id = :id AND company_flag = 'a'");
+        $stmt->execute(['id' => $companyId]);
+        return (bool) $stmt->fetch();
+    }
+
+    // Verifica se um status existe
+    public function statusExists(int $statusId): bool
+    {
+        $stmt = $this->pdo->prepare("SELECT 1 FROM status WHERE status_id = :id");
+        $stmt->execute(['id' => $statusId]);
+        return (bool) $stmt->fetch();
+    }
+
+    public function getStatusIdByName(string $statusStatus): ?int
+    {
+        $stmt = $this->pdo->prepare("SELECT status_id FROM status WHERE status_status = :name");
+        $stmt->execute(['name' => $statusStatus]);
+        $result = $stmt->fetch(\PDO::FETCH_ASSOC);
+        return $result ? (int)$result['status_id'] : null;
+    }
+
+    public function updateTableByNumber(string $tableNumber, int $companyId, int $statusId): bool
+{
+    $stmt = $this->pdo->prepare("
+        UPDATE `table`
+        SET status_id = :status_id
+        WHERE table_number = :table_number AND company_id = :company_id AND table_flag = 'a'
+    ");
+    return $stmt->execute([
+        'status_id' => $statusId,
+        'table_number' => $tableNumber,
+        'company_id' => $companyId
+    ]);
+}
+
+}
+
+

+ 6 - 0
public/index.php

@@ -57,4 +57,10 @@ $app->post('/product/create', $cors, $authJwt, \Controllers\ProductCreateControl
 $app->post('/product/update', $cors, $authJwt, \Controllers\ProductUpdateController::class);
 $app->post('/product/delete', $cors, $authJwt, \Controllers\ProductDeleteController::class);
 
+//Rotas Table
+$app->post('/table/get', $cors, $authJwt, \Controllers\TableGetController::class); 
+$app->post('/table/create', $cors, $authJwt, \Controllers\TableCreateController::class);
+$app->post('/table/delete', $cors, $authJwt, \Controllers\TableDeleteController::class);
+$app->post('/table/update', $cors, $authJwt, \Controllers\TableUpdateController::class);
+
 $app->run();