ReportsGetController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Models\ReportsModel;
  6. use Respect\Validation\Validator as v;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class ReportsGetController
  9. {
  10. private ReportsModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new ReportsModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string) $request->getBody(), true) ?? [];
  18. try {
  19. v::key('company_id', v::intType()->positive())->assert($body);
  20. v::key('page', v::intType()->positive(), false)->assert($body);
  21. v::key('limit', v::intType()->positive(), false)->assert($body);
  22. } catch (ValidationException $e) {
  23. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
  24. }
  25. $companyId = (int) $body['company_id'];
  26. $page = isset($body['page']) ? (int) $body['page'] : null;
  27. $limit = isset($body['limit']) ? (int) $body['limit'] : null;
  28. $reports = $this->model->getReports($companyId, $page, $limit);
  29. if (!$reports) {
  30. return ResponseLib::sendFail("No Reports Found", [], "E_NO_DATA")->withStatus(204);
  31. }
  32. return ResponseLib::sendOk($reports);
  33. }
  34. }