* 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user