Browse Source

feat: category endpoints

Fernando 5 months ago
parent
commit
4e165123f2
4 changed files with 92 additions and 14 deletions
  1. 2 2
      bin/testhmac
  2. 51 9
      controllers/CategoryController.php
  3. 36 3
      models/CategoryModel.php
  4. 3 0
      public/index.php

+ 2 - 2
bin/testhmac

@@ -3,8 +3,8 @@
 # Configurações da API e dados do usuário (do test.db)
 API_URL="http://localhost:8000/hmachelloworld"  # Ajuste a porta se necessário (ex: 8080)
 API_USER="du"
-API_KEY="1d747c12b371babd86d560a57937d3e7"
-API_SECRET="684d02af140a99edd4ce0bdeb568f4efb256d38f92112b7c4dee3089ec74c3f1"
+API_KEY="b27dafa631bb814bdf7b130ec913fb81"
+API_SECRET="4ee698dd22640f3c8a5736321581e728ae28c3b64399725405c92a6bf2d5c7eb"
 
 # Gera nonce (timestamp atual em segundos)
 NONCE=$(date +%s)

+ 51 - 9
controllers/CategoryController.php

@@ -3,19 +3,61 @@
 namespace Controllers;
 
 use Libs\ResponseLib;
+use Models\CategoryModel;
 use Psr\Http\Message\ServerRequestInterface;
 
 class CategoryController
 {
+    private CategoryModel $model;
+
+    public function __construct()
+    {
+        $this->model = new CategoryModel();
+    }
+
     public function __invoke(ServerRequestInterface $request)
     {
-        $body = json_decode((string) $request->getBody(), true);
-        $category_id = $body['category_id'];
-        $company_id = $body['company_id'];
-        $category_name = $body['category_name'];
-        $category_is_kitchen = $body['category_is_kitchen'];
-        $category_flag = $body['category_flag'];
-
-        return ResponseLib::sendOk($data);
+        $method = $request->getMethod();
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+
+        if (!$companyId) {
+            return ResponseLib::sendFail("Missing Company ID", [], "E_VALIDATE")->withStatus(401);
+        }
+
+        if ($method === 'GET') {
+            $categories = $this->model->getCategories($companyId);
+            return ResponseLib::sendOk($categories);
+        }
+
+        if ($method === 'POST') {
+            if (isset($body['category_name'])) {
+                $created = $this->model->createCategory(
+                    $body['category_name'],
+                    $companyId,
+                    $body['category_is_kitchen'] ?? false
+                );
+                return $created ? ResponseLib::sendOk(['created' => true]) : ResponseLib::sendFail("Failed to Create Category", [], "E_VALIDATE")->withStatus(402);
+            }
+
+            if (isset($body['delete'])) {
+                $deleted = $this->model->deleteByName($body['delete'], $companyId);
+                return $deleted ? ResponseLib::sendOk(['deleted' => true]) : ResponseLib::sendFail("Failed to Delete Category", [], "E_VALIDATE")->withStatus(403);
+            }
+
+            if (isset($body['product_name'], $body['category_name'], $body['product_price'])) {
+                $added = $this->model->addProductToCategory(
+                    $body['product_name'],
+                    (float)$body['product_price'],
+                    $body['category_name'],
+                    $companyId
+                );
+                return $added ? ResponseLib::sendOk(['product_added' => true]) : ResponseLib::sendFail("Category Not Found", [], "E_VALIDATE")->withStatus(404);
+            }
+
+            return ResponseLib::sendFail("Missing Data", [], "E_VALIDATE")->withStatus(405);
+        }
+
+        return ResponseLib::sendMethodNotAllowed(['GET', 'POST']);
     }
-}
+}

+ 36 - 3
models/CategoryModel.php

@@ -1,4 +1,4 @@
-<?php 
+<?php
 
 namespace Models;
 
@@ -8,12 +8,45 @@ class CategoryModel
 
     public function __construct()
     {
-        // Conecta ao DB usando variável do .env
         $dbFile = $_ENV['DB_FILE'];
         $dbPath = __DIR__ . '/../' . $dbFile;
         $this->pdo = new \PDO("sqlite:" . $dbPath);
         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     }
 
-    
+    public function getCategories(int $companyId): array
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM category WHERE company_id = :company_id AND category_flag = 'a'");
+        $stmt->execute(['company_id' => $companyId]);
+        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
+    }
+
+    public function createCategory(string $name, int $companyId, bool $isKitchen): bool
+    {
+        $stmt = $this->pdo->prepare("INSERT INTO category (category_name, category_is_kitchen, category_flag, company_id)
+            VALUES (:name, :is_kitchen, 'a', :company_id)");
+        return $stmt->execute(['name' => $name, 'is_kitchen' => $isKitchen ? 1 : 0, 'company_id' => $companyId]);
+    }
+
+    public function deleteByName(string $name, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("UPDATE category SET category_flag = 'd'
+            WHERE category_name = :name AND company_id = :company_id AND category_flag = 'a'");
+        return $stmt->execute(['name' => $name, 'company_id' => $companyId]);
+    }
+
+    public function addProductToCategory(string $productName, float $price, string $categoryName, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("SELECT category_id FROM category WHERE category_name = :name AND company_id = :company_id AND category_flag = 'a'");
+        $stmt->execute(['name' => $categoryName, 'company_id' => $companyId]);
+        $category = $stmt->fetch(\PDO::FETCH_ASSOC);
+
+        if (!$category) {
+            return false;
+        }
+
+        $stmt = $this->pdo->prepare("INSERT INTO product (product_name, product_price, category_id, company_id, product_flag)
+            VALUES (:name, :price, :category_id, :company_id, 'a')");
+        return $stmt->execute(['name' => $productName, 'price' => $price, 'category_id' => $category['category_id'], 'company_id' => $companyId]);
+    }
 }

+ 3 - 0
public/index.php

@@ -33,5 +33,8 @@ $app->post('/login', \Controllers\LoginController::class);
 $app->post('/register', \Controllers\RegisterController::class);
 
 $app->get('/category', \Controllers\CategoryController::class);
+$app->post('/category', \Controllers\CategoryController::class);
+$app->post('/category/delete', \Controllers\CategoryController::class);
+$app->post('/category/add-product', \Controllers\CategoryController::class);
 
 $app->run();