From 0a274eb2a4bd33d2de8a6521be27f3de69ded9e8 Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 6 Sep 2021 13:28:39 +0200 Subject: [PATCH] Enable DELETE in the ApiController --- .../Controller/CalendarRangeAPIController.php | 6 --- .../CRUD/Controller/ApiController.php | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php index a60eb98b3..672e9527c 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarRangeAPIController.php @@ -38,10 +38,4 @@ class CalendarRangeAPIController extends ApiController //TODO use also the paginator, eg return $this->serializeCollection('get', $request, $_format, $paginator, $results); } - // public function calendarRangeApi($id, Request $request, string $_format): Response - // { - // return $this->addRemoveSomething('calendarRange', $id, $request, $_format, 'calendarRange', CalendarRange::class, [ 'groups' => [ 'read' ] ]); - // } - - } \ No newline at end of file diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php index cab0bcdcb..359ff0384 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php @@ -87,6 +87,8 @@ class ApiController extends AbstractCRUDController return $this->entityPut('_entity', $request, $id, $_format); case Request::METHOD_POST: return $this->entityPostAction('_entity', $request, $id, $_format); + case Request::METHOD_DELETE: + return $this->entityDelete('_entity', $request, $id, $_format); default: throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException("This method is not implemented"); } @@ -217,6 +219,54 @@ class ApiController extends AbstractCRUDController $this->getContextForSerializationPostAlter($action, $request, $_format, $entity) ); } + public function entityDelete($action, Request $request, $id, string $_format): Response + { + $entity = $this->getEntity($action, $id, $request, $_format); + + if (NULL === $entity) { + throw $this->createNotFoundException(sprintf("The %s with id %s " + . "is not found", $this->getCrudName(), $id)); + } + + $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; + } + + $response = $this->onBeforeSerialize($action, $request, $_format, $entity); + if ($response instanceof Response) { + return $response; + } + + $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; + } + + $this->getDoctrine()->getManager()->remove($entity); + $this->getDoctrine()->getManager()->flush(); + + $response = $this->onAfterFlush($action, $request, $_format, $entity, $errors); + if ($response instanceof Response) { + return $response; + } + + return $this->json(Response::HTTP_OK); + } protected function onAfterValidation(string $action, Request $request, string $_format, $entity, ConstraintViolationListInterface $errors, array $more = []): ?Response {