Forráskód Böngészése

description is now created along with the product (opcional)

EduLascala 4 hónapja
szülő
commit
ca1518bab3

+ 36 - 13
controllers/ProductCreateController.php

@@ -1,20 +1,23 @@
 <?php
-
 namespace Controllers;
 
 use Libs\ResponseLib;
+use Models\CategoryModel; 
 use Models\ProductModel;
+use Models\DescriptionModel; 
 use Psr\Http\Message\ServerRequestInterface;
 use Respect\Validation\Validator as v;
 use Respect\Validation\Exceptions\ValidationException;
 
 class ProductCreateController
 {
-    private ProductModel $model;
+    private ProductModel $productModel;
+    private DescriptionModel $descriptionModel;
 
     public function __construct()
     {
-        $this->model = new ProductModel();
+        $this->productModel = new ProductModel();
+        $this->descriptionModel = new DescriptionModel();
     }
 
     public function __invoke(ServerRequestInterface $request)
@@ -23,11 +26,16 @@ class ProductCreateController
 
         try {
             v::key('company_id', v::intType()->positive())
-             ->key('product_name', v::stringType()->notEmpty()->alnum(' '))
-             ->key('product_price', v::number()->positive())
-             ->key('category_id', v::intType()->positive())
-             ->key('product_is_kitchen', v::optional(v::boolType()))
-             ->assert($body);
+            ->key('product_name', v::stringType()->notEmpty()->alnum(' '))
+            ->key('product_price', v::number()->positive())
+            ->key('category_id', v::intType()->positive())
+            ->key('product_is_kitchen', v::optional(v::boolType()))
+            ->assert($body);
+
+        if (array_key_exists('description_text', $body)) {
+            v::stringType()->notEmpty()->length(1, 70)->assert($body['description_text']);
+        }
+
         } catch (ValidationException $e) {
             return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
@@ -37,8 +45,9 @@ class ProductCreateController
         $productPrice = (float) $body['product_price'];
         $categoryId = $body['category_id'];
         $productIsKitchen = $body['product_is_kitchen'] ?? false;
+        $descriptionText = $body['description_text'] ?? null;
 
-        $created = $this->model->createProduct(
+        $productId = $this->productModel->createProduct(
             $productName,
             $productPrice,
             (int)$categoryId,
@@ -46,8 +55,22 @@ class ProductCreateController
             $productIsKitchen
         );
 
-        return $created
-            ? ResponseLib::sendOk(['created' => true])
-            : ResponseLib::sendFail("Failed to Create Product", [], "E_DATABASE")->withStatus(402);
+        if (!$productId) {
+            return ResponseLib::sendFail("Failed to Create Product", [], "E_DATABASE")->withStatus(402);
+        }
+
+        if ($descriptionText !== null) {
+            $descriptionCreated = $this->descriptionModel->addDescription(
+                (int)$productId,
+                (int)$companyId,
+                $descriptionText
+            );
+
+            if (!$descriptionCreated) {
+                return ResponseLib::sendFail("Product created, but failed to create description.", [], "E_DATABASE")->withStatus(500);
+            }
+        }
+
+        return ResponseLib::sendOk(['created' => true, 'product_id' => $productId]);
     }
-}
+}

+ 1 - 1
controllers/TableCreateController.php

@@ -24,7 +24,7 @@ class TableCreateController
         // ✅ Validação com Respect\Validation (letras, números e espaços permitidos)
         try {
             v::key('company_id', v::intVal()->positive())
-             ->key('table_number', v::stringType()->notEmpty()->regex('/^[\pL\pN ]+$/u')->length(1, 50))
+             ->key('table_number', v::stringType()->notEmpty()->regex('/^[\pL\pN ]+$/u')->length(1, 10))
              ->key('status_id', v::intVal()->positive())
              ->assert($body);
         } catch (NestedValidationException $e) {

+ 4 - 2
models/ProductModel.php

@@ -32,17 +32,19 @@ class ProductModel
         return $products;
     }
 
-    public function createProduct(string $name, float $price, int $categoryId, int $companyId, bool $productIsKitchen): bool
+    public function createProduct(string $name, float $price, int $categoryId, int $companyId, bool $productIsKitchen): int|false
     {
         $stmt = $this->pdo->prepare("INSERT INTO product (product_name, product_price, category_id, company_id, product_flag, product_is_kitchen)
                                      VALUES (:name, :price, :category_id, :company_id, 'a', :product_is_kitchen)");
-        return $stmt->execute([
+        $executed = $stmt->execute([
             'name' => $name,
             'price' => $price,
             'category_id' => $categoryId,
             'company_id' => $companyId,
             'product_is_kitchen' => $productIsKitchen ? 1 : 0
         ]);
+
+        return $executed ? (int)$this->pdo->lastInsertId() : false;
     }
 
     public function updateProduct(int $productId, int $companyId, ?string $productName = null, ?float $productPrice = null, ?bool $productIsKitchen = null): bool