2018-12-20 10:25:24 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-04-14 03:19:24 +00:00
|
|
|
namespace App\Controller\Api\Admin;
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2019-04-14 03:19:24 +00:00
|
|
|
use App\Controller\Api\AbstractApiCrudController;
|
2018-12-20 10:25:24 +00:00
|
|
|
use App\Entity;
|
2020-02-22 20:15:02 +00:00
|
|
|
use App\Exception;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2018-12-20 10:25:24 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
/**
|
|
|
|
* @template TEntity as object
|
|
|
|
* @extends AbstractApiCrudController<TEntity>
|
|
|
|
*/
|
2019-04-14 03:19:24 +00:00
|
|
|
abstract class AbstractAdminApiCrudController extends AbstractApiCrudController
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2019-08-09 15:00:21 +00:00
|
|
|
public function listAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
|
|
|
$query = $this->em->createQuery('SELECT e FROM ' . $this->entityClass . ' e');
|
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
return $this->listPaginatedFromQuery($request, $response, $query);
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2019-09-04 18:00:51 +00:00
|
|
|
* @throws Exception
|
2018-12-20 10:25:24 +00:00
|
|
|
*/
|
2019-08-09 15:00:21 +00:00
|
|
|
public function createAction(ServerRequest $request, Response $response): ResponseInterface
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
$row = $this->createRecord((array)$request->getParsedBody());
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
$return = $this->viewRecord($row, $request);
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withJson($return);
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
2021-07-19 05:53:45 +00:00
|
|
|
*
|
|
|
|
* @return TEntity
|
2019-09-04 18:00:51 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function createRecord(array $data): object
|
2019-09-04 18:00:51 +00:00
|
|
|
{
|
2021-04-23 22:12:47 +00:00
|
|
|
return $this->editRecord($data);
|
2019-09-04 18:00:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 10:25:24 +00:00
|
|
|
/**
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2019-09-18 20:11:49 +00:00
|
|
|
* @param mixed $id
|
2018-12-20 10:25:24 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
public function getAction(ServerRequest $request, Response $response, mixed $id): ResponseInterface
|
2018-12-22 00:01:04 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$record = $this->getRecord($id);
|
2018-12-22 00:01:04 +00:00
|
|
|
|
2019-09-20 02:31:22 +00:00
|
|
|
if (null === $record) {
|
|
|
|
return $response->withStatus(404)
|
2021-07-19 05:53:45 +00:00
|
|
|
->withJson(Entity\Api\Error::notFound());
|
2019-09-20 02:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
$return = $this->viewRecord($record, $request);
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withJson($return);
|
2018-12-22 00:01:04 +00:00
|
|
|
}
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2019-04-08 05:09:53 +00:00
|
|
|
/**
|
2019-09-18 20:11:49 +00:00
|
|
|
* @param mixed $id
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2021-07-19 05:53:45 +00:00
|
|
|
* @return TEntity|null
|
2019-04-08 05:09:53 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
protected function getRecord(mixed $id): ?object
|
2019-04-08 05:09:53 +00:00
|
|
|
{
|
2019-09-18 20:11:49 +00:00
|
|
|
return $this->em->find($this->entityClass, $id);
|
2019-04-08 05:09:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 00:01:04 +00:00
|
|
|
/**
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2019-09-18 20:11:49 +00:00
|
|
|
* @param mixed $id
|
2018-12-22 00:01:04 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
public function editAction(ServerRequest $request, Response $response, mixed $id): ResponseInterface
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$record = $this->getRecord($id);
|
2018-12-20 10:25:24 +00:00
|
|
|
|
|
|
|
if (null === $record) {
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withStatus(404)
|
2021-07-19 05:53:45 +00:00
|
|
|
->withJson(Entity\Api\Error::notFound());
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
$this->editRecord((array)$request->getParsedBody(), $record);
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2019-08-21 22:33:47 +00:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Changes saved successfully.')));
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2019-09-18 20:11:49 +00:00
|
|
|
* @param mixed $id
|
2018-12-20 10:25:24 +00:00
|
|
|
*/
|
2021-04-23 22:12:47 +00:00
|
|
|
public function deleteAction(ServerRequest $request, Response $response, mixed $id): ResponseInterface
|
2018-12-20 10:25:24 +00:00
|
|
|
{
|
2020-10-14 22:19:31 +00:00
|
|
|
$record = $this->getRecord($id);
|
2018-12-20 10:25:24 +00:00
|
|
|
|
|
|
|
if (null === $record) {
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withStatus(404)
|
2021-07-19 05:53:45 +00:00
|
|
|
->withJson(Entity\Api\Error::notFound());
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 20:27:14 +00:00
|
|
|
$this->deleteRecord($record);
|
2018-12-20 10:25:24 +00:00
|
|
|
|
2019-08-21 22:33:47 +00:00
|
|
|
return $response->withJson(new Entity\Api\Status(true, __('Record deleted successfully.')));
|
2018-12-20 10:25:24 +00:00
|
|
|
}
|
|
|
|
}
|