From af056ee80a1b8ad6e4d1de18f8e691eb15e250c3 Mon Sep 17 00:00:00 2001 From: Jeremy Guiselin <jeremy.guiselin@student.ecp.fr> Date: Tue, 17 Jan 2017 14:57:58 +0100 Subject: [PATCH] read notifications --- .../Controller/DefaultController.php | 21 ++++++++++++--- .../NotificationOnDeviceRepository.php | 27 ++++++++----------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index ad49e27..4426d80 100644 --- a/src/ApiBundle/Controller/DefaultController.php +++ b/src/ApiBundle/Controller/DefaultController.php @@ -157,6 +157,12 @@ class DefaultController extends FOSRestController return $this->handleView($view); } + /** + * @param Request $request + * @return Response + * + * @Post("/save-purchase") + */ public function savePurchaseAction(Request $request) { $view = View::create(); @@ -207,23 +213,30 @@ class DefaultController extends FOSRestController /** * @param string $uuid - * + * @return Response * * GET Route annotation. * @Get("/notifications/{uuid}/read") */ public function readAllNotificationsAction(string $uuid) { + $view = View::create(); $device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]); + if ($device) { try { $this->getNotificationOnDeviceRepository()->readAllNotifications($device); + return $this->handleView($view); } catch (\Exception $e) { - + $view->setStatusCode(400); + $this->getLogger()->err($e->getMessage()); + return $this->handleView($view); } - } else { - } + + $view->setStatusCode(400); + $this->getLogger()->err('No device for this uuid'); + return $this->handleView($view); } private function getNotificationOnDeviceRepository() diff --git a/src/BackendBundle/Repository/NotificationOnDeviceRepository.php b/src/BackendBundle/Repository/NotificationOnDeviceRepository.php index 363e346..7410fab 100644 --- a/src/BackendBundle/Repository/NotificationOnDeviceRepository.php +++ b/src/BackendBundle/Repository/NotificationOnDeviceRepository.php @@ -16,10 +16,7 @@ use Doctrine\ORM\EntityRepository; class NotificationOnDeviceRepository extends EntityRepository { - public function findNotificationsByDevice( - array $notifications, - Device $device - ) + public function findNotificationsByDevice(array $notifications, Device $device) { return $this->createQueryBuilder('s') ->where('s.notification IN (:notifications)') @@ -32,10 +29,7 @@ class NotificationOnDeviceRepository extends EntityRepository ->getResult(); } - public function updateStatusOnAllDevices( - array $devices, - Notification $notification - ) + public function updateStatusOnAllDevices(array $devices, Notification $notification) { foreach ($devices as $device) { $notifOnDevice = new NotificationOnDevice(); @@ -49,13 +43,14 @@ class NotificationOnDeviceRepository extends EntityRepository public function readAllNotifications(Device $device) { - $qb = $this->createQueryBuilder('n'); - $qb - ->update('n') - ->set('n.status', NotificationOnDevice::STATUS_READ) - ->where('n.device = :device') - ->setParameter('device', $device); - - $qb->getQuery()->execute(); + $notifications = $this->findBy([ + 'device' => $device + ]); + + foreach ($notifications as $notification) { + $notification->setStatus(NotificationOnDevice::STATUS_READ); + } + + $this->getEntityManager()->flush(); } } -- GitLab