mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 15:43:51 +00:00
Add conflict resolution for generated API + add test
Implemented additional code to handle version conflicts when editing accompanying period work. By keeping track of the current version and returning an HTTP conflict response when it doesn't match with the provided entity version, users are properly alerted to update their entity before continuing. Furthermore, adjusted BadRequestHttpException to match correct arguments order and introduced entity version as query parameter for the URL. ensure kernel is shutdown after generating data
This commit is contained in:
@@ -15,10 +15,14 @@ use Chill\MainBundle\CRUD\Resolver\Resolver;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Doctrine\DBAL\LockMode;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\OptimisticLockException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
@@ -173,6 +177,21 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
if (null === $e) {
|
||||
throw $this->createNotFoundException(sprintf('The object %s for id %s is not found', $this->getEntityClass(), $id));
|
||||
}
|
||||
if ($request->query->has('entity_version')) {
|
||||
$expectedVersion = $request->query->getInt('entity_version');
|
||||
|
||||
try {
|
||||
$manager = $this->getDoctrine()->getManagerForClass($this->getEntityClass());
|
||||
|
||||
if ($manager instanceof EntityManagerInterface) {
|
||||
$manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion);
|
||||
} else {
|
||||
throw new \LogicException('This manager does not allow locking.');
|
||||
}
|
||||
} catch (OptimisticLockException $e) {
|
||||
throw new ConflictHttpException('Sorry, but someone else has already changed this entity. Please refresh the page and apply the changes again', $e);
|
||||
}
|
||||
}
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
@@ -135,7 +135,7 @@ class ApiController extends AbstractCRUDController
|
||||
try {
|
||||
$entity = $this->deserialize($action, $request, $_format, $entity);
|
||||
} catch (NotEncodableValueException $e) {
|
||||
throw new BadRequestHttpException('invalid json', 400, $e);
|
||||
throw new BadRequestHttpException('invalid json', $e, 400);
|
||||
}
|
||||
|
||||
$errors = $this->validate($action, $request, $_format, $entity);
|
||||
@@ -153,7 +153,7 @@ class ApiController extends AbstractCRUDController
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->getDoctrine()->getManagerForClass($this->getEntityClass())->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
|
||||
|
||||
|
Reference in New Issue
Block a user