* initial commit
This commit is contained in:
49
src/App/Action/Article/DeleteAction.php
Normal file
49
src/App/Action/Article/DeleteAction.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Entity\Article;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class DeleteAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
public function __construct(EntityManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
|
||||
if (null === ($entity = $this->em->find(Article::class, $id))) {
|
||||
$ret = new JsonResponse([
|
||||
'success' => false
|
||||
]);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
} catch (\Exception $ex) {
|
||||
$ret = new JsonResponse([
|
||||
'success' => false
|
||||
]);
|
||||
return $ret->withStatus(500);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Article/DeleteFactory.php
Normal file
17
src/App/Action/Article/DeleteFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFactory;
|
||||
use App\Action\Article\DeleteAction;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class DeleteFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
return new DeleteAction($em);
|
||||
}
|
||||
}
|
||||
51
src/App/Action/Article/GetAction.php
Normal file
51
src/App/Action/Article/GetAction.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Entity\Article;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Query;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
public function __construct(EntityManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id');
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
|
||||
$entity = $qb->select('a,u,c')
|
||||
->from(Article::class, 'a')
|
||||
->leftJoin('a.author', 'u')
|
||||
->leftJoin('a.comments', 'c')
|
||||
->where('a.id = :aid')
|
||||
->setParameter('aid', $id)
|
||||
->getQuery()
|
||||
->getOneOrNullResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
if (null === $entity) {
|
||||
$ret = new JsonResponse([
|
||||
'success' => false
|
||||
]);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
'result' => $entity,
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Article/GetFactory.php
Normal file
17
src/App/Action/Article/GetFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFactory;
|
||||
use App\Action\Article\GetAction;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class GetFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
return new GetAction($em);
|
||||
}
|
||||
}
|
||||
39
src/App/Action/Article/ListAction.php
Normal file
39
src/App/Action/Article/ListAction.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Entity\Article;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
public function __construct(EntityManager $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$entities = $qb->select('a,u,c')
|
||||
->from(Article::class, 'a')
|
||||
->leftJoin('a.author', 'u')
|
||||
->leftJoin('a.comments', 'c')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
'result' => $entities,
|
||||
]);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/Article/ListFactory.php
Normal file
17
src/App/Action/Article/ListFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFactory;
|
||||
use App\Action\Article\ListAction;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ListFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
return new ListAction($em);
|
||||
}
|
||||
}
|
||||
45
src/App/Action/Article/PostAction.php
Normal file
45
src/App/Action/Article/PostAction.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Entity\Article;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PostAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = $this->getRequestData($request);
|
||||
|
||||
$entity = $this->hydrator->hydrate($data, new Article());
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
'result' => $entity,
|
||||
]);
|
||||
}
|
||||
}
|
||||
18
src/App/Action/Article/PostFactory.php
Normal file
18
src/App/Action/Article/PostFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFactory;
|
||||
use App\Action\Article\PostAction;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PostFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
$hydrator = $this->getDoctrineHydrator($container);
|
||||
return new PostAction($em, $hydrator);
|
||||
}
|
||||
}
|
||||
53
src/App/Action/Article/PutAction.php
Normal file
53
src/App/Action/Article/PutAction.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFormAction;
|
||||
use App\Entity\Article;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class PutAction extends AbstractFormAction
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute('id', false);
|
||||
|
||||
if (null === ($entity = $this->em->find(Article::class, $id))) {
|
||||
$ret = new JsonResponse([
|
||||
'success' => false
|
||||
]);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
$data = $this->getRequestData($request);
|
||||
$entity = $this->hydrator->hydrate($data, $entity);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
'result' => $entity,
|
||||
]);
|
||||
}
|
||||
}
|
||||
18
src/App/Action/Article/PutFactory.php
Normal file
18
src/App/Action/Article/PutFactory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action\Article;
|
||||
|
||||
use App\Action\AbstractFactory;
|
||||
use App\Action\Article\PutAction;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class PutFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
$hydrator = $this->getDoctrineHydrator($container);
|
||||
return new PutAction($em, $hydrator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user