Skip to content

Commit

Permalink
api: use uriTemplate feature for Camp -> Profile relation
Browse files Browse the repository at this point in the history
To omit the additional queries to each profile
of each campCollaboration.
  • Loading branch information
BacLuc committed Oct 10, 2023
1 parent e14bf89 commit fe00aa1
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 55 deletions.
7 changes: 4 additions & 3 deletions api/src/Entity/Camp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use ApiPlatform\Metadata\Post;
use App\InputFilter;
use App\Repository\CampRepository;
use App\Serializer\Normalizer\RelatedCollectionLink;
use App\State\CampCreateProcessor;
use App\State\CampRemoveProcessor;
use App\Util\EntityMap;
Expand Down Expand Up @@ -438,8 +437,10 @@ public function removeCampCollaboration(CampCollaboration $collaboration): self
*
* @return Profile[]
*/
#[ApiProperty(example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d')]
#[RelatedCollectionLink(Profile::class, ['user.collaborations.camp' => '$this'])]
#[ApiProperty(
uriTemplate: Profile::CAMP_FILTER_URI_TEMPLATE,
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
)]
#[Groups(['read'])]
public function getProfiles(): array {
$accessibleCampCollaborations = array_filter(
Expand Down
10 changes: 10 additions & 0 deletions api/src/Entity/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Patch;
use App\InputFilter;
use App\Repository\ProfileRepository;
Expand All @@ -34,6 +35,13 @@
new GetCollection(
security: 'is_authenticated()'
),
new GetCollection(
uriTemplate: self::CAMP_FILTER_URI_TEMPLATE,
uriVariables: [
'campIri' => new Link(toProperty: 'user.collaborations.camp', fromClass: Camp::class),
],
security: 'is_authenticated()'
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']]
Expand All @@ -42,6 +50,8 @@
#[ORM\Entity(repositoryClass: ProfileRepository::class)]
#[ORM\Table(name: '`profile`')]
class Profile extends BaseEntity {
public const CAMP_FILTER_URI_TEMPLATE = '/profiles/user.collaborations.camp={campIri}';

public const EXAMPLE_EMAIL = '[email protected]';
public const EXAMPLE_FIRSTNAME = 'Robert';
public const EXAMPLE_SURNAME = 'Baden-Powell';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
/activity_progress_labels/item: 7
/activity_responsibles: 6
/activity_responsibles/item: 8
/camps: 29
/camps: 21
/camps/item: 28
/camp_collaborations: 25
/camp_collaborations/item: 15
/camp_collaborations: 22
/camp_collaborations/item: 12
/categories: 6
/categories/item: 9
/content_nodes: 8
Expand All @@ -32,7 +32,7 @@
/material_lists: 6
/material_lists/item: 7
/periods: 6
/periods/item: 19
/periods/item: 16
/profiles: 6
/profiles/item: 6
/schedule_entries: 23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2150,11 +2150,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -2334,11 +2332,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -2520,11 +2516,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -2706,11 +2700,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -3134,11 +3126,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -3327,11 +3317,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -3513,11 +3501,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -3699,11 +3685,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -4048,11 +4032,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -4255,11 +4237,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -4441,11 +4421,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -4627,11 +4605,9 @@ components:
profiles:
description: 'All profiles of the users collaborating in this camp.'
example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d'
items:
format: iri-reference
type: string
format: iri-reference
readOnly: true
type: array
type: string
progressLabels:
description: 'All the progress labels within this camp.'
example: '["/progress_labels/1a2b3c4d"]'
Expand Down Expand Up @@ -22820,6 +22796,94 @@ paths:
tags:
- Profile
parameters: []
'/profiles/user.collaborations.camp={campIri}':
get:
deprecated: false
description: 'Retrieves the collection of Profile resources.'
operationId: api_profilesuser.collaborations.camp=_campIri_get_collection
parameters:
-
allowEmptyValue: false
allowReserved: false
deprecated: false
description: 'Camp identifier'
explode: false
in: path
name: campIri
required: true
schema:
type: string
style: simple
-
allowEmptyValue: true
allowReserved: false
deprecated: false
description: ''
explode: false
in: query
name: user.collaborations.camp
required: false
schema:
type: string
style: form
-
allowEmptyValue: true
allowReserved: false
deprecated: false
description: ''
explode: true
in: query
name: 'user.collaborations.camp[]'
required: false
schema:
items:
type: string
type: array
style: form
responses:
200:
content:
application/hal+json:
schema:
properties:
_embedded: { items: { $ref: '#/components/schemas/Profile.jsonhal-read' }, type: array }
_links: { properties: { first: { properties: { href: { format: iri-reference, type: string } }, type: object }, last: { properties: { href: { format: iri-reference, type: string } }, type: object }, next: { properties: { href: { format: iri-reference, type: string } }, type: object }, previous: { properties: { href: { format: iri-reference, type: string } }, type: object }, self: { properties: { href: { format: iri-reference, type: string } }, type: object } }, type: object }
itemsPerPage: { minimum: 0, type: integer }
totalItems: { minimum: 0, type: integer }
required:
- _embedded
- _links
type: object
application/json:
schema:
items:
$ref: '#/components/schemas/Profile-read'
type: array
application/ld+json:
schema:
properties:
'hydra:member': { items: { $ref: '#/components/schemas/Profile.jsonld-read' }, type: array }
'hydra:search': { properties: { '@type': { type: string }, 'hydra:mapping': { items: { properties: { '@type': { type: string }, property: { type: ['null', string] }, required: { type: boolean }, variable: { type: string } }, type: object }, type: array }, 'hydra:template': { type: string }, 'hydra:variableRepresentation': { type: string } }, type: object }
'hydra:totalItems': { minimum: 0, type: integer }
'hydra:view': { example: { '@id': string, 'hydra:first': string, 'hydra:last': string, 'hydra:next': string, 'hydra:previous': string, type: string }, properties: { '@id': { format: iri-reference, type: string }, '@type': { type: string }, 'hydra:first': { format: iri-reference, type: string }, 'hydra:last': { format: iri-reference, type: string }, 'hydra:next': { format: iri-reference, type: string }, 'hydra:previous': { format: iri-reference, type: string } }, type: object }
required:
- 'hydra:member'
type: object
application/vnd.api+json:
schema:
items:
$ref: '#/components/schemas/Profile-read'
type: array
text/html:
schema:
items:
$ref: '#/components/schemas/Profile-read'
type: array
description: 'Profile collection'
summary: 'Retrieves the collection of Profile resources.'
tags:
- Profile
parameters: []
'/profiles/{id}':
get:
deprecated: false
Expand Down

0 comments on commit fe00aa1

Please sign in to comment.