<?php
namespace App\Controller\ApiV2\Residents;
use App\Controller\ApiV2\AbstractController;
use App\Repository\BuildingRepository;
use App\Repository\FlatRepository;
use App\Repository\NotificationRepository;
use App\Repository\PollRepository;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
class DashboardController extends AbstractController
{
private FlatRepository $flatRepository;
private NotificationRepository $notificationRepository;
private PollRepository $pollRepository;
/**
* @param BuildingRepository $buildingRepository
* @param FlatRepository $flatRepository
* @param NotificationRepository $notificationRepository
* @param PollRepository $pollRepository
*/
public function __construct(
BuildingRepository $buildingRepository,
FlatRepository $flatRepository,
NotificationRepository $notificationRepository,
PollRepository $pollRepository
) {
parent::__construct($buildingRepository);
$this->flatRepository = $flatRepository;
$this->notificationRepository = $notificationRepository;
$this->pollRepository = $pollRepository;
}
/**
* @Rest\Get("/", name="residents_dashobard")
* @param Request $request
* @param int $id
* @return array<string, mixed>
*/
public function getLastTwoNotificationsAndPolls(Request $request, int $id): array
{
$flat = $this->flatRepository->find($id);
return [
'data' => [
'polls' => $this->pollRepository->getLastTwoActiveBuildingPolls($flat->getBuilding()),
'notifications' => $this->notificationRepository->getLastNotificationsByBuilding(
$flat->getBuilding(),
2
)
]
];
}
}