| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?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);
- }
- }
|