* api implementation
* no auth yet * images renamed to reflect name-prefix changes * yearservice now gets years from judge data
This commit is contained in:
@@ -6,15 +6,22 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Awardee;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use DoctrineExpressiveModule\Hydrator\DoctrineObject;
|
||||
|
||||
class AwardeeManager
|
||||
{
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
/** @var DoctrineObject */
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
DoctrineObject $hydrator
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getAwardees(): ?array
|
||||
@@ -54,4 +61,50 @@ class AwardeeManager
|
||||
]);
|
||||
return $awardee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Awardee
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function create($data): Awardee
|
||||
{
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, new Awardee());
|
||||
$this->entityManager->persist($awardee);
|
||||
$this->entityManager->flush();
|
||||
return $awardee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param $data
|
||||
* @return Awardee
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function update(int $id, $data): Awardee
|
||||
{
|
||||
$awardee = $this->entityManager->getRepository(Awardee::class)->find($id);
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->hydrator->hydrate($data, $awardee);
|
||||
$this->entityManager->persist($awardee);
|
||||
$this->entityManager->flush();
|
||||
return $awardee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function delete(int $id): bool {
|
||||
if (null !== ($entity = $this->getAwardee($id))) {
|
||||
$this->entityManager->remove($entity);
|
||||
$this->entityManager->flush();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use DoctrineExpressiveModule\Hydrator\DoctrineObject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class AwardeeManagerFactory
|
||||
@@ -12,6 +13,7 @@ class AwardeeManagerFactory
|
||||
public function __invoke(ContainerInterface $container) : AwardeeManager
|
||||
{
|
||||
$entityManager = $container->get(EntityManager::class);
|
||||
return new AwardeeManager($entityManager);
|
||||
$hydrator = $container->get(DoctrineObject::class);
|
||||
return new AwardeeManager($entityManager, $hydrator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Judge;
|
||||
@@ -11,6 +13,7 @@ class JudgeManager
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
/** @var DoctrineObject */
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(
|
||||
@@ -38,10 +41,11 @@ class JudgeManager
|
||||
public function getJudgesByYear(int $year)
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j')
|
||||
return $qb->select('j,t')
|
||||
->from(Judge::class, 'j')
|
||||
->innerJoin('j.titles', 't')
|
||||
->where('t.year = :year')
|
||||
->orderBy('j.name', 'ASC')
|
||||
->setParameter('year', $year)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
@@ -89,4 +93,20 @@ class JudgeManager
|
||||
$this->entityManager->flush();
|
||||
return $judge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
if (null !== ($entity = $this->getJudge($id))) {
|
||||
$this->entityManager->remove($entity);
|
||||
$this->entityManager->flush();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,6 @@ class YearManager
|
||||
return $year['year'];
|
||||
}, $years);
|
||||
|
||||
$thisYear = date("Y");
|
||||
if (!in_array($thisYear, $filteredYears)) {
|
||||
array_unshift($filteredYears, $thisYear);
|
||||
}
|
||||
|
||||
return $filteredYears;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user