diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index f0232fa688e59e2107c1591bab0822a9ab7a1bbe..42d3e49d78eeb310d948d647ce4b634e10fe1b7d 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 bc140ba268c0ee46e6796f37d3ef48b68a96588f..311af2680fb083571f68505ae231509270a6ab4e 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 7b8ae0a75aeb545f88f4bb66ea0a6fcfb783ab59..619ae05913892cccba6d4d9ffb1a522adbf219ca 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 3627b6ded404661a5cb14da3226dcf049e1e3790..7e05c0d189576f34eda0c8a8ee7a6ceabfb96b13 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); }