Merge branch 'homepage/rewrite' of gitlab.com:Chill-Projet/chill-bundles into homepage/rewrite

This commit is contained in:
2022-01-28 09:56:52 +01:00
5 changed files with 74 additions and 3 deletions

View File

@@ -17,9 +17,11 @@ use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\NotificationRepository;
use Chill\MainBundle\Security\Authorization\NotificationVoter;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\MainBundle\Serializer\Model\Counter;
use Doctrine\ORM\EntityManagerInterface;
use RuntimeException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
@@ -74,16 +76,24 @@ class NotificationApiController
/**
* @Route("/my/unread")
*/
public function myUnreadNotifications(): JsonResponse
public function myUnreadNotifications(Request $request): JsonResponse
{
$total = $this->notificationRepository->countUnreadByUser($this->security->getUser());
if ($request->query->getBoolean('countOnly')) {
return new JsonResponse(
$this->serializer->serialize(new Counter($total), 'json', ['groups' => ['read']]),
JsonResponse::HTTP_OK,
[],
true
);
}
$paginator = $this->paginatorFactory->create($total);
$notifications = $this->notificationRepository->findUnreadByUser(
$this->security->getUser(),
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
dump($notifications);
$collection = new Collection($notifications, $paginator);
return new JsonResponse(

View File

@@ -0,0 +1,31 @@
<?php
namespace Chill\MainBundle\Serializer\Model;
class Counter implements \JsonSerializable
{
private int $counter;
public function __construct(?int $counter)
{
$this->counter = $counter;
}
public function getCounter(): ?int
{
return $this->counter;
}
public function setCounter(?int $counter): Counter
{
$this->counter = $counter;
return $this;
}
public function jsonSerialize()
{
return ['count' => $this->counter];
}
}