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

add additional information for league + transaction

parent 83df441a
Branches
No related tags found
No related merge requests found
...@@ -48,9 +48,7 @@ class DefaultController extends FOSRestController ...@@ -48,9 +48,7 @@ class DefaultController extends FOSRestController
public function getLeaguesBySportAction(string $sport) public function getLeaguesBySportAction(string $sport)
{ {
try { try {
$leagues = $this->getLeagueRepository()->findBy([ $leagues = $this->getLeagueRepository()->findBySport($sport);
'sport' => $sport
]);
$view = View::create() $view = View::create()
->setStatusCode(200) ->setStatusCode(200)
->setData($leagues); ->setData($leagues);
......
...@@ -44,6 +44,21 @@ class League ...@@ -44,6 +44,21 @@ class League
*/ */
private $picture; private $picture;
/**
* @var int
*/
private $predictionsNumber;
/**
* @var \DateTime
*/
private $minDate;
/**
* @var \DateTime
*/
private $maxDate;
/** /**
* Get id * Get id
* *
...@@ -117,4 +132,52 @@ class League ...@@ -117,4 +132,52 @@ class League
{ {
$this->picture = $picture; $this->picture = $picture;
} }
/**
* @return int
*/
public function getPredictionsNumber()
{
return $this->predictionsNumber;
}
/**
* @param int $predictionsNumber
*/
public function setPredictionsNumber($predictionsNumber)
{
$this->predictionsNumber = $predictionsNumber;
}
/**
* @return \DateTime
*/
public function getMinDate()
{
return $this->minDate;
}
/**
* @param \DateTime $minDate
*/
public function setMinDate($minDate)
{
$this->minDate = $minDate;
}
/**
* @return \DateTime
*/
public function getMaxDate()
{
return $this->maxDate;
}
/**
* @param \DateTime $maxDate
*/
public function setMaxDate($maxDate)
{
$this->maxDate = $maxDate;
}
} }
<?php
namespace BackendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Transaction
*
* @ORM\Table(name="transaction")
* @ORM\Entity(repositoryClass="BackendBundle\Repository\TransactionRepository")
*/
class Transaction
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="store_transaction_id", type="string", length=255, unique=true)
*/
private $storeTransactionId;
/**
* @var Device
*
* @ORM\ManyToOne(targetEntity="BackendBundle\Entity\Device")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $device;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set storeTransactionId
*
* @param string $storeTransactionId
*
* @return Transaction
*/
public function setStoreTransactionId($storeTransactionId)
{
$this->storeTransactionId = $storeTransactionId;
return $this;
}
/**
* Get storeTransactionId
*
* @return string
*/
public function getStoreTransactionId()
{
return $this->storeTransactionId;
}
/**
* @return Device
*/
public function getDevice()
{
return $this->device;
}
/**
* @param Device $device
*/
public function setDevice($device)
{
$this->device = $device;
}
/**
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* @param \DateTime $date
*/
public function setDate($date)
{
$this->date = $date;
}
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace BackendBundle\Repository; namespace BackendBundle\Repository;
use BackendBundle\Entity\League; use BackendBundle\Entity\League;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
/** /**
* LeagueRepository * LeagueRepository
...@@ -24,6 +25,46 @@ class LeagueRepository extends EntityRepository ...@@ -24,6 +25,46 @@ class LeagueRepository extends EntityRepository
]) !== null; ]) !== null;
} }
public function findBySport(string $sport)
{
$leagues = $this->findBy([
'sport' => $sport
]);
$qb = $this->getEntityManager()->getRepository('BackendBundle:Prediction')->createQueryBuilder('p');
$qb
->select('count(p.id) AS predictions, l.name AS name, min(p.date) as minDate, max(p.date) as maxDate')
->join('p.league', 'l', Join::WITH, 'p.league = l')
->where('l in (:leagues)')
->setParameter('leagues', $leagues)
->groupBy('l');
$result = $qb->getQuery()->getResult();
$predictions = [];
foreach ($result as $r) {
$predictions[$r['name']] = [
'number' => (int) $r['predictions'],
'min_date' => $r['minDate'],
'max_date' => $r['maxDate']
];
}
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']);
} else {
$league->setPredictionsNumber(0);
}
}
return $leagues;
}
public function deleteElements(array $ids) public function deleteElements(array $ids)
{ {
$qb = $this->createQueryBuilder('l'); $qb = $this->createQueryBuilder('l');
......
<?php
namespace BackendBundle\Repository;
/**
* TransactionRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class TransactionRepository extends \Doctrine\ORM\EntityRepository
{
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment