CategoryAddProductController.php 901 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CategoryModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class CategoryAddProductController
  7. {
  8. private CategoryModel $model;
  9. public function __construct()
  10. {
  11. $this->model = new CategoryModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $body = json_decode((string)$request->getBody(), true) ?? [];
  16. $companyId = $body['company_id'] ?? null;
  17. $added = $this->model->addProductToCategory(
  18. $body['product_name'],
  19. $body['product_price'],
  20. $body['category_name'],
  21. $companyId
  22. );
  23. return $added ? ResponseLib::sendOk(['product_added' => true]) : ResponseLib::sendFail("Category Not Found", [], "E_VALIDATE")->withStatus(404);
  24. }
  25. }