ProductGetController.php 901 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\ProductModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class ProductGetController
  7. {
  8. private ProductModel $model;
  9. public function __construct()
  10. {
  11. $this->model = new ProductModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $body = json_decode((string)$request->getBody(), true) ?? [];
  16. $companyId = $body['company_id'] ?? null;
  17. if (!$companyId) {
  18. return ResponseLib::sendFail("Missing Company ID", [], "E_VALIDATE")->withStatus(401);
  19. }
  20. $products = $this->model->getProducts($companyId);
  21. if ($products) {
  22. return ResponseLib::sendOk($products);
  23. }
  24. return ResponseLib::sendFail("Failed to retrieve products", [], "E_DATABASE")->withStatus(500);
  25. }
  26. }