This commit is contained in:
PaulCombal
2019-03-01 16:02:12 +01:00
parent a98aa394ac
commit 4af65e72f7
4 changed files with 102 additions and 7 deletions

View File

@@ -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']);
}
}

View File

@@ -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+'

View File

@@ -0,0 +1,3 @@
bluesquare_notifications_bundle_notifications_test:
path: /add-dev
controller: Bluesquare\NotificationsBundle\Controller\MainController::test