AnalyticsSentimentDashboardController.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\AnalyticsSentimentDashboardModel;
  5. use Models\UserModel;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. class AnalyticsSentimentDashboardController
  8. {
  9. private UserModel $userModel;
  10. private AnalyticsSentimentDashboardModel $analyticsSentimentDashboardModel;
  11. public function __construct()
  12. {
  13. $this->userModel = new UserModel();
  14. $this->analyticsSentimentDashboardModel = new AnalyticsSentimentDashboardModel();
  15. }
  16. public function __invoke(ServerRequestInterface $request)
  17. {
  18. $userId = (int) ($request->getAttribute('user_id') ?? 0);
  19. if ($userId <= 0) {
  20. return ResponseLib::sendFail('Unauthorized: Missing authenticated user', [], 'E_VALIDATE')->withStatus(401);
  21. }
  22. try {
  23. $companyId = $this->userModel->getCompanyIdByUserId($userId);
  24. if ($companyId === null) {
  25. return ResponseLib::sendFail('User not found', [], 'E_NOT_FOUND')->withStatus(404);
  26. }
  27. return ResponseLib::sendOk(
  28. $this->analyticsSentimentDashboardModel->getDashboardData($companyId, $request->getQueryParams())
  29. );
  30. } catch (\Throwable $e) {
  31. return ResponseLib::sendFail('Failed to load sentiment dashboard', [], 'E_GENERIC')->withStatus(500);
  32. }
  33. }
  34. }