Skip to content

Commit

Permalink
[Improvement] Switch from GET to POST for notes (#721)
Browse files Browse the repository at this point in the history
* Switched collection controller from get to post

* Apply php-cs-fixer changes

* Fixed FilterServiceTest.php

* Fixed FilterServiceTest.php

* Apply php-cs-fixer changes
  • Loading branch information
mcop1 authored Jan 23, 2025
1 parent 2bd27dd commit 385b638
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 40 deletions.
92 changes: 92 additions & 0 deletions src/Note/Attribute/Request/NotesCollectionRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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\StudioBackendBundle\Note\Attribute\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;

/**
* @internal
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class NotesCollectionRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(
required: ['page', 'pageSize'],
properties: [
new Property(
property: 'page',
type: 'integer',
example: 1
),
new Property(
property: 'pageSize',
type: 'integer',
example: 50
),
new Property(
property: 'sortOrder',
description: 'Sort order (asc or desc).',
type: 'string',
enum: [
'ASC',
'DESC',
],
example: 'ASC'
),
new Property(
property: 'sortBy',
description: 'Sort by field. Only works in combination with sortOrder.',
type: 'string',
enum: [
'id',
'type',
'cId',
'cType',
'cPath',
'date',
'title',
'description',
'locked',
],
example: 'id',
),
new Property(
property: 'filter',
description: 'Filter for notes',
type: 'string',
example: ''
),
new Property(
property: 'fieldFilters',
description: 'Filter for specific fields, will be json decoded to an array. e.g.
[{"operator":"like","value":"John","field":"name","type":"string"}]',
type: 'object',
example: '[{"operator":"like","value":"consent-given","field":"type","type":"string"}]'
),
],
type: 'object',
),
);
}
}
24 changes: 7 additions & 17 deletions src/Note/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

namespace Pimcore\Bundle\StudioBackendBundle\Note\Controller;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterException;
use Pimcore\Bundle\StudioBackendBundle\Note\Attribute\Parameter\Query\NoteSortByParameter;
use Pimcore\Bundle\StudioBackendBundle\Note\Attribute\Request\NotesCollectionRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Note\MappedParameter\NoteElementParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\MappedParameter\NoteParameters;
use Pimcore\Bundle\StudioBackendBundle\Note\Schema\Note;
use Pimcore\Bundle\StudioBackendBundle\Note\Service\NoteServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\FieldFilterParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\FilterParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageSizeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\SortOrderParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\GenericCollection;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\CollectionJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
Expand All @@ -38,7 +33,7 @@
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -60,21 +55,16 @@ public function __construct(
/**
* @throws InvalidFilterException
*/
#[Route('/notes', name: 'pimcore_studio_api_get_notes', methods: ['GET'])]
#[Route('/notes', name: 'pimcore_studio_api_get_notes', methods: ['POST'])]
#[IsGranted(UserPermissions::NOTES_EVENTS->value)]
#[Get(
#[Post(
path: self::PREFIX . '/notes',
operationId: 'note_get_collection',
description: 'note_get_collection_description',
summary: 'note_get_collection_summary',
tags: [Tags::Notes->name]
)]
#[PageParameter]
#[PageSizeParameter(50)]
#[NoteSortByParameter]
#[SortOrderParameter]
#[FilterParameter('notes')]
#[FieldFilterParameter]
#[NotesCollectionRequestBody]
#[SuccessResponse(
description: 'note_get_collection_success_response',
content: new CollectionJson(new GenericCollection(Note::class))
Expand All @@ -83,7 +73,7 @@ public function __construct(
HttpResponseCodes::UNAUTHORIZED,
])]
public function getNotes(
#[MapQueryString] NoteParameters $parameters = new NoteParameters()
#[MapRequestPayload] NoteParameters $parameters
): JsonResponse {
$collection = $this->noteService->listNotes(new NoteElementParameters(), $parameters);

Expand Down
14 changes: 2 additions & 12 deletions src/Note/MappedParameter/NoteParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace Pimcore\Bundle\StudioBackendBundle\Note\MappedParameter;

use JsonException;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\CollectionParameters;

/**
Expand All @@ -30,7 +29,7 @@ public function __construct(
private ?string $sortBy = null,
private ?string $sortOrder = null,
private ?string $filter = null,
private ?string $fieldFilters = null,
private ?array $fieldFilters = null,
) {
parent::__construct($page, $pageSize);
}
Expand All @@ -50,17 +49,8 @@ public function getFilter(): ?string
return $this->filter;
}

/**
* @throws JsonException
*/
public function getFieldFilters(): ?array
{
return $this->fieldFilters === null ? null :
json_decode(
$this->fieldFilters,
true,
512,
JSON_THROW_ON_ERROR
);
return $this->fieldFilters;
}
}
26 changes: 15 additions & 11 deletions tests/Unit/Note/Service/FilterServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public function testApplyFieldFiltersDate(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: json_encode([
fieldFilters: [
[
'field' => 'date',
'type' => 'date',
'operator' => 'eq',
'value' => '05/04/2024',
],
], JSON_THROW_ON_ERROR)
],
);
$this->filterService->applyFieldFilters($noteListing, $noteParameters);

Expand All @@ -86,14 +86,14 @@ public function testApplyFieldFiltersNumeric(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: json_encode([
fieldFilters: [
[
'field' => 'numeric',
'type' => 'numeric',
'operator' => 'eq',
'value' => 10,
],
], JSON_THROW_ON_ERROR)
],
);
$this->filterService->applyFieldFilters($noteListing, $noteParameters);

Expand All @@ -113,14 +113,14 @@ public function testApplyFieldFiltersBoolean(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: json_encode([
fieldFilters: [
[
'field' => 'boolean',
'type' => 'boolean',
'operator' => 'boolean',
'value' => true,
],
], JSON_THROW_ON_ERROR)
],
);
$this->filterService->applyFieldFilters($noteListing, $noteParameters);

Expand All @@ -140,14 +140,14 @@ public function testApplyFieldFiltersList(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: json_encode([
fieldFilters: [
[
'field' => 'list',
'type' => 'list',
'operator' => 'list',
'value' => 'list',
],
], JSON_THROW_ON_ERROR)
],
);
$this->filterService->applyFieldFilters($noteListing, $noteParameters);

Expand All @@ -167,14 +167,14 @@ public function testApplyFieldFiltersUser(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: json_encode([
fieldFilters: [
[
'field' => 'user',
'type' => 'user',
'operator' => 'user',
'value' => 'admin',
],
], JSON_THROW_ON_ERROR)
],
);
$this->filterService->applyFieldFilters($noteListing, $noteParameters);

Expand All @@ -197,7 +197,11 @@ public function testApplyFieldFiltersInvalidJson(): void
{
$noteListing = $this->getNoteListing();
$noteParameters = new NoteParameters(
fieldFilters: 'invalid'
fieldFilters: [
[
'invalidKey' => 'invalidValue',
],
],
);

$this->expectException(InvalidFilterException::class);
Expand Down

0 comments on commit 385b638

Please sign in to comment.