diff --git a/NotificationsBundle/Controller/MainController.php b/NotificationsBundle/Controller/MainController.php index d5668d7..7692056 100644 --- a/NotificationsBundle/Controller/MainController.php +++ b/NotificationsBundle/Controller/MainController.php @@ -2,11 +2,19 @@ namespace Bluesquare\NotificationsBundle\Controller; +use Bluesquare\NotificationsBundle\Entity\Notification; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +/** + * Oui, je throw des exceptions sans catch, c'est comme ça qu'on utilise ce framework proprement + * https://symfony.com/doc/current/controller.html#managing-errors-and-404-pages + * + * Class MainController + * @package Bluesquare\NotificationsBundle\Controller + */ class MainController extends AbstractController { - public function myUser() + public function getAll() { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); @@ -16,4 +24,62 @@ class MainController extends AbstractController $notifs = $notifssrv->getForUser($user, true); return $this->json($notifs); } + + public function delete($id) + { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $em = $this->getDoctrine()->getManager(); + $user = $this->getUser(); + $notif = $em->getRepository(Notification::class)->find($id); + + if (!$notif) + throw $this->createNotFoundException('Notification not found'); + + if ($notif->getUser() != $user) + throw $this->createAccessDeniedException(); + + $em->remove($notif); + $em->flush(); + + return $this->json(["message" => 'OK']); + } + + public function markSeen($id) + { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $em = $this->getDoctrine()->getManager(); + $user = $this->getUser(); + $notif = $em->getRepository(Notification::class)->find($id); + + if (!$notif) + throw $this->createNotFoundException('Notification not found'); + + if ($notif->getUser() != $user) + throw $this->createAccessDeniedException(); + + $notif->setSeenAt(new \DateTime()); + + $em->persist($notif); + $em->flush(); + + return $this->json(["message" => 'OK']); + + } + + public function test() + { + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $user = $this->getUser(); + $notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv'); + + $notif = new Notification(); + $notif->setTitle("Bonjour"); + $notif->setDescription("Skippy vous attend dans son antre"); + $notif->setData("le_nom_de_la_route_en_react"); + + $users = [$user]; // Peut contenir plein d'users + $notifssrv->notifyUsers($users, $notif); + + return $this->json(["message" => 'OK']); + } } \ No newline at end of file diff --git a/NotificationsBundle/Resources/config/routes.yaml b/NotificationsBundle/Resources/config/routes.yaml index 7bc7f22..2d03259 100644 --- a/NotificationsBundle/Resources/config/routes.yaml +++ b/NotificationsBundle/Resources/config/routes.yaml @@ -1,3 +1,18 @@ -bluesquare_notifications_bundle_notifications: - path: /my-user - controller: Bluesquare\NotificationsBundle\Controller\MainController::myUser +bluesquare_notifications_bundle_notifications_get: + path: /get + controller: Bluesquare\NotificationsBundle\Controller\MainController::getAll + methods: [GET] + +bluesquare_notifications_bundle_notifications_delete: + path: /delete/{id} + controller: Bluesquare\NotificationsBundle\Controller\MainController::delete + methods: [DELETE] + requirements: + id: '\d+' + +bluesquare_notifications_bundle_notifications_mark_seen: + path: /mark-seen/{id} + controller: Bluesquare\NotificationsBundle\Controller\MainController::markSeen + methods: [POST] + requirements: + id: '\d+' diff --git a/NotificationsBundle/Resources/config/routes_dev.yaml b/NotificationsBundle/Resources/config/routes_dev.yaml new file mode 100644 index 0000000..fe082d3 --- /dev/null +++ b/NotificationsBundle/Resources/config/routes_dev.yaml @@ -0,0 +1,3 @@ +bluesquare_notifications_bundle_notifications_test: + path: /add-dev + controller: Bluesquare\NotificationsBundle\Controller\MainController::test diff --git a/README.md b/README.md index f8fe63f..1ffe78c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Ajout de notifications pour les users (backend seulement) ## Restrictions Votre classe User doit absoluement être dans le namespace \App\Entity et s'appeler User, soit etre -\App\Entity\User. +`\App\Entity\User\`. ## Routes: @@ -18,17 +18,28 @@ notifications: resource: '@NotificationsBundle/Resources/config/routes.yaml' ``` -à votre config/routes.yaml +à votre `config/routes.yaml` + +Il existe aussi le fichier `routes_dev.yaml` qui ajoute le endpoint à `/add-dev` qui permet +d'ajouter une notification de test à l'utilisateur courant. Pour créer des notifs, il faut utiliser le service. Effectivement on ne veut pas permettre aux clients de spammer les routes de notifications ou lire les notifs de toute le monde. Vous pouvez ajouter des routes dans votre application pour plus de flexibilité. -* **GET** /notifications/my-user +* **GET** /get Récupère toutes les notifications de l'utilisateur courant. +* **DELETE** /delete/{id} + +Supprime la notification à l'id indiqué si elle appartient à l'user courant. + +* **POST** /mark-seen/{id} + +Marque la notifiquation à l'id indiqué comme lue si elle appartient à l'user courant. + ## Service La classe est `NotificationsService`. Toutes les méthodes sont commentées, veuillez vous