UserGetController.php 985 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\UserModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Validator as val;
  7. use Respect\Validation\Exceptions\ValidationException;
  8. class UserGetController
  9. {
  10. private UserModel $model;
  11. public function __construct()
  12. {
  13. $this->model = new UserModel();
  14. }
  15. public function __invoke(ServerRequestInterface $request)
  16. {
  17. $body = json_decode((string)$request->getBody(), true) ?? [];
  18. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  19. if ($companyId <= 0) {
  20. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  21. }
  22. $users = $this->model->getUsersByCompany($companyId);
  23. return $users
  24. ? ResponseLib::sendOk($users)
  25. : ResponseLib::sendFail("User Not Found", [], "E_DATABASE")->withStatus(204);
  26. }
  27. }