| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Psr\Http\Message\ServerRequestInterface;
- use Models\ReportsModel;
- use Respect\Validation\Validator as v;
- use Respect\Validation\Exceptions\ValidationException;
- class ReportsGetController
- {
- private ReportsModel $model;
- public function __construct()
- {
- $this->model = new ReportsModel();
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string) $request->getBody(), true) ?? [];
- try {
- v::key('company_id', v::intType()->positive())->assert($body);
- v::optional(v::key('page', v::intType()->positive()))->assert($body);
- v::optional(v::key('limit', v::intType()->positive()))->assert($body);
- } catch (ValidationException $e) {
- return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
- }
- $companyId = (int) $body['company_id'];
- $page = isset($body['page']) ? (int) $body['page'] : 1;
- $limit = isset($body['limit']) ? (int) $body['limit'] : 10;
- $reports = $this->model->getReports($companyId, $page, $limit);
- if (!$reports) {
- return ResponseLib::sendFail("No Reports Found", [], "E_NO_DATA")->withStatus(204);
- }
- return ResponseLib::sendOk($reports);
- }
- }
|