From e92860173c8635da1f393345fc6dea6a27325b59 Mon Sep 17 00:00:00 2001 From: mattamon Date: Wed, 3 Apr 2024 14:11:09 +0200 Subject: [PATCH] Add asset collections and single asset --- config/api/V1/assets.yaml | 38 +++++++++++++ config/api/V1/getAsset.yaml | 30 ++++++++++ config/pimcore/packages/nelmio_api_doc.yaml | 1 + config/pimcore/routing.yaml | 1 - src/Controller/Api/AbstractApiController.php | 1 + .../Api/V1/Assets/CollectionController.php | 55 +++++++++++++++++++ .../Api/V1/Assets/GetController.php | 39 +++++++++++++ .../Api/V1/TranslationController.php | 2 +- .../Collection.php} | 24 ++++++-- src/EventSubscriber/ApiExceptionListener.php | 47 ---------------- src/Service/TranslatorService.php | 7 ++- 11 files changed, 189 insertions(+), 56 deletions(-) create mode 100644 config/api/V1/assets.yaml create mode 100644 config/api/V1/getAsset.yaml create mode 100644 src/Controller/Api/V1/Assets/CollectionController.php create mode 100644 src/Controller/Api/V1/Assets/GetController.php rename src/{Controller/Api/Trait/JsonLdResponseTrait.php => Dto/Collection.php} (53%) delete mode 100644 src/EventSubscriber/ApiExceptionListener.php diff --git a/config/api/V1/assets.yaml b/config/api/V1/assets.yaml new file mode 100644 index 000000000..d13e779c0 --- /dev/null +++ b/config/api/V1/assets.yaml @@ -0,0 +1,38 @@ +/api/v1/assets: + get: + tags: [Assets] + summary: Get all assets + parameters: + - in: query + name: page + description: Page number + required: true + schema: + type: integer + format: int + example: 1 + - in: query + name: pageSize + description: Number of items per page + required: true + schema: + type: integer + format: int + example: 10 + responses: + '200': + description: Successful response + content: + application/json: + schema: + ref: '#/components/schemas/Asset' + '403': + description: Unauthorized + content: + application/json: + schema: + type: object + properties: + message: + type: string + example: 'Unauthorized' \ No newline at end of file diff --git a/config/api/V1/getAsset.yaml b/config/api/V1/getAsset.yaml new file mode 100644 index 000000000..0870bf1aa --- /dev/null +++ b/config/api/V1/getAsset.yaml @@ -0,0 +1,30 @@ +/api/v1/assets/{id}: + get: + tags: [Assets] + summary: Get asset with id + parameters: + - in: path + name: id + description: Id of the asset + required: true + schema: + type: integer + format: int + example: 1 + responses: + '200': + description: Successful response + content: + application/json: + schema: + ref: '#/components/schemas/Asset' + '403': + description: Unauthorized + content: + application/json: + schema: + type: object + properties: + message: + type: string + example: 'Unauthorized' \ No newline at end of file diff --git a/config/pimcore/packages/nelmio_api_doc.yaml b/config/pimcore/packages/nelmio_api_doc.yaml index e1c6d6869..4c3cec1c9 100644 --- a/config/pimcore/packages/nelmio_api_doc.yaml +++ b/config/pimcore/packages/nelmio_api_doc.yaml @@ -1,6 +1,7 @@ nelmio_api_doc: models: names: + - { alias: Asset, type: Pimcore\Bundle\StudioApiBundle\Dto\Asset } - { alias: Translation, type: Pimcore\Bundle\StudioApiBundle\Dto\Translation } areas: path_patterns: # an array of regexps (document only routes under /api, except /api/doc) diff --git a/config/pimcore/routing.yaml b/config/pimcore/routing.yaml index 8916e04a7..35999526b 100644 --- a/config/pimcore/routing.yaml +++ b/config/pimcore/routing.yaml @@ -1,7 +1,6 @@ api_platform: resource: . type: api_platform - prefix: /testtesttest api_platform_swagger_ui: diff --git a/src/Controller/Api/AbstractApiController.php b/src/Controller/Api/AbstractApiController.php index 2e2802c33..838b22376 100644 --- a/src/Controller/Api/AbstractApiController.php +++ b/src/Controller/Api/AbstractApiController.php @@ -1,4 +1,5 @@ getAssetQuery() + ->setPage($collection->getPage()) + ->setPageSize($collection->getPageSize()); + + + return $this->jsonLd($this->assetSearchService->searchAssets($assetQuery)); + } + + private function getAssetQuery(): AssetQuery + { + return $this->assetQueryProvider->createAssetQuery(); + } +} \ No newline at end of file diff --git a/src/Controller/Api/V1/Assets/GetController.php b/src/Controller/Api/V1/Assets/GetController.php new file mode 100644 index 000000000..ed335bbed --- /dev/null +++ b/src/Controller/Api/V1/Assets/GetController.php @@ -0,0 +1,39 @@ +jsonLd($this->assetSearchService->getAssetById($id)); + } +} \ No newline at end of file diff --git a/src/Controller/Api/V1/TranslationController.php b/src/Controller/Api/V1/TranslationController.php index c8aa0529d..d1b24345a 100644 --- a/src/Controller/Api/V1/TranslationController.php +++ b/src/Controller/Api/V1/TranslationController.php @@ -1,5 +1,5 @@ page; + } + + public function getPageSize(): int + { + return $this->pageSize; + } +} \ No newline at end of file diff --git a/src/EventSubscriber/ApiExceptionListener.php b/src/EventSubscriber/ApiExceptionListener.php deleted file mode 100644 index 6ed97c4ba..000000000 --- a/src/EventSubscriber/ApiExceptionListener.php +++ /dev/null @@ -1,47 +0,0 @@ - ['onKernelException', 2], - ]; - } - - public function onKernelException(ExceptionEvent $event): void - { - $exception = $event->getThrowable(); - - // ... perform some action (e.g. logging) - - // optionally set the custom response - $event->setResponse(new Response($exception->getMessage(), $exception->getCode())); - - // or stop propagation (prevents the next exception listeners from being called) - //$event->stopPropagation(); - } -} diff --git a/src/Service/TranslatorService.php b/src/Service/TranslatorService.php index 13ac2a384..7d18e41dc 100644 --- a/src/Service/TranslatorService.php +++ b/src/Service/TranslatorService.php @@ -42,9 +42,10 @@ public function __construct( public function getAllTranslations(string $locale): Translation { - $translations = $this->translator->getCatalogue($locale)->all(self::DOMAIN); - - return new Translation($locale, $translations); + return new Translation( + $locale, + $this->translator->getCatalogue($locale)->all(self::DOMAIN) + ); } public function getTranslationsForKeys(string $locale, array $keys): Translation