Pārlūkot izejas kodu

refacto for category controller

ljoaquim 5 mēneši atpakaļ
vecāks
revīzija
411cadd418

+ 30 - 0
controllers/CategoryAddProductController.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\CategoryModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class CategoryAddProductController
+{
+    private CategoryModel $model;
+
+    public function __construct()
+    {
+        $this->model = new CategoryModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $added = $this->model->addProductToCategory(
+                    $body['product_name'],
+                    $body['product_price'],
+                    $body['category_name'],
+                    $companyId
+                );
+        return $added ? ResponseLib::sendOk(['product_added' => true]) : ResponseLib::sendFail("Category Not Found", [], "E_VALIDATE")->withStatus(404);
+    }
+}

+ 0 - 63
controllers/CategoryController.php

@@ -1,63 +0,0 @@
-<?php
-
-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)
-    {
-        $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']);
-    }
-}

+ 29 - 0
controllers/CategoryCreateController.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\CategoryModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class CategoryCreateController
+{
+    private CategoryModel $model;
+
+    public function __construct()
+    {
+        $this->model = new CategoryModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $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);
+    }
+}

+ 26 - 0
controllers/CategoryDeleteController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\CategoryModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class CategoryDeleteController
+{
+    private CategoryModel $model;
+
+    public function __construct()
+    {
+        $this->model = new CategoryModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $deleted = $this->model->deleteByName($body['category_name'], $companyId);
+        return $deleted ? ResponseLib::sendOk(['deleted' => true]) : ResponseLib::sendFail("Failed to Delete Category", [], "E_VALIDATE")->withStatus(204);
+
+    }
+}

+ 29 - 0
controllers/CategoryGetController.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\CategoryModel;
+use Psr\Http\Message\ServerRequestInterface;
+
+class CategoryGetController
+{
+    private CategoryModel $model;
+
+    public function __construct()
+    {
+        $this->model = new CategoryModel();
+    }
+
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string)$request->getBody(), true) ?? [];
+        $companyId = $body['company_id'] ?? null;
+        $categories = $this->model->getCategories($companyId);
+        
+        if ($categories) {
+            return ResponseLib::sendOk($categories);
+        }
+        return ResponseLib::sendFail("Failed to retrieve categories", [], "E_DATABASE")->withStatus(500);
+    }
+}

+ 6 - 6
public/index.php

@@ -53,13 +53,13 @@ $app->post('/login', $withCors(\Controllers\LoginController::class));
 $app->post('/register', $withCors(\Controllers\RegisterController::class));
 
 //Rotas Category
-$app->get('/category', $withCors(\Controllers\CategoryController::class));
-$app->post('/category', $withCors(\Controllers\CategoryController::class));
-$app->post('/category/delete', $withCors(\Controllers\CategoryController::class));
-$app->post('/category/add-product', $withCors(\Controllers\CategoryController::class));
+$app->post('/category/get', $withCors(\Controllers\CategoryGetController::class) );
+$app->post('/category/create', $withCors(\Controllers\CategoryCreateController::class) );
+$app->post('/category/delete', $withCors(\Controllers\CategoryDeleteController::class) );
+$app->post('/category/add-product', $withCors(\Controllers\CategoryAddProductController::class) );
 
 //Rotas Product
-$app->get('/product', $withCors(\Controllers\ProductController::class));
-$app->post('/product', $withCors(\Controllers\ProductController::class));
+$app->get('/product', $withCors($authHmac), \Controllers\ProductController::class);
+$app->post('/product', $withCors($authHmac), \Controllers\ProductController::class);
 
 $app->run();