Skip to content

Commit

Permalink
Fix update commands that didn't use the provided Request (and instead…
Browse files Browse the repository at this point in the history
… falling back to the global request, causing trouble with charon-frontend.)
  • Loading branch information
daedeloth committed Sep 13, 2022
1 parent a2c65b0 commit 921cf2b
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ public function store(Request $request)
$entity = $this->toEntity($inputResource, $writeContext);

// Save the entity
$entity = $this->saveEntity($request, $entity);
try {
$entity = $this->saveEntity($request, $entity);
} catch (ResourceValidationException $e) {
return $this->getValidationErrorResponse($e);
}

$createdResources->add($this->toResource($entity, $readContext));
}
Expand Down Expand Up @@ -221,7 +225,7 @@ public function edit(Request $request)
$writeContext = $this->getContext(Action::EDIT);

$inputResource = $this->getResourceTransformer()
->fromInput($resourceDefinition, $writeContext)
->fromInput($resourceDefinition, $writeContext, $request)
->first();

try {
Expand All @@ -233,7 +237,13 @@ public function edit(Request $request)
$entity = $this->toEntity($inputResource, $writeContext, $entity);

// Save the entity
$entity = $this->saveEntity($request, $entity);
//$entity = $this->saveEntity($request, $entity);
// Save the entity
try {
$entity = $this->saveEntity($request, $entity);
} catch (ResourceValidationException $e) {
return $this->getValidationErrorResponse($e);
}

// Turn back into a resource
return $this->createViewEntityResponse($entity);
Expand Down Expand Up @@ -264,7 +274,7 @@ public function patch(Request $request)
->getResourceDefinition($this->getResourceDefinition(), $entity);

$writeContext = $this->getContext(Action::EDIT);
$inputResource = $this->getResourceTransformer()->fromInput($resourceDefinition, $writeContext);
$inputResource = $this->getResourceTransformer()->fromInput($resourceDefinition, $writeContext, $request);

try {
return $this->processPatchResource($request, $entity, $inputResource, $writeContext);
Expand Down Expand Up @@ -294,7 +304,13 @@ protected function processPatchResource(
$entity = $this->toEntity($inputResource, $writeContext, $entity);

// Save the entity
$entity = $this->saveEntity($request, $entity);
//$entity = $this->saveEntity($request, $entity);
// Save the entity
try {
$entity = $this->saveEntity($request, $entity);
} catch (ResourceValidationException $e) {
return $this->getValidationErrorResponse($e);
}

// Turn back into a resource
return $this->createViewEntityResponse($entity);
Expand Down Expand Up @@ -324,6 +340,13 @@ public function destroy(Request $request)
*
* @param $entity
* @return ResourceResponse
* @throws \CatLab\Charon\Exceptions\InvalidContextAction
* @throws \CatLab\Charon\Exceptions\InvalidEntityException
* @throws \CatLab\Charon\Exceptions\InvalidPropertyException
* @throws \CatLab\Charon\Exceptions\InvalidResourceDefinition
* @throws \CatLab\Charon\Exceptions\InvalidTransformer
* @throws \CatLab\Charon\Exceptions\IterableExpected
* @throws \CatLab\Charon\Exceptions\VariableNotFoundInContext
*/
protected function createViewEntityResponse($entity)
{
Expand Down

0 comments on commit 921cf2b

Please sign in to comment.