DocumentListController.php 988 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\DocumentModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class DocumentListController
  7. {
  8. private DocumentModel $documentModel;
  9. public function __construct()
  10. {
  11. $this->documentModel = new DocumentModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  16. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  17. if ($userId <= 0 || $companyId <= 0) {
  18. return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401);
  19. }
  20. try {
  21. $documents = $this->documentModel->listByUserId($userId);
  22. } catch (\Throwable $e) {
  23. return ResponseLib::sendFail('Database error: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  24. }
  25. return ResponseLib::sendOk(['documents' => $documents], 'S_OK');
  26. }
  27. }