Skip to content
Snippets Groups Projects
Commit 426460a6 authored by Jeremy Guiselin's avatar Jeremy Guiselin
Browse files

add additional information for league + lock all leagues when register device...

add additional information for league + lock all leagues when register device for the first time + unread notifications
parent fe4b5218
No related branches found
No related tags found
No related merge requests found
...@@ -39,16 +39,18 @@ class DefaultController extends FOSRestController ...@@ -39,16 +39,18 @@ class DefaultController extends FOSRestController
} }
/** /**
* @param string $uuid
* @param string $sport * @param string $sport
* @return Response * @return Response
* *
* GET Route annotation. * GET Route annotation.
* @Get("/leagues/{sport}") * @Get("/{uuid}/leagues/{sport}")
*/ */
public function getLeaguesBySportAction(string $sport) public function getLeaguesBySportAction(string $uuid, string $sport)
{ {
try { try {
$leagues = $this->getLeagueRepository()->findBySport($sport); $device = $this->getDeviceRepository()->findOneBy(['uuid' => $uuid]);
$leagues = $this->getLeagueRepository()->findBySport($sport, $device);
$view = View::create() $view = View::create()
->setStatusCode(200) ->setStatusCode(200)
->setData($leagues); ->setData($leagues);
......
...@@ -59,6 +59,11 @@ class League ...@@ -59,6 +59,11 @@ class League
*/ */
private $maxDate; private $maxDate;
/**
* @var string
*/
private $deviceStatus;
/** /**
* Get id * Get id
* *
...@@ -180,4 +185,20 @@ class League ...@@ -180,4 +185,20 @@ class League
{ {
$this->maxDate = $maxDate; $this->maxDate = $maxDate;
} }
/**
* @return string
*/
public function getDeviceStatus()
{
return $this->deviceStatus;
}
/**
* @param string $deviceStatus
*/
public function setDeviceStatus($deviceStatus)
{
$this->deviceStatus = $deviceStatus;
}
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace BackendBundle\Repository; namespace BackendBundle\Repository;
use BackendBundle\Entity\Device; use BackendBundle\Entity\Device;
use BackendBundle\Entity\LeagueOnDevice;
use BackendBundle\Entity\NotificationOnDevice;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
/** /**
...@@ -18,10 +20,13 @@ class DeviceRepository extends EntityRepository ...@@ -18,10 +20,13 @@ class DeviceRepository extends EntityRepository
public function hydrate(array $data) public function hydrate(array $data)
{ {
$device = new Device(); $device = new Device();
$em = $this->getEntityManager();
$device->setUuid($data['uuid']); $device->setUuid($data['uuid']);
$this->updateChangingInfo($data, $device); $this->updateChangingInfo($data, $device);
$this->_em->persist($device); $em->persist($device);
$this->_em->flush(); $this->lockLeagues($device);
$this->unreadNotifications($device);
$em->flush();
} }
/** /**
...@@ -47,4 +52,32 @@ class DeviceRepository extends EntityRepository ...@@ -47,4 +52,32 @@ class DeviceRepository extends EntityRepository
->setLocale($data['locale']) ->setLocale($data['locale'])
->setVersion($data['version']); ->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);
}
}
} }
<?php <?php
namespace BackendBundle\Repository; namespace BackendBundle\Repository;
use BackendBundle\Entity\Device;
use BackendBundle\Entity\League; use BackendBundle\Entity\League;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
...@@ -25,7 +26,7 @@ class LeagueRepository extends EntityRepository ...@@ -25,7 +26,7 @@ class LeagueRepository extends EntityRepository
]) !== null; ]) !== null;
} }
public function findBySport(string $sport) public function findBySport(string $sport, Device $device)
{ {
$leagues = $this->findBy([ $leagues = $this->findBy([
'sport' => $sport 'sport' => $sport
...@@ -40,10 +41,23 @@ class LeagueRepository extends EntityRepository ...@@ -40,10 +41,23 @@ class LeagueRepository extends EntityRepository
->setParameter('leagues', $leagues) ->setParameter('leagues', $leagues)
->groupBy('l'); ->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 = []; $predictions = [];
foreach ($result as $r) { foreach ($leagueResult as $r) {
$predictions[$r['name']] = [ $predictions[$r['name']] = [
'number' => (int) $r['predictions'], 'number' => (int) $r['predictions'],
'min_date' => $r['minDate'], 'min_date' => $r['minDate'],
...@@ -51,12 +65,17 @@ class LeagueRepository extends EntityRepository ...@@ -51,12 +65,17 @@ class LeagueRepository extends EntityRepository
]; ];
} }
foreach ($leagueOnDeviceResult as $r) {
$predictions[$r['name']]['status'] = $r['status'];
}
foreach ($leagues as $league) { foreach ($leagues as $league) {
if (isset ($predictions[$league->getName()])) { if (isset ($predictions[$league->getName()])) {
$prediction = $predictions[$league->getName()]; $prediction = $predictions[$league->getName()];
$league->setPredictionsNumber($prediction['number']); $league->setPredictionsNumber($prediction['number']);
$league->setMinDate($prediction['min_date']); $league->setMinDate($prediction['min_date']);
$league->setMaxDate($prediction['max_date']); $league->setMaxDate($prediction['max_date']);
$league->setDeviceStatus($prediction['status']);
} else { } else {
$league->setPredictionsNumber(0); $league->setPredictionsNumber(0);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment