mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
Merge branch 'add-api-for-person' into 'master'
Add api for person See merge request Chill-Projet/chill-bundles!61
This commit is contained in:
@@ -40,6 +40,20 @@ class AbstractCRUDController extends AbstractController
|
||||
return $e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entity.
|
||||
*
|
||||
* @param string $action
|
||||
* @param Request $request
|
||||
* @return object
|
||||
*/
|
||||
protected function createEntity(string $action, Request $request): object
|
||||
{
|
||||
$type = $this->getEntityClass();
|
||||
|
||||
return new $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of entities
|
||||
*
|
||||
|
@@ -85,11 +85,75 @@ class ApiController extends AbstractCRUDController
|
||||
case Request::METHOD_PUT:
|
||||
case Request::METHOD_PATCH:
|
||||
return $this->entityPut('_entity', $request, $id, $_format);
|
||||
case Request::METHOD_POST:
|
||||
return $this->entityPostAction('_entity', $request, $id, $_format);
|
||||
default:
|
||||
throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException("This method is not implemented");
|
||||
}
|
||||
}
|
||||
public function entityPost(Request $request, $_format): Response
|
||||
{
|
||||
switch($request->getMethod()) {
|
||||
case Request::METHOD_POST:
|
||||
return $this->entityPostAction('_entity', $request, $_format);
|
||||
default:
|
||||
throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException("This method is not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
protected function entityPostAction($action, Request $request, string $_format): Response
|
||||
{
|
||||
$entity = $this->createEntity($action, $request);
|
||||
|
||||
try {
|
||||
$entity = $this->deserialize($action, $request, $_format, $entity);
|
||||
} catch (NotEncodableValueException $e) {
|
||||
throw new BadRequestException("invalid json", 400, $e);
|
||||
}
|
||||
|
||||
$errors = $this->validate($action, $request, $_format, $entity);
|
||||
|
||||
$response = $this->onAfterValidation($action, $request, $_format, $entity, $errors);
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if ($errors->count() > 0) {
|
||||
$response = $this->json($errors);
|
||||
$response->setStatusCode(Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $request, $_format, $entity);
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$response = $this->onPostCheckACL($action, $request, $_format, $entity);
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->persist($entity);
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
$response = $this->onBeforeSerialize($action, $request, $_format, $entity);
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->json(
|
||||
$entity,
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity)
|
||||
);
|
||||
}
|
||||
public function entityPut($action, Request $request, $id, string $_format): Response
|
||||
{
|
||||
$entity = $this->getEntity($action, $id, $request, $_format);
|
||||
@@ -407,6 +471,7 @@ class ApiController extends AbstractCRUDController
|
||||
return [ 'groups' => [ 'read' ]];
|
||||
case Request::METHOD_PUT:
|
||||
case Request::METHOD_PATCH:
|
||||
case Request::METHOD_POST:
|
||||
return [ 'groups' => [ 'write' ]];
|
||||
default:
|
||||
throw new \LogicException("get context for serialization is not implemented for this method");
|
||||
|
@@ -183,48 +183,26 @@ class CRUDRoutesLoader extends Loader
|
||||
$methods = \array_keys(\array_filter($action['methods'], function($value, $key) { return $value; },
|
||||
ARRAY_FILTER_USE_BOTH));
|
||||
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods($methods);
|
||||
|
||||
$collection->add('chill_api_single_'.$crudConfig['name'].'_'.$name, $route);
|
||||
}
|
||||
if (count($methods) === 0) {
|
||||
throw new \RuntimeException("The api configuration named \"{$crudConfig['name']}\", action \"{$name}\", ".
|
||||
"does not have any allowed methods. You should remove this action from the config ".
|
||||
"or allow, at least, one method");
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
if ('_entity' === $name && \in_array(Request::METHOD_POST, $methods)) {
|
||||
unset($methods[\array_search(Request::METHOD_POST, $methods)]);
|
||||
$entityPostRoute = $this->createEntityPostRoute($name, $crudConfig, $action,
|
||||
$controller);
|
||||
$collection->add("chill_api_single_{$crudConfig['name']}_{$name}_create",
|
||||
$entityPostRoute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for api multi
|
||||
*
|
||||
* @param $crudConfig
|
||||
* @return RouteCollection
|
||||
*/
|
||||
protected function loadApiMultiConfig(array $crudConfig): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller ='csapi_'.$crudConfig['name'].'_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// filter only on single actions
|
||||
$singleCollection = $action['single-collection'] ?? $name === '_index' ? 'collection' : NULL;
|
||||
if ('single' === $singleCollection) {
|
||||
if (count($methods) === 0) {
|
||||
// the only method was POST,
|
||||
// continue to next
|
||||
continue;
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? '_entity' === $name ? 'entityApi' : $name.'Api')
|
||||
];
|
||||
|
||||
// path are rewritten
|
||||
// if name === 'default', we rewrite it to nothing :-)
|
||||
$localName = '_entity' === $name ? '' : '/'.$name;
|
||||
$localPath = $action['path'] ?? '/{id}'.$localName.'.{_format}';
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
|
||||
$requirements = $action['requirements'] ?? [ '{id}' => '\d+' ];
|
||||
|
||||
$methods = \array_keys(\array_filter($action['methods'], function($value, $key) { return $value; },
|
||||
ARRAY_FILTER_USE_BOTH));
|
||||
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods($methods);
|
||||
|
||||
@@ -233,4 +211,18 @@ class CRUDRoutesLoader extends Loader
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
private function createEntityPostRoute(string $name, $crudConfig, array $action, $controller): Route
|
||||
{
|
||||
$localPath = $action['path'].'.{_format}';
|
||||
$defaults = [
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? 'entityPost')
|
||||
];
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
$requirements = $action['requirements'] ?? [];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods([ Request::METHOD_POST ]);
|
||||
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
|
@@ -20,19 +20,32 @@
|
||||
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Repository\CenterRepository;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class CenterNormalizer implements NormalizerInterface
|
||||
class CenterNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
private CenterRepository $repository;
|
||||
|
||||
|
||||
public function __construct(CenterRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function normalize($center, string $format = null, array $context = array())
|
||||
{
|
||||
/** @var Center $center */
|
||||
return [
|
||||
'id' => $center->getId(),
|
||||
'type' => 'center',
|
||||
'name' => $center->getName()
|
||||
];
|
||||
}
|
||||
@@ -41,4 +54,30 @@ class CenterNormalizer implements NormalizerInterface
|
||||
{
|
||||
return $data instanceof Center;
|
||||
}
|
||||
|
||||
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||
{
|
||||
if (FALSE === \array_key_exists('type', $data)) {
|
||||
throw new InvalidArgumentException('missing "type" key in data');
|
||||
}
|
||||
if ('center' !== $data['type']) {
|
||||
throw new InvalidArgumentException('type should be equal to "center"');
|
||||
}
|
||||
if (FALSE === \array_key_exists('id', $data)) {
|
||||
throw new InvalidArgumentException('missing "id" key in data');
|
||||
}
|
||||
|
||||
$center = $this->repository->find($data['id']);
|
||||
|
||||
if (null === $center) {
|
||||
throw new UnexpectedValueException("The type with id {$data['id']} does not exists");
|
||||
}
|
||||
|
||||
return $center;
|
||||
}
|
||||
|
||||
public function supportsDenormalization($data, string $type, string $format = null)
|
||||
{
|
||||
return $type === Center::class;
|
||||
}
|
||||
}
|
||||
|
@@ -9,15 +9,14 @@ servers:
|
||||
description: "Your current dev server"
|
||||
|
||||
components:
|
||||
parameters:
|
||||
_format:
|
||||
name: _format
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
enum:
|
||||
- json
|
||||
schemas:
|
||||
Center:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
|
||||
paths:
|
||||
/1.0/search.json:
|
||||
|
Reference in New Issue
Block a user