From 426460a61ef709c1987528b5dc99c7339eb2d322 Mon Sep 17 00:00:00 2001
From: Jeremy Guiselin <jeremy.guiselin@student.ecp.fr>
Date: Thu, 12 Jan 2017 18:45:12 +0100
Subject: [PATCH] add additional information for league + lock all leagues when
 register device for the first time + unread notifications

---
 .../Controller/DefaultController.php          |  8 ++--
 src/BackendBundle/Entity/League.php           | 21 +++++++++++
 .../Repository/DeviceRepository.php           | 37 ++++++++++++++++++-
 .../Repository/LeagueRepository.php           | 25 +++++++++++--
 4 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php
index f0232fa..42d3e49 100644
--- a/src/ApiBundle/Controller/DefaultController.php
+++ b/src/ApiBundle/Controller/DefaultController.php
@@ -39,16 +39,18 @@ class DefaultController extends FOSRestController
     }
 
     /**
+     * @param string $uuid
      * @param string $sport
      * @return Response
      *
      * GET Route annotation.
-     * @Get("/leagues/{sport}")
+     * @Get("/{uuid}/leagues/{sport}")
      */
-    public function getLeaguesBySportAction(string $sport)
+    public function getLeaguesBySportAction(string $uuid, string $sport)
     {
         try {
-            $leagues = $this->getLeagueRepository()->findBySport($sport);
+            $device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]);
+            $leagues = $this->getLeagueRepository()->findBySport($sport, $device);
             $view = View::create()
                 ->setStatusCode(200)
                 ->setData($leagues);
diff --git a/src/BackendBundle/Entity/League.php b/src/BackendBundle/Entity/League.php
index bc140ba..311af26 100644
--- a/src/BackendBundle/Entity/League.php
+++ b/src/BackendBundle/Entity/League.php
@@ -59,6 +59,11 @@ class League
      */
     private $maxDate;
 
+    /**
+     * @var string
+     */
+    private $deviceStatus;
+
     /**
      * Get id
      *
@@ -180,4 +185,20 @@ class League
     {
         $this->maxDate = $maxDate;
     }
+
+    /**
+     * @return string
+     */
+    public function getDeviceStatus()
+    {
+        return $this->deviceStatus;
+    }
+
+    /**
+     * @param string $deviceStatus
+     */
+    public function setDeviceStatus($deviceStatus)
+    {
+        $this->deviceStatus = $deviceStatus;
+    }
 }
diff --git a/src/BackendBundle/Repository/DeviceRepository.php b/src/BackendBundle/Repository/DeviceRepository.php
index 7b8ae0a..619ae05 100644
--- a/src/BackendBundle/Repository/DeviceRepository.php
+++ b/src/BackendBundle/Repository/DeviceRepository.php
@@ -2,6 +2,8 @@
 
 namespace BackendBundle\Repository;
 use BackendBundle\Entity\Device;
+use BackendBundle\Entity\LeagueOnDevice;
+use BackendBundle\Entity\NotificationOnDevice;
 use Doctrine\ORM\EntityRepository;
 
 /**
@@ -18,10 +20,13 @@ class DeviceRepository extends EntityRepository
     public function hydrate(array $data)
     {
         $device = new Device();
+        $em = $this->getEntityManager();
         $device->setUuid($data['uuid']);
         $this->updateChangingInfo($data, $device);
-        $this->_em->persist($device);
-        $this->_em->flush();
+        $em->persist($device);
+        $this->lockLeagues($device);
+        $this->unreadNotifications($device);
+        $em->flush();
     }
 
     /**
@@ -47,4 +52,32 @@ class DeviceRepository extends EntityRepository
             ->setLocale($data['locale'])
             ->setVersion($data['version']);
     }
+
+    private function lockLeagues(Device $device)
+    {
+        $em = $this->getEntityManager();
+
+        $leagues = $em->getRepository('BackendBundle:League')->findAll();
+
+        foreach ($leagues as $league) {
+            $leagueDevice = new LeagueOnDevice();
+            $leagueDevice->setDevice($device);
+            $leagueDevice->setLeague($league);
+            $em->persist($leagueDevice);
+        }
+    }
+
+    private function unreadNotifications(Device $device)
+    {
+        $em = $this->getEntityManager();
+
+        $notifications = $em->getRepository('BackendBundle:Notification')->findAll();
+
+        foreach ($notifications as $notification) {
+            $notificationDevice = new NotificationOnDevice();
+            $notificationDevice->setDevice($device);
+            $notificationDevice->setNotification($notification);
+            $em->persist($notificationDevice);
+        }
+    }
 }
diff --git a/src/BackendBundle/Repository/LeagueRepository.php b/src/BackendBundle/Repository/LeagueRepository.php
index 3627b6d..7e05c0d 100644
--- a/src/BackendBundle/Repository/LeagueRepository.php
+++ b/src/BackendBundle/Repository/LeagueRepository.php
@@ -1,6 +1,7 @@
 <?php
 
 namespace BackendBundle\Repository;
+use BackendBundle\Entity\Device;
 use BackendBundle\Entity\League;
 use Doctrine\ORM\EntityRepository;
 use Doctrine\ORM\Query\Expr\Join;
@@ -25,7 +26,7 @@ class LeagueRepository extends EntityRepository
         ]) !== null;
     }
 
-    public function findBySport(string $sport)
+    public function findBySport(string $sport, Device $device)
     {
         $leagues = $this->findBy([
             'sport' => $sport
@@ -40,10 +41,23 @@ class LeagueRepository extends EntityRepository
             ->setParameter('leagues', $leagues)
             ->groupBy('l');
 
-        $result = $qb->getQuery()->getResult();
+        $leagueResult = $qb->getQuery()->getResult();
+
+        $qb = $this->getEntityManager()->getRepository('BackendBundle:LeagueOnDevice')->createQueryBuilder('ld');
+        $qb
+            ->select('ld.status AS status, l.name AS name')
+            ->join('ld.league', 'l', Join::WITH, 'ld.league = l')
+            ->where('l in (:leagues)')
+            ->setParameter('leagues', $leagues)
+            ->andWhere('ld.device = :device')
+            ->setParameter('device', $device)
+            ->groupBy('l');
+
+        $leagueOnDeviceResult = $qb->getQuery()->getResult();
+
         $predictions = [];
 
-        foreach ($result as $r) {
+        foreach ($leagueResult as $r) {
             $predictions[$r['name']] = [
                 'number' => (int) $r['predictions'],
                 'min_date' => $r['minDate'],
@@ -51,12 +65,17 @@ class LeagueRepository extends EntityRepository
             ];
         }
 
+        foreach ($leagueOnDeviceResult as $r) {
+            $predictions[$r['name']]['status'] = $r['status'];
+        }
+
         foreach ($leagues as $league) {
             if (isset ($predictions[$league->getName()])) {
                 $prediction = $predictions[$league->getName()];
                 $league->setPredictionsNumber($prediction['number']);
                 $league->setMinDate($prediction['min_date']);
                 $league->setMaxDate($prediction['max_date']);
+                $league->setDeviceStatus($prediction['status']);
             } else {
                 $league->setPredictionsNumber(0);
             }
-- 
GitLab