50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\User;
|
|
use App\Repository\NotificationRepository;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
class NotificationController extends AbstractController
|
|
{
|
|
#[Route('/api/notifications', methods: ['GET'])]
|
|
#[IsGranted('ROLE_USER')]
|
|
public function index(NotificationRepository $notificationRepository): JsonResponse
|
|
{
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
|
|
$notifications = $notificationRepository->findRecentForUser($user);
|
|
$unreadCount = $notificationRepository->countUnreadForUser($user);
|
|
|
|
return $this->json([
|
|
'unreadCount' => $unreadCount,
|
|
'notifications' => array_map(fn ($n) => [
|
|
'id' => $n->getId(),
|
|
'message' => $n->getMessage(),
|
|
'read' => $n->isRead(),
|
|
'createdAt' => $n->getCreatedAt()->format('c'),
|
|
], $notifications),
|
|
]);
|
|
}
|
|
|
|
#[Route('/api/notifications/read', methods: ['POST'])]
|
|
#[IsGranted('ROLE_USER')]
|
|
public function markRead(NotificationRepository $notificationRepository): JsonResponse
|
|
{
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
|
|
$notificationRepository->markAllReadForUser($user);
|
|
|
|
return $this->json(null, Response::HTTP_NO_CONTENT);
|
|
}
|
|
}
|