src/Notification/Controller/NotificationsController.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Notification\Controller;
  3. use App\Authentication\Entity\User;
  4. use App\Framework\Controller\APIController;
  5. use App\Framework\Serializer\APISerializerGroup;
  6. use App\Notification\Entity\Notification;
  7. use App\Notification\Service\NotificationService;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. #[Route(path'/api/users/{id}/notifications')]
  11. #[Security("is_granted('IS_AUTHENTICATED_FULLY')")]
  12. class NotificationsController extends APIController
  13. {
  14.     /**
  15.      * @return Notification[]
  16.      */
  17.     #[Route(path''methods: ['GET'])]
  18.     #[APISerializerGroup('user:default''default')]
  19.     public function getNotificationsAction(User $userNotificationService $notificationService): array
  20.     {
  21.         return $notificationService->getNotificationsByUser($user);
  22.     }
  23.     /**
  24.      * @return Notification[]
  25.      */
  26.     #[Route(path'/{ids}'methods: ['PATCH'])]
  27.     public function readNotificationsAction(User $userstring $idsNotificationService $notificationService): array
  28.     {
  29.         // Get array of ids that we want to pack
  30.         $ids array_map('intval'explode(','$ids));
  31.         // Mark them as read
  32.         $notifications $notificationService->getNotificationsByIds($ids);
  33.         $notificationService->markNotificationsAsRead($notifications);
  34.         // And re-fetch to return a view
  35.         return $notificationService->getNotificationsByUser($user);
  36.     }
  37. }