소스 검색

New Products routes refactored

EduLascala 5 달 전
부모
커밋
b85b1248d1

+ 0 - 77
controllers/ProductController.php

@@ -1,77 +0,0 @@
-<?php
-
-namespace Controllers;
-
-use Libs\ResponseLib;
-use Models\ProductModel;
-use Psr\Http\Message\ServerRequestInterface;
-
-class ProductController
-{
-    private ProductModel $model;
-
-    public function __construct()
-    {
-        $this->model = new ProductModel();
-    }
-
-    public function __invoke(ServerRequestInterface $request)
-    {
-        $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') {
-            $products = $this->model->getProducts($companyId);
-            return ResponseLib::sendOk($products);
-        }
-
-        if ($method === 'POST') {
-            // 1. Criar Produto
-            if (isset($body['product_name'], $body['product_price'], $body['category_id'])) {
-                $created = $this->model->createProduct(
-                    $body['product_name'],
-                    $body['product_price'],
-                    (int)$body['category_id'],
-                    $companyId
-                );
-                return $created ? ResponseLib::sendOk(['created' => true]) : ResponseLib::sendFail("Failed to Create Product", [], "E_VALIDATE")->withStatus(402);
-            }
-
-            // 2. Deletar Produto (usando 'delete_product_id' para clareza)
-            if (isset($body['delete_product_id'])) {
-                $deleted = $this->model->deleteProduct((int)$body['delete_product_id'], $companyId);
-                return $deleted ? ResponseLib::sendOk(['deleted' => true]) : ResponseLib::sendFail("Failed to Delete Product or Product Not Found", [], "E_VALIDATE")->withStatus(403);
-            }
-
-            // 3. Atualizar Produto (usando 'update_product_id')
-            if (isset($body['update_product_id'])) {
-                $productId = (int)$body['update_product_id'];
-                $productName = $body['product_name'] ?? null;
-                $productPrice = $body['product_price'] ?? null;
-
-                if ($productName === null && $productPrice === null) {
-                    return ResponseLib::sendFail("Missing product_name or product_price for update", [], "E_VALIDATE")->withStatus(400);
-                }
-
-                $updated = $this->model->updateProduct(
-                    $productId,
-                    $companyId,
-                    $productName,
-                    $productPrice
-                );
-                return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Product or Product Not Found", [], "E_VALIDATE")->withStatus(404);
-            }
-
-            // Se nenhuma das ações POST acima for reconhecida
-            return ResponseLib::sendFail("Missing Data for Product POST action", [], "E_VALIDATE")->withStatus(405);
-        }
-
-        return ResponseLib::sendMethodNotAllowed(['GET', 'POST']);
-    }
-}

+ 38 - 0
controllers/ProductCreateController.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\ProductModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class ProductCreateController
+{
+    private ProductModel $model;
+
+    public function __construct()
+    {
+        $this->model = new ProductModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $productName = $body['product_name'] ?? null;
+        $productPrice = $body['product_price'] ?? null;
+        $categoryId = $body['category_id'] ?? null;
+
+        if (!$companyId || !$productName || !$productPrice || !$categoryId) {
+            return ResponseLib::sendFail("Missing product_name, product_price, category_id or company_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $created = $this->model->createProduct(
+            $productName,
+            (float)$productPrice,
+            (int)$categoryId,
+            (int)$companyId
+        );
+        return $created ? ResponseLib::sendOk(['created' => true]) : ResponseLib::sendFail("Failed to Create Product", [], "E_DATABASE")->withStatus(402);
+    }
+}

+ 34 - 0
controllers/ProductDeleteController.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\ProductModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class ProductDeleteController
+{
+    private ProductModel $model;
+
+    public function __construct()
+    {
+        $this->model = new ProductModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $productName = $body['product_name'] ?? null; // Alterado para receber 'product_name'
+
+        if (!$companyId || !$productName) { // Validação para 'product_name'
+            return ResponseLib::sendFail("Missing company_id or product_name", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $deleted = $this->model->deleteProductByName( // Chamada ao novo método no model
+            $productName,
+            (int)$companyId
+        );
+        return $deleted ? ResponseLib::sendOk(['deleted' => true]) : ResponseLib::sendFail("Failed to Delete Product or Product Not Found", [], "E_DATABASE")->withStatus(403);
+    }
+}

+ 34 - 0
controllers/ProductGetController.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\ProductModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class ProductGetController
+{
+    private ProductModel $model;
+
+    public function __construct()
+    {
+        $this->model = new ProductModel();
+    }
+
+    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(401);
+        }
+
+        $products = $this->model->getProducts($companyId);
+        
+        if ($products) {
+            return ResponseLib::sendOk($products);
+        }
+        return ResponseLib::sendFail("Failed to retrieve products", [], "E_DATABASE")->withStatus(500);
+    }
+}

+ 42 - 0
controllers/ProductUpdateController.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\ProductModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class ProductUpdateController
+{
+    private ProductModel $model;
+
+    public function __construct()
+    {
+        $this->model = new ProductModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $productId = $body['update_product_id'] ?? null;
+        $productName = $body['product_name'] ?? null;
+        $productPrice = $body['product_price'] ?? null;
+
+        if (!$companyId || !$productId) {
+            return ResponseLib::sendFail("Missing company_id or update_product_id", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        if ($productName === null && $productPrice === null) {
+            return ResponseLib::sendFail("Missing product_name or product_price for update", [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $updated = $this->model->updateProduct(
+            (int)$productId,
+            (int)$companyId,
+            $productName,
+            $productPrice !== null ? (float)$productPrice : null
+        );
+        return $updated ? ResponseLib::sendOk(['updated' => true]) : ResponseLib::sendFail("Failed to Update Product or Product Not Found", [], "E_DATABASE")->withStatus(404);
+    }
+}

+ 8 - 0
models/ProductModel.php

@@ -67,4 +67,12 @@ class ProductModel
                                     WHERE product_id = :product_id AND company_id = :company_id AND product_flag = 'a'");
         return $stmt->execute(['product_id' => $productId, 'company_id' => $companyId]);
     }
+
+    // NOVO MÉTODO PARA DELETAR POR NOME
+    public function deleteProductByName(string $productName, int $companyId): bool
+    {
+        $stmt = $this->pdo->prepare("UPDATE product SET product_flag = 'd'
+                                    WHERE product_name = :product_name AND company_id = :company_id AND product_flag = 'a'");
+        return $stmt->execute(['product_name' => $productName, 'company_id' => $companyId]);
+    }
 }

+ 5 - 3
public/index.php

@@ -58,8 +58,10 @@ $app->post('/category/create', $withCors(\Controllers\CategoryCreateController::
 $app->post('/category/delete', $withCors(\Controllers\CategoryDeleteController::class) );
 $app->post('/category/add-product', $withCors(\Controllers\CategoryAddProductController::class) );
 
-//Rotas Product
-$app->get('/product', $withCors($authHmac), \Controllers\ProductController::class);
-$app->post('/product', $withCors($authHmac), \Controllers\ProductController::class);
+//Rotas Product 
+$app->post('/product/get', $withCors(\Controllers\ProductGetController::class));
+$app->post('/product/create', $withCors(\Controllers\ProductCreateController::class));
+$app->post('/product/update', $withCors(\Controllers\ProductUpdateController::class));
+$app->post('/product/delete', $withCors(\Controllers\ProductDeleteController::class));
 
 $app->run();