TableGetController.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\TableModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as v;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class TableGetController
  9. {
  10. private TableModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new TableModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string)$request->getBody(), true) ?? [];
  18. // Validação do company_id
  19. try {
  20. v::key('company_id', v::intType()->positive())
  21. ->assert($body);
  22. } catch (ValidationException $e) {
  23. return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(422);
  24. }
  25. $companyId = $body['company_id'];
  26. $tables = $this->model->getTables((int)$companyId);
  27. if ($tables) {
  28. return ResponseLib::sendOk($tables);
  29. }
  30. return ResponseLib::sendFail("Table Not Found", [], "E_DATABASE")->withStatus(404);
  31. }
  32. }