diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index ad49e2704f7bd7c6bc1c76d70b7931f65e0546a1..4426d80d004ff895a1e3ec1a1d30b6e9ee38daf7 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 363e346374c7abb90df47e63c190839cc67f1bd7..7410fab47731c84e669613486dae878d0398c637 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(); } }