* initial api stuff
This commit is contained in:
@@ -41,15 +41,19 @@ class ConfigProvider
|
||||
Handler\AwardeeHandler::class => Handler\AwardeeHandlerFactory::class,
|
||||
Handler\JudgesHandler::class => Handler\JudgesHandlerFactory::class,
|
||||
Handler\ProfileHandler::class => Handler\ProfileHandlerFactory::class,
|
||||
|
||||
Handler\AwardeeRedirectHandler::class => Handler\AwardeeRedirectHandlerFactory::class,
|
||||
Handler\PrizeRedirectHandler::class => Handler\PrizeRedirectHandlerFactory::class,
|
||||
|
||||
Handler\Api\AwardeeHandler::class => Handler\Api\AwardeeHandlerFactory::class,
|
||||
Handler\Api\JudgesHandler::class => Handler\Api\JudgesHandlerFactory::class,
|
||||
Handler\Api\YearsHandler::class => Handler\Api\YearsHandlerFactory::class,
|
||||
|
||||
Plates\StringExtension::class => Plates\StringExtensionFactory::class,
|
||||
Plates\NavigationExtension::class => Plates\NavigationExtensionFactory::class,
|
||||
|
||||
Service\AwardeeManager::class => Service\AwardeeManagerFactory::class,
|
||||
Service\JudgeManager::class => Service\JudgeManagerFactory::class,
|
||||
Service\YearManager::class => Service\YearManagerFactory::class,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ class Awardee implements JsonSerializable
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'year' => $this->getYear(),
|
||||
'name' => $this->getYear(),
|
||||
'name' => $this->getName(),
|
||||
'text' => $this->getText(),
|
||||
'imageLabel' => $this->getImageLabel(),
|
||||
'slug' => $this->getSlug(),
|
||||
|
||||
@@ -34,12 +34,6 @@ class Judge implements JsonSerializable
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="title", type="text", length=1500)
|
||||
* @var string
|
||||
*/
|
||||
private $title;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="slug", type="string", length=250)
|
||||
* @Gedmo\Slug(fields={"name"})
|
||||
@@ -48,14 +42,15 @@ class Judge implements JsonSerializable
|
||||
private $slug;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToMany(targetEntity="Year", mappedBy="judges")
|
||||
* @var Collection|Year[]
|
||||
* @ORM\OneToMany(targetEntity="JudgeTitle", mappedBy="judge")
|
||||
* @var Collection|JudgeTitle[]
|
||||
*/
|
||||
private $years;
|
||||
private $titles;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->years = new ArrayCollection();
|
||||
$this->titles = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,24 +89,6 @@ class Judge implements JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Judge
|
||||
*/
|
||||
public function setTitle(string $title): Judge
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -131,48 +108,44 @@ class Judge implements JsonSerializable
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Year[]|Collection
|
||||
* @return JudgeTitle[]|Collection
|
||||
*/
|
||||
public function getYears(): ?Collection
|
||||
public function getTitles()
|
||||
{
|
||||
return $this->years;
|
||||
return $this->titles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Year $year
|
||||
* @param JudgeTitle $title
|
||||
* @return Judge
|
||||
*/
|
||||
public function addYear(Year $year): Judge
|
||||
public function addTitle(JudgeTitle $title): Judge
|
||||
{
|
||||
if ($this->years->contains($year)) {
|
||||
return $this;
|
||||
if(!$this->titles->contains($title)) {
|
||||
$this->titles->add($title);
|
||||
}
|
||||
$this->years->add($year);
|
||||
$year->addJudge($this);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Year $year
|
||||
* @param JudgeTitle $title
|
||||
* @return Judge
|
||||
*/
|
||||
public function removeYear(Year $year): Judge
|
||||
public function removeTitle(JudgeTitle $title): Judge
|
||||
{
|
||||
if (!$this->years->contains($year)) {
|
||||
return $this;
|
||||
if($this->titles->contains($title)) {
|
||||
$this->titles->removeElement($title);
|
||||
}
|
||||
$this->years->removeElement($year);
|
||||
$year->removeJudge($this);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Year[]|Collection $years
|
||||
* @param JudgeTitle[]|Collection $titles
|
||||
* @return Judge
|
||||
*/
|
||||
public function setYears(?Collection $years)
|
||||
public function setTitles($titles)
|
||||
{
|
||||
$this->years = $years;
|
||||
$this->titles = $titles;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -184,7 +157,7 @@ class Judge implements JsonSerializable
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'name' => $this->getName(),
|
||||
'title' => $this->getTitle(),
|
||||
'titles' => $this->getTitles()->getValues(),
|
||||
'slug' => $this->getSlug(),
|
||||
];
|
||||
}
|
||||
|
||||
131
src/App/Entity/JudgeTitle.php
Normal file
131
src/App/Entity/JudgeTitle.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(
|
||||
* name="judge_titles",
|
||||
* indexes={
|
||||
* @ORM\Index(name="jt_year_idx", columns={"year"})
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class JudgeTitle implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="year", type="integer")
|
||||
* @var int
|
||||
*/
|
||||
private $year;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="title", type="text", length=1500)
|
||||
* @var string
|
||||
*/
|
||||
private $title;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="Judge", inversedBy="titles")
|
||||
* @ORM\JoinColumn(name="title_id", referencedColumnName="id")
|
||||
* @var Judge
|
||||
*/
|
||||
private $judge;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return JudgeTitle
|
||||
*/
|
||||
public function setId(int $id): JudgeTitle
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getYear(): int
|
||||
{
|
||||
return $this->year;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return JudgeTitle
|
||||
*/
|
||||
public function setYear(int $year): JudgeTitle
|
||||
{
|
||||
$this->year = $year;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return JudgeTitle
|
||||
*/
|
||||
public function setTitle(string $title): JudgeTitle
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Judge
|
||||
*/
|
||||
public function getJudge(): Judge
|
||||
{
|
||||
return $this->judge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Judge $judge
|
||||
* @return JudgeTitle
|
||||
*/
|
||||
public function setJudge(Judge $judge): JudgeTitle
|
||||
{
|
||||
$this->judge = $judge;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'year' => $this->getYear(),
|
||||
'title' => $this->getTitle(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(
|
||||
* name="years",
|
||||
* indexes={
|
||||
* @ORM\Index(name="y_year_idx", columns={"year"})
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Year implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="year", type="integer", nullable=false)
|
||||
* @var int
|
||||
*/
|
||||
private $year;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToMany(targetEntity="Judge", inversedBy="years")
|
||||
* @ORM\JoinTable(
|
||||
* name="years_judges",
|
||||
* joinColumns={
|
||||
* @ORM\JoinColumn(name="year_id", referencedColumnName="id")
|
||||
* },
|
||||
* inverseJoinColumns={
|
||||
* @ORM\JoinColumn(name="judge_id", referencedColumnName="id")
|
||||
* }
|
||||
* )
|
||||
* @var \Doctrine\Common\Collections\Collection|Judge[]
|
||||
*/
|
||||
private $judges;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->judges = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Year
|
||||
*/
|
||||
public function setId(int $id): Year
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getYear(): int
|
||||
{
|
||||
return $this->year;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return Year
|
||||
*/
|
||||
public function setYear(int $year): Year
|
||||
{
|
||||
$this->year = $year;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Judge[]|Collection
|
||||
*/
|
||||
public function getJudges(): ?Collection
|
||||
{
|
||||
return $this->judges;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Judge $judge
|
||||
* @return Year
|
||||
*/
|
||||
public function addJudge(Judge $judge): Year
|
||||
{
|
||||
if ($this->judges->contains($judge)) {
|
||||
return $this;
|
||||
}
|
||||
$this->judges->add($judge);
|
||||
$judge->addYear($this);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Judge $judge
|
||||
* @return Year
|
||||
*/
|
||||
public function removeJudge(Judge $judge): Year
|
||||
{
|
||||
if (!$this->judges->contains($judge)) {
|
||||
return $this;
|
||||
}
|
||||
$this->judges->removeElement($judge);
|
||||
$judge->removeYear($this);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Judge[]|Collection $judges
|
||||
* @return Year
|
||||
*/
|
||||
public function setJudges(?Collection $judges): Year
|
||||
{
|
||||
$this->judges = $judges;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'year' => $this->getYear(),
|
||||
'judges' => $this->getJudges()->getValues(),
|
||||
];
|
||||
}
|
||||
}
|
||||
161
src/App/Handler/Api/AbstractCrudHandler.php
Normal file
161
src/App/Handler/Api/AbstractCrudHandler.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Zend\Diactoros\Response\EmptyResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Json\Json;
|
||||
|
||||
abstract class AbstractCrudHandler implements RequestHandlerInterface
|
||||
{
|
||||
const IDENTIFIER_NAME = 'id';
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$requestMethod = strtoupper($request->getMethod());
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
|
||||
switch ($requestMethod) {
|
||||
case 'GET':
|
||||
return isset($id)
|
||||
? $this->get($request)
|
||||
: $this->getList($request);
|
||||
case 'POST':
|
||||
return $this->create($request);
|
||||
case 'PUT':
|
||||
return $this->update($request);
|
||||
case 'DELETE':
|
||||
return isset($id)
|
||||
? $this->delete($request)
|
||||
: $this->deleteList($request);
|
||||
case 'HEAD':
|
||||
return $this->head($request);
|
||||
case 'OPTIONS':
|
||||
return $this->options($request);
|
||||
case 'PATCH':
|
||||
return $this->patch($request);
|
||||
default:
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function create(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function update(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function delete(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function deleteList(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function head(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request)
|
||||
{
|
||||
return new EmptyResponse(200);
|
||||
}
|
||||
|
||||
public function patch(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
final protected function createResponse($data, $status = 200)
|
||||
{
|
||||
return new JsonResponse($data, $status);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @return array|object
|
||||
*/
|
||||
public function getRequestData(ServerRequestInterface $request)
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
|
||||
if (!empty($body)) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
return $this->parseRequestData(
|
||||
$request->getBody()->getContents(),
|
||||
$request->getHeaderLine('content-type')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $input
|
||||
* @param string $contentType
|
||||
* @return mixed
|
||||
*/
|
||||
private function parseRequestData($input, $contentType)
|
||||
{
|
||||
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
|
||||
$parser = $this->returnParserContentType($contentTypeParts[0]);
|
||||
|
||||
return $parser($input);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $contentType
|
||||
* @return callable
|
||||
*/
|
||||
private function returnParserContentType(string $contentType): callable
|
||||
{
|
||||
if ($contentType === 'application/x-www-form-urlencoded') {
|
||||
return function ($input) {
|
||||
parse_str($input, $data);
|
||||
return $data;
|
||||
};
|
||||
} elseif ($contentType === 'application/json') {
|
||||
return function ($input) {
|
||||
$jsonDecoder = new Json();
|
||||
try {
|
||||
return $jsonDecoder->decode($input, Json::TYPE_ARRAY);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} elseif ($contentType === 'multipart/form-data') {
|
||||
return function ($input) {
|
||||
return $input;
|
||||
};
|
||||
}
|
||||
|
||||
return function ($input) {
|
||||
return $input;
|
||||
};
|
||||
}
|
||||
}
|
||||
35
src/App/Handler/Api/AwardeeHandler.php
Normal file
35
src/App/Handler/Api/AwardeeHandler.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class AwardeeHandler extends AbstractCrudHandler
|
||||
{
|
||||
/** @var AwardeeManager */
|
||||
private $awardeeManager;
|
||||
|
||||
public function __construct(
|
||||
AwardeeManager $awardeeManager
|
||||
) {
|
||||
$this->awardeeManager = $awardeeManager;
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$entities = $this->awardeeManager->getAwardees();
|
||||
return new JsonResponse($entities);
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$entity = $this->awardeeManager->getAwardee((int)$id);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
}
|
||||
18
src/App/Handler/Api/AwardeeHandlerFactory.php
Normal file
18
src/App/Handler/Api/AwardeeHandlerFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\AwardeeManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AwardeeHandlerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container): RequestHandlerInterface
|
||||
{
|
||||
$awardeeManager = $container->get(AwardeeManager::class);
|
||||
return new AwardeeHandler($awardeeManager);
|
||||
}
|
||||
}
|
||||
70
src/App/Handler/Api/JudgesHandler.php
Normal file
70
src/App/Handler/Api/JudgesHandler.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\JudgeManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class JudgesHandler extends AbstractCrudHandler
|
||||
{
|
||||
/** @var JudgeManager */
|
||||
private $judgeManager;
|
||||
|
||||
public function __construct(
|
||||
JudgeManager $judgeManager
|
||||
) {
|
||||
$this->judgeManager = $judgeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function getList(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$entities = $this->judgeManager->getJudges();
|
||||
return new JsonResponse($entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$entity = $this->judgeManager->getJudge((int)$id);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function create(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$data = $this->getRequestData($request);
|
||||
$entity = $this->judgeManager->create($data);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return JsonResponse
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function update(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
$data = $this->getRequestData($request);
|
||||
$entity = $this->judgeManager->update((int)$id, $data);
|
||||
return new JsonResponse($entity);
|
||||
}
|
||||
}
|
||||
18
src/App/Handler/Api/JudgesHandlerFactory.php
Normal file
18
src/App/Handler/Api/JudgesHandlerFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\JudgeManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class JudgesHandlerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container) : RequestHandlerInterface
|
||||
{
|
||||
$judgeManager = $container->get(JudgeManager::class);
|
||||
return new JudgesHandler($judgeManager);
|
||||
}
|
||||
}
|
||||
32
src/App/Handler/Api/YearsHandler.php
Normal file
32
src/App/Handler/Api/YearsHandler.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\YearManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class YearsHandler extends AbstractCrudHandler
|
||||
{
|
||||
/** @var YearManager */
|
||||
private $yearManager;
|
||||
|
||||
public function __construct(
|
||||
YearManager $yearManager
|
||||
) {
|
||||
$this->yearManager = $yearManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function getList(ServerRequestInterface $request) : ResponseInterface
|
||||
{
|
||||
$judges = $this->yearManager->getYears();
|
||||
return new JsonResponse($judges);
|
||||
}
|
||||
}
|
||||
18
src/App/Handler/Api/YearsHandlerFactory.php
Normal file
18
src/App/Handler/Api/YearsHandlerFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler\Api;
|
||||
|
||||
use App\Service\YearManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class YearsHandlerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container) : RequestHandlerInterface
|
||||
{
|
||||
$yearManager = $container->get(YearManager::class);
|
||||
return new YearsHandler($yearManager);
|
||||
}
|
||||
}
|
||||
@@ -47,13 +47,6 @@ class AwardeeHandler implements RequestHandlerInterface
|
||||
$awardees = $this->awardeeManager->getAwardeesByYear((int)$year);
|
||||
$judges = $this->judgeManager->getJudgesByYear((int)$year);
|
||||
|
||||
// if (count($awardees) === 1) {
|
||||
// $url = $this->urlHelper->generate('awardee', [
|
||||
// 'slug' => $awardees[0]->getSlug(),
|
||||
// ]);
|
||||
// return new RedirectResponse($url);
|
||||
// }
|
||||
|
||||
return new HtmlResponse($this->template->render("app::awardees", [
|
||||
'year' => $year,
|
||||
'awardees' => $awardees,
|
||||
|
||||
@@ -17,6 +17,13 @@ class AwardeeManager
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
public function getAwardees(): ?array
|
||||
{
|
||||
return $this->entityManager->getRepository(Awardee::class)->findBy([], [
|
||||
'year' => 'DESC',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return Awardee[]
|
||||
@@ -28,6 +35,13 @@ class AwardeeManager
|
||||
]);
|
||||
}
|
||||
|
||||
public function getAwardee(int $id): ?Awardee
|
||||
{
|
||||
/** @var Awardee $awardee */
|
||||
$awardee = $this->entityManager->getRepository(Awardee::class)->find($id);
|
||||
return $awardee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @return Awardee|null
|
||||
|
||||
@@ -4,146 +4,89 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Judge;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use DoctrineExpressiveModule\Hydrator\DoctrineObject;
|
||||
|
||||
class JudgeManager
|
||||
{
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
DoctrineObject $hydrator
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getJudges()
|
||||
{
|
||||
// return [
|
||||
// [
|
||||
// 'image' => 'agnes_soos',
|
||||
// 'name' => 'Dr Ágnes Soós',
|
||||
// 'desc' => 'National Institute for Sports Medicine, Director General',
|
||||
// ], [
|
||||
// 'image' => 'balazs_nagy_lantos',
|
||||
// 'name' => 'Balázs Nagy Lantos',
|
||||
// 'desc' => 'Mensa HungarIQa, Former President',
|
||||
// ], [
|
||||
// 'image' => 'bertalan_mesko',
|
||||
// 'name' => 'Dr Bertalan Meskó',
|
||||
// 'desc' => 'Winner of GRAN PRIZE 2013, medical futurist, founder of Webicina',
|
||||
// ], [
|
||||
// 'image' => 'edit_nemeth',
|
||||
// 'name' => 'Dr Edit Németh',
|
||||
// 'desc' => 'ELTE Institute of Business Economics, Management and Business Law Faculty',
|
||||
// ], [
|
||||
// 'image' => 'erno_keszei',
|
||||
// 'name' => 'Prof. Ernő Keszei',
|
||||
// 'desc' => 'Eötvös Loránd University, Former Vice-Rector for Science, Research, and Innovation',
|
||||
// ], [
|
||||
// 'image' => 'gabor_kopek',
|
||||
// 'name' => 'Gábor Kopek',
|
||||
// 'desc' => 'Moholy-Nagy University of Art and Design Budapest, Former Rector',
|
||||
// ], [
|
||||
// 'image' => 'gabor_nemeth',
|
||||
// 'name' => 'Dr Gábor Németh',
|
||||
// 'desc' => 'Hungarian Intellectual Property Office, Director',
|
||||
// ], [
|
||||
// 'image' => 'gabor_szabo',
|
||||
// 'name' => 'Dr Gábor Szabó',
|
||||
// 'desc' => 'University of Szeged, Rector; Hungarian Association for Innovation, President',
|
||||
// ], [
|
||||
// 'image' => 'gyorgy_nagy',
|
||||
// 'name' => 'György Nagy',
|
||||
// 'desc' => 'Sigma Technology, Managing Director; Swedish Chamber of Commerce in Hungary, Vice-President',
|
||||
// ], [
|
||||
// 'image' => 'gyula_patko',
|
||||
// 'name' => 'Dr Gyula Patkó',
|
||||
// 'desc' => 'University of Miskolc, Former Rector',
|
||||
// ], [
|
||||
// 'image' => 'ildiko_csejtei',
|
||||
// 'name' => 'Ildikó B. Csejtei',
|
||||
// 'desc' => 'Independent Media Group, Owner, Director',
|
||||
// ], [
|
||||
// 'image' => 'istvan_salgo',
|
||||
// 'name' => 'István Salgó',
|
||||
// 'desc' => 'Business Council for Sustainable Development in Hungary, Honorary President',
|
||||
// ], [
|
||||
// 'image' => 'janos_durr',
|
||||
// 'name' => 'János Dürr',
|
||||
// 'desc' => 'Club of Hungarian Science Journalists, President',
|
||||
// ], [
|
||||
// 'image' => 'janos_takacs',
|
||||
// 'name' => 'János Takács',
|
||||
// 'desc' => 'Swedish Chamber of Commerce, President',
|
||||
// ], [
|
||||
// 'image' => 'jozsef_fulop',
|
||||
// 'name' => 'József Fülöp',
|
||||
// 'desc' => 'Moholy-Nagy University of Art and Design Budapest, Rector',
|
||||
// ], [
|
||||
// 'image' => 'jozsef_peter_martin',
|
||||
// 'name' => 'Dr József Péter Martin',
|
||||
// 'desc' => 'Transparency International Hungary, Managing Director',
|
||||
// ], [
|
||||
// 'image' => 'maria_judit_molnar',
|
||||
// 'name' => 'Dr Mária Judit Molnár',
|
||||
// 'desc' => 'SOTE Institute of Genomic Medicine and Rare Disorders, Head of the Institute',
|
||||
// ], [
|
||||
// 'image' => 'melinda_kamasz',
|
||||
// 'name' => 'Melinda Kamasz',
|
||||
// 'desc' => 'Figyelő, Deputy Editor in Chief, Figyelő Trend, Editor in Chief',
|
||||
// ], [
|
||||
// 'image' => 'miklos_antalovits',
|
||||
// 'name' => 'Dr Miklós Antalovits',
|
||||
// 'desc' => 'Budapest University of Technology and Economics, Professor Emeritus',
|
||||
// ], [
|
||||
// 'image' => 'miklos_bendzsel',
|
||||
// 'name' => 'Dr Miklós Bendzsel',
|
||||
// 'desc' => 'Hungarian Academy of Engineers, Vice-President; Hungarian Intellectual Property Office, Former President',
|
||||
// ], [
|
||||
// 'image' => 'peter_szauer',
|
||||
// 'name' => 'Péter Szauer',
|
||||
// 'desc' => 'HVG, President and Chief Executive Officer',
|
||||
// ], [
|
||||
// 'image' => 'richard_bogdan',
|
||||
// 'name' => 'Richárd Bogdán',
|
||||
// 'desc' => 'Mensa HungarIQa, President',
|
||||
// ], [
|
||||
// 'image' => 'rita_istivan',
|
||||
// 'name' => 'Rita Istiván',
|
||||
// 'desc' => 'BME, Honorary Associate Professor; Swedish Chamber of Commerce, Secretary General',
|
||||
// ], [
|
||||
// 'image' => 'roland_jakab',
|
||||
// 'name' => 'Roland Jakab',
|
||||
// 'desc' => 'Ericsson Hungary, Managing Director; Swedish Chamber of Commerce, Member of the Board',
|
||||
// ], [
|
||||
// 'image' => 'szabolcs_farkas',
|
||||
// 'name' => 'Dr Szabolcs Farkas',
|
||||
// 'desc' => 'Hungarian Intellectual Property Office, Vice-President',
|
||||
// ], [
|
||||
// 'image' => 'zoltan_bruckner',
|
||||
// 'name' => 'Zoltán Bruckner',
|
||||
// 'desc' => 'Primus Capital Management, Investment Director, Managing Partner',
|
||||
// ], [
|
||||
// 'image' => 'viktor_luszcz',
|
||||
// 'name' => 'Dr Viktor Łuszcz',
|
||||
// 'desc' => 'Hungarian Intellectual Property Office, President',
|
||||
// ]
|
||||
// ];
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j, t')
|
||||
->from(Judge::class, 'j')
|
||||
->leftJoin('j.titles', 't')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $year
|
||||
* @return array
|
||||
* @todo implement real filter by year
|
||||
*/
|
||||
public function getJudgesByYear(int $year)
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
return $qb->select('j')
|
||||
->from(Judge::class, 'j')
|
||||
->innerJoin('j.years', 'y')
|
||||
->where('y.year = :year')
|
||||
->innerJoin('j.titles', 't')
|
||||
->where('t.year = :year')
|
||||
->setParameter('year', $year)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Judge|null
|
||||
*/
|
||||
public function getJudge(int $id): ?Judge
|
||||
{
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->entityManager->getRepository(Judge::class)->find($id);
|
||||
return $judge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return Judge
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function create($data): Judge
|
||||
{
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, new Judge());
|
||||
$this->entityManager->persist($judge);
|
||||
$this->entityManager->flush();
|
||||
return $judge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param $data
|
||||
* @return Judge
|
||||
* @throws \Doctrine\ORM\ORMException
|
||||
* @throws \Doctrine\ORM\OptimisticLockException
|
||||
*/
|
||||
public function update(int $id, $data): Judge
|
||||
{
|
||||
$judge = $this->entityManager->getRepository(Judge::class)->find($id);
|
||||
/** @var Judge $judge */
|
||||
$judge = $this->hydrator->hydrate($data, $judge);
|
||||
$this->entityManager->persist($judge);
|
||||
$this->entityManager->flush();
|
||||
return $judge;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use DoctrineExpressiveModule\Hydrator\DoctrineObject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class JudgeManagerFactory
|
||||
@@ -12,6 +13,7 @@ class JudgeManagerFactory
|
||||
public function __invoke(ContainerInterface $container): JudgeManager
|
||||
{
|
||||
$entityManager = $container->get(EntityManager::class);
|
||||
return new JudgeManager($entityManager);
|
||||
$hydrator = $container->get(DoctrineObject::class);
|
||||
return new JudgeManager($entityManager, $hydrator);
|
||||
}
|
||||
}
|
||||
|
||||
39
src/App/Service/YearManager.php
Normal file
39
src/App/Service/YearManager.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\JudgeTitle;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
class YearManager
|
||||
{
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
public function __construct(EntityManager $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
public function getYears()
|
||||
{
|
||||
$qb = $this->entityManager->createQueryBuilder();
|
||||
$years = $qb->select('jt.year')
|
||||
->from(JudgeTitle::class, 'jt')
|
||||
->orderBy('jt.year', 'DESC')
|
||||
->distinct()
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
$filteredYears = array_map(function($year) {
|
||||
return $year['year'];
|
||||
}, $years);
|
||||
|
||||
$thisYear = date("Y");
|
||||
if (!in_array($thisYear, $filteredYears)) {
|
||||
array_unshift($filteredYears, $thisYear);
|
||||
}
|
||||
|
||||
return $filteredYears;
|
||||
}
|
||||
}
|
||||
17
src/App/Service/YearManagerFactory.php
Normal file
17
src/App/Service/YearManagerFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class YearManagerFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container): YearManager
|
||||
{
|
||||
$entityManager = $container->get(EntityManager::class);
|
||||
return new YearManager($entityManager);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user