Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/api filters #242

Merged
merged 3 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions config/api_platform/filters/Attendance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
services:
_defaults:
autowire: false
autoconfigure: false
public: false

attendance.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
tags: [ 'api_platform.filter' ]
arguments:
- id: 'exact'
missionId: 'exact'
playerId: 'exact'

attendance.date_filter:
parent: 'api_platform.doctrine.orm.date_filter'
tags: [ 'api_platform.filter' ]
arguments:
- createdAt: ~

attendance.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
tags: [ 'api_platform.filter' ]
arguments:
- createdAt: ~
missionId: ~
playerId: ~
30 changes: 30 additions & 0 deletions config/api_platform/filters/ModList.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
services:
_defaults:
autowire: false
autoconfigure: false
public: false

mod_list.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
tags: [ 'api_platform.filter' ]
arguments:
- id: 'exact'
createdBy.id: 'exact'
lastUpdatedBy.id: 'exact'
name: 'partial'
description: 'partial'

mod_list.date_filter:
parent: 'api_platform.doctrine.orm.date_filter'
tags: [ 'api_platform.filter' ]
arguments:
- createdAt: ~
lastUpdatedAt: ~

mod_list.order_filter:
parent: 'api_platform.doctrine.orm.order_filter'
tags: [ 'api_platform.filter' ]
arguments:
- createdAt: ~
lastUpdatedAt: ~
name: ~
17 changes: 17 additions & 0 deletions config/api_platform/resources/Attendance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
App\Entity\Attendance\Attendance:
attributes:
input: 'App\Api\Input\Attendance\AttendanceInput'
output: 'App\Api\Output\Attendance\AttendanceOutput'

itemOperations:
get: ~

collectionOperations:
get:
filters:
- 'attendance.search_filter'
- 'attendance.date_filter'
- 'attendance.order_filter'

post:
input: 'App\Api\Input\Attendance\AttendanceInput'
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
App\Entity\Attendance\Attendance:
attributes:
input: 'App\Api\Input\Attendance\AttendanceInput'
output: 'App\Api\Output\Attendance\AttendanceOutput'

itemOperations:
get: ~

collectionOperations:
get: ~
post:
input: 'App\Api\Input\Attendance\AttendanceInput'


App\Entity\ModList\ModList:
attributes:
output: 'App\Api\Output\ModList\ModListOutput'

itemOperations:
get:
output: 'App\Api\Output\ModList\ModListDetailsOutput'

get_by_name:
method: 'GET'
path: '/mod-lists/by-name/{name}'
Expand All @@ -27,4 +14,8 @@ App\Entity\ModList\ModList:
read: false

collectionOperations:
get: ~
get:
filters:
- 'mod_list.search_filter'
- 'mod_list.date_filter'
- 'mod_list.order_filter'
14 changes: 13 additions & 1 deletion config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: ../api_platform/filters/ }

api_platform:
title: "%app.api.name%"
version: "%app.api.version%"
Expand All @@ -11,9 +14,18 @@ api_platform:

path_segment_name_generator: api_platform.path_segment_name_generator.dash
mapping:
paths: ['%kernel.project_dir%/config/api_platform']
paths: ['%kernel.project_dir%/config/api_platform/resources']
patch_formats:
json: ['application/merge-patch+json']
formats:
json: ['application/json']
html: ['text/html']

defaults:
pagination_enabled: true
pagination_maximum_items_per_page: 50
pagination_client_items_per_page: true
collection:
pagination:
items_per_page_parameter_name: limit
page_parameter_name: page
38 changes: 38 additions & 0 deletions src/Api/Serializer/Normalizer/PaginatorNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Api\Serializer\Normalizer;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class PaginatorNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

public function normalize($object, string $format = null, array $context = []): array
{
/** @var Paginator $object */
$data = [];
foreach ($object->getIterator() as $item) {
$data[] = $this->normalizer->normalize($item, $format, $context);
}

return [
'data' => $data,
'items' => $object->count(),
'totalItems' => $object->getTotalItems(),
'currentPage' => $object->getCurrentPage(),
'lastPage' => $object->getLastPage(),
'itemsPerPage' => $object->getItemsPerPage(),
];
}

public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof Paginator && 'json' === $format;
}
}
28 changes: 0 additions & 28 deletions src/Service/Serializer/CircularReferenceHandler.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public function getModListsAction_authorizedUser_returnsSuccessfulResponse(strin

$this::assertResponseStatusCodeSame(Response::HTTP_OK);
$this::assertJsonContains([
[
'id' => 'f3e04dae-18a8-4533-99ea-d6d763ebabcf',
'name' => 'Default',
'active' => true,
'approved' => false,
'createdAt' => '2020-01-01T00:00:00+01:00',
'lastUpdatedAt' => null,
'data' => [
[
'id' => 'f3e04dae-18a8-4533-99ea-d6d763ebabcf',
'name' => 'Default',
'active' => true,
'approved' => false,
'createdAt' => '2020-01-01T00:00:00+01:00',
'lastUpdatedAt' => null,
],
],
]);
}
Expand Down
55 changes: 0 additions & 55 deletions tests/unit/Service/Serializer/CircularReferenceHandlerTest.php

This file was deleted.