From 6b5e127317b3c43d20b51e34269f6256439b47e3 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Wed, 18 Dec 2024 15:28:53 +0100 Subject: [PATCH 1/2] extend patcher to use adapters for editable data --- .../Request/PatchDataObjectRequestBody.php | 2 ++ src/Patcher/Service/PatchService.php | 18 +++++++++++++++--- src/Updater/Service/UpdateService.php | 4 +--- src/Updater/Service/UpdateServiceInterface.php | 8 ++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/DataObject/Attribute/Request/PatchDataObjectRequestBody.php b/src/DataObject/Attribute/Request/PatchDataObjectRequestBody.php index 44439cd34..db8c16dcc 100644 --- a/src/DataObject/Attribute/Request/PatchDataObjectRequestBody.php +++ b/src/DataObject/Attribute/Request/PatchDataObjectRequestBody.php @@ -23,6 +23,7 @@ use OpenApi\Attributes\RequestBody; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateBooleanProperty; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateIntegerProperty; +use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateObjectProperty; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\UpdateStringProperty; /** @@ -57,6 +58,7 @@ public function __construct() new UpdateStringProperty('childrenSortBy'), new UpdateStringProperty('childrenSortOrder'), new UpdateBooleanProperty('published'), + new UpdateObjectProperty('editableData'), ], type: 'object', ), diff --git a/src/Patcher/Service/PatchService.php b/src/Patcher/Service/PatchService.php index 1e5c441be..3c63f0fb2 100644 --- a/src/Patcher/Service/PatchService.php +++ b/src/Patcher/Service/PatchService.php @@ -32,6 +32,8 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Config; use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs; +use Pimcore\Bundle\StudioBackendBundle\Updater\Service\UpdateServiceInterface; +use Pimcore\Model\DataObject\Concrete; use Pimcore\Model\Element\ElementDescriptor; use Pimcore\Model\Element\ElementInterface; use Pimcore\Model\UserInterface; @@ -43,10 +45,11 @@ final readonly class PatchService implements PatchServiceInterface { public function __construct( - private SynchronousProcessingServiceInterface $synchronousProcessingService, - private JobExecutionAgentInterface $jobExecutionAgent, + private AdapterLoaderInterface $adapterLoader, private ElementServiceInterface $elementService, - private AdapterLoaderInterface $adapterLoader + private JobExecutionAgentInterface $jobExecutionAgent, + private SynchronousProcessingServiceInterface $synchronousProcessingService, + private UpdateServiceInterface $updateService, ) { } @@ -112,6 +115,15 @@ public function patchElement( UserInterface $user, ): void { try { + if (isset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]) && $element instanceof Concrete) { + $this->updateService->updateEditableData( + $element, + $elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY] + ); + + unset($elementPatchData[UpdateServiceInterface::EDITABLE_DATA_KEY]); + } + $adapters = $this->adapterLoader->loadAdapters($elementType); foreach ($adapters as $adapter) { $adapter->patch($element, $elementPatchData); diff --git a/src/Updater/Service/UpdateService.php b/src/Updater/Service/UpdateService.php index b4a80f55f..b8a0b5a2d 100644 --- a/src/Updater/Service/UpdateService.php +++ b/src/Updater/Service/UpdateService.php @@ -36,8 +36,6 @@ use ElementProviderTrait; use ValidateFieldTypeTrait; - private const EDITABLE_DATA_KEY = 'editableData'; - public function __construct( private AdapterLoaderInterface $adapterLoader, private DataAdapterServiceInterface $dataAdapterService, @@ -74,7 +72,7 @@ public function update(string $elementType, int $id, array $data): void /** * @throws ElementSavingFailedException */ - private function updateEditableData(Concrete $element, array $editableData): void + public function updateEditableData(Concrete $element, array $editableData): void { try { $class = $element->getClass(); diff --git a/src/Updater/Service/UpdateServiceInterface.php b/src/Updater/Service/UpdateServiceInterface.php index fb14d1afa..5ecd5cf86 100644 --- a/src/Updater/Service/UpdateServiceInterface.php +++ b/src/Updater/Service/UpdateServiceInterface.php @@ -18,14 +18,22 @@ use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException; use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; +use Pimcore\Model\DataObject\Concrete; /** * @internal */ interface UpdateServiceInterface { + public const EDITABLE_DATA_KEY = 'editableData'; + /** * @throws ElementSavingFailedException|NotFoundException */ public function update(string $elementType, int $id, array $data): void; + + /** + * @throws ElementSavingFailedException + */ + public function updateEditableData(Concrete $element, array $editableData): void; } From 5f2a745863274b35e3971351737a4a01c7b03a7d Mon Sep 17 00:00:00 2001 From: lukmzig Date: Wed, 18 Dec 2024 15:57:54 +0100 Subject: [PATCH 2/2] update controller default responses --- src/Asset/Controller/PatchController.php | 4 +++- src/Asset/Controller/PatchFolderController.php | 4 +++- src/Asset/Controller/UpdateController.php | 8 +++++++- src/DataObject/Controller/PatchController.php | 4 +++- src/DataObject/Controller/UpdateController.php | 8 +++++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Asset/Controller/PatchController.php b/src/Asset/Controller/PatchController.php index 21a33734e..3874a1923 100644 --- a/src/Asset/Controller/PatchController.php +++ b/src/Asset/Controller/PatchController.php @@ -76,8 +76,10 @@ public function __construct( content: new IdJson('ID of created jobRun', 'jobRunId') )] #[DefaultResponses([ - HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::BAD_REQUEST, + HttpResponseCodes::INTERNAL_SERVER_ERROR, HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::UNAUTHORIZED, ])] public function assetPatchById(#[MapRequestPayload] PatchAssetParameter $patchAssetParameter): Response { diff --git a/src/Asset/Controller/PatchFolderController.php b/src/Asset/Controller/PatchFolderController.php index a722e24a9..0e0ab169b 100644 --- a/src/Asset/Controller/PatchFolderController.php +++ b/src/Asset/Controller/PatchFolderController.php @@ -72,8 +72,10 @@ public function __construct( content: new IdJson('ID of created jobRun', 'jobRunId') )] #[DefaultResponses([ - HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::BAD_REQUEST, + HttpResponseCodes::INTERNAL_SERVER_ERROR, HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::UNAUTHORIZED, ])] public function assetPatchFolderById(#[MapRequestPayload] PatchFolderParameter $patchFolderParameter): Response { diff --git a/src/Asset/Controller/UpdateController.php b/src/Asset/Controller/UpdateController.php index 7292292a7..a3a3cbd78 100644 --- a/src/Asset/Controller/UpdateController.php +++ b/src/Asset/Controller/UpdateController.php @@ -22,6 +22,8 @@ use Pimcore\Bundle\StudioBackendBundle\Asset\MappedParameter\UpdateAssetParameter; use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface; use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; @@ -49,6 +51,9 @@ public function __construct( parent::__construct($serializer); } + /** + * @throws ElementSavingFailedException|NotFoundException + */ #[Route('/assets/{id}', name: 'pimcore_studio_api_update_asset', methods: ['PUT'])] #[IsGranted(UserPermissions::ASSETS->value)] #[Put( @@ -65,8 +70,9 @@ public function __construct( content: new OneOfAssetJson() )] #[DefaultResponses([ - HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::INTERNAL_SERVER_ERROR, HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::UNAUTHORIZED, ])] public function assetUpdateById(int $id, #[MapRequestPayload] UpdateAssetParameter $updateAsset): JsonResponse { diff --git a/src/DataObject/Controller/PatchController.php b/src/DataObject/Controller/PatchController.php index 4b6a4592c..8ad5af386 100644 --- a/src/DataObject/Controller/PatchController.php +++ b/src/DataObject/Controller/PatchController.php @@ -76,8 +76,10 @@ public function __construct( content: new IdJson('ID of created jobRun', 'jobRunId') )] #[DefaultResponses([ - HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::BAD_REQUEST, + HttpResponseCodes::INTERNAL_SERVER_ERROR, HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::UNAUTHORIZED, ])] public function dataObjectPatchById(#[MapRequestPayload] DataParameter $parameter): Response { diff --git a/src/DataObject/Controller/UpdateController.php b/src/DataObject/Controller/UpdateController.php index 5fbb281ca..1dfec51b6 100644 --- a/src/DataObject/Controller/UpdateController.php +++ b/src/DataObject/Controller/UpdateController.php @@ -22,6 +22,8 @@ use Pimcore\Bundle\StudioBackendBundle\DataObject\Attribute\Response\Content\OneOfDataObjectsJson; use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\DataParameter; use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataObjectServiceInterface; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException; +use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; @@ -49,6 +51,9 @@ public function __construct( parent::__construct($serializer); } + /** + * @throws ElementSavingFailedException|NotFoundException + */ #[Route('/data-objects/{id}', name: 'pimcore_studio_api_update_data_object', methods: ['PUT'])] #[IsGranted(UserPermissions::DATA_OBJECTS->value)] #[Put( @@ -65,8 +70,9 @@ public function __construct( content: new OneOfDataObjectsJson() )] #[DefaultResponses([ - HttpResponseCodes::UNAUTHORIZED, + HttpResponseCodes::INTERNAL_SERVER_ERROR, HttpResponseCodes::NOT_FOUND, + HttpResponseCodes::UNAUTHORIZED, ])] public function dataObjectUpdateById(int $id, #[MapRequestPayload] DataParameter $parameter): JsonResponse {