|
@@ -1,20 +1,23 @@
|
|
|
<?php
|
|
<?php
|
|
|
-
|
|
|
|
|
namespace Controllers;
|
|
namespace Controllers;
|
|
|
|
|
|
|
|
use Libs\ResponseLib;
|
|
use Libs\ResponseLib;
|
|
|
|
|
+use Models\CategoryModel;
|
|
|
use Models\ProductModel;
|
|
use Models\ProductModel;
|
|
|
|
|
+use Models\DescriptionModel;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
use Respect\Validation\Validator as v;
|
|
use Respect\Validation\Validator as v;
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
|
|
|
|
class ProductCreateController
|
|
class ProductCreateController
|
|
|
{
|
|
{
|
|
|
- private ProductModel $model;
|
|
|
|
|
|
|
+ private ProductModel $productModel;
|
|
|
|
|
+ private DescriptionModel $descriptionModel;
|
|
|
|
|
|
|
|
public function __construct()
|
|
public function __construct()
|
|
|
{
|
|
{
|
|
|
- $this->model = new ProductModel();
|
|
|
|
|
|
|
+ $this->productModel = new ProductModel();
|
|
|
|
|
+ $this->descriptionModel = new DescriptionModel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function __invoke(ServerRequestInterface $request)
|
|
public function __invoke(ServerRequestInterface $request)
|
|
@@ -23,11 +26,16 @@ class ProductCreateController
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
v::key('company_id', v::intType()->positive())
|
|
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) {
|
|
} catch (ValidationException $e) {
|
|
|
return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
|
|
return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
|
|
|
}
|
|
}
|
|
@@ -37,8 +45,9 @@ class ProductCreateController
|
|
|
$productPrice = (float) $body['product_price'];
|
|
$productPrice = (float) $body['product_price'];
|
|
|
$categoryId = $body['category_id'];
|
|
$categoryId = $body['category_id'];
|
|
|
$productIsKitchen = $body['product_is_kitchen'] ?? false;
|
|
$productIsKitchen = $body['product_is_kitchen'] ?? false;
|
|
|
|
|
+ $descriptionText = $body['description_text'] ?? null;
|
|
|
|
|
|
|
|
- $created = $this->model->createProduct(
|
|
|
|
|
|
|
+ $productId = $this->productModel->createProduct(
|
|
|
$productName,
|
|
$productName,
|
|
|
$productPrice,
|
|
$productPrice,
|
|
|
(int)$categoryId,
|
|
(int)$categoryId,
|
|
@@ -46,8 +55,22 @@ class ProductCreateController
|
|
|
$productIsKitchen
|
|
$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]);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|