Skip to content

Commit

Permalink
Add asset collections and single asset
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Apr 3, 2024
1 parent c01b5f6 commit e928601
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 56 deletions.
38 changes: 38 additions & 0 deletions config/api/V1/assets.yaml
Original file line number Diff line number Diff line change
@@ -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'
30 changes: 30 additions & 0 deletions config/api/V1/getAsset.yaml
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions config/pimcore/packages/nelmio_api_doc.yaml
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 0 additions & 1 deletion config/pimcore/routing.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
api_platform:
resource: .
type: api_platform
prefix: /testtesttest


api_platform_swagger_ui:
Expand Down
1 change: 1 addition & 0 deletions src/Controller/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

/**
* Pimcore
Expand Down
55 changes: 55 additions & 0 deletions src/Controller/Api/V1/Assets/CollectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\V1\Assets;

use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Dto\Collection;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQuery;
use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\AssetQueryProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

final class CollectionController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly AssetQueryProviderInterface $assetQueryProvider,
private readonly AssetSearchServiceInterface $assetSearchService,
)
{
parent::__construct($serializer);
}

#[Route('/v1/assets', name: 'pimcore_studio_api_v1_asets', methods: ['GET'])]
public function getAssets(#[MapQueryString] Collection $collection): JsonResponse
{

$assetQuery = $this->getAssetQuery()
->setPage($collection->getPage())
->setPageSize($collection->getPageSize());


return $this->jsonLd($this->assetSearchService->searchAssets($assetQuery));
}

private function getAssetQuery(): AssetQuery
{
return $this->assetQueryProvider->createAssetQuery();
}
}
39 changes: 39 additions & 0 deletions src/Controller/Api/V1/Assets/GetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\V1\Assets;

use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

final class GetController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly AssetSearchServiceInterface $assetSearchService,
)
{
parent::__construct($serializer);
}

#[Route('/v1/assets/{id}', name: 'pimcore_studio_api_v1_get_asset', methods: ['GET'])]
public function getAssets(int $id): JsonResponse
{
return $this->jsonLd($this->assetSearchService->getAssetById($id));
}
}
2 changes: 1 addition & 1 deletion src/Controller/Api/V1/TranslationController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
/**
* Pimcore
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
/**
* Pimcore
*
Expand All @@ -13,8 +13,24 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioApiBundle\Controller\Api\Trait;
namespace Pimcore\Bundle\StudioApiBundle\Dto;

trait JsonLdResponseTrait
final readonly class Collection
{
}
public function __construct(
private int $page = 1,
private int $pageSize = 10
)
{
}

public function getPage(): int
{
return $this->page;
}

public function getPageSize(): int
{
return $this->pageSize;
}
}
47 changes: 0 additions & 47 deletions src/EventSubscriber/ApiExceptionListener.php

This file was deleted.

7 changes: 4 additions & 3 deletions src/Service/TranslatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e928601

Please sign in to comment.