diff --git a/src/ApiBundle/Controller/DefaultController.php b/src/ApiBundle/Controller/DefaultController.php index 23298aee7931e4141b75afe9efe4e6192e49babe..f0232fa688e59e2107c1591bab0822a9ab7a1bbe 100644 --- a/src/ApiBundle/Controller/DefaultController.php +++ b/src/ApiBundle/Controller/DefaultController.php @@ -48,9 +48,7 @@ class DefaultController extends FOSRestController public function getLeaguesBySportAction(string $sport) { try { - $leagues = $this->getLeagueRepository()->findBy([ - 'sport' => $sport - ]); + $leagues = $this->getLeagueRepository()->findBySport($sport); $view = View::create() ->setStatusCode(200) ->setData($leagues); diff --git a/src/BackendBundle/Entity/League.php b/src/BackendBundle/Entity/League.php index a304bad8a019d1a78e31880ae006d88308ff3149..bc140ba268c0ee46e6796f37d3ef48b68a96588f 100644 --- a/src/BackendBundle/Entity/League.php +++ b/src/BackendBundle/Entity/League.php @@ -44,6 +44,21 @@ class League */ private $picture; + /** + * @var int + */ + private $predictionsNumber; + + /** + * @var \DateTime + */ + private $minDate; + + /** + * @var \DateTime + */ + private $maxDate; + /** * Get id * @@ -117,4 +132,52 @@ class League { $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; + } } diff --git a/src/BackendBundle/Entity/Transaction.php b/src/BackendBundle/Entity/Transaction.php new file mode 100644 index 0000000000000000000000000000000000000000..5ec215f2500ab9e9e910249de12f3502b8a12836 --- /dev/null +++ b/src/BackendBundle/Entity/Transaction.php @@ -0,0 +1,112 @@ +<?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; + } +} + diff --git a/src/BackendBundle/Repository/LeagueRepository.php b/src/BackendBundle/Repository/LeagueRepository.php index e3252f5b3ef076a506c850334fbc1cc35e9f8c78..3627b6ded404661a5cb14da3226dcf049e1e3790 100644 --- a/src/BackendBundle/Repository/LeagueRepository.php +++ b/src/BackendBundle/Repository/LeagueRepository.php @@ -3,6 +3,7 @@ namespace BackendBundle\Repository; use BackendBundle\Entity\League; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Query\Expr\Join; /** * LeagueRepository @@ -24,6 +25,46 @@ class LeagueRepository extends EntityRepository ]) !== 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) { $qb = $this->createQueryBuilder('l'); diff --git a/src/BackendBundle/Repository/TransactionRepository.php b/src/BackendBundle/Repository/TransactionRepository.php new file mode 100644 index 0000000000000000000000000000000000000000..746f9378f5020b30ee5c01c7a2fe38bdde0acfdd --- /dev/null +++ b/src/BackendBundle/Repository/TransactionRepository.php @@ -0,0 +1,13 @@ +<?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 +{ +}