-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
435 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# RESTful Webservices in Symfony | ||
|
||
## Coding Challenge 4 - HATEOAS | ||
|
||
### Tasks | ||
|
||
- introduce HATEOAS links for your read and list representations of workshops and attendees | ||
- use the JSON-HAL format | ||
|
||
### Solution | ||
|
||
- add a `links` property to the `PaginatedCollection` class (annotate the getter with `#[SerializedName('_links')]`) | ||
- add `UrlGeneratorInterface` as dependency of `PaginatedCollectionFactory` | ||
- introduce a `addLink(string $rel, string $href)` method in the `PaginatedCollection` class | ||
- add links to the created `PaginatedCollection` (self, next, prev, first, last) | ||
- adjust the AttendeeNormalizer and WorkshopNormalizer and add | ||
- `$data['_links']['self']['href']` (remember to check for is_array($data)) | ||
- `$data['_links']['collection']['href']` (remember to check for is_array($data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Pagination; | ||
|
||
use App\Repository\AttendeeRepository; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; | ||
|
||
final class AttendeeCollectionFactory extends PaginatedCollectionFactory | ||
{ | ||
public function __construct( | ||
private readonly AttendeeRepository $attendeeRepository | ||
) { | ||
} | ||
|
||
public function getRepository(): ServiceEntityRepositoryInterface | ||
{ | ||
return $this->attendeeRepository; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Pagination; | ||
|
||
final class PaginatedCollection | ||
{ | ||
public readonly array $items; | ||
public readonly int $total; | ||
public readonly int $count; | ||
|
||
public function __construct(\Iterator $items, int $total) | ||
{ | ||
$this->items = iterator_to_array($items); | ||
$this->total = $total; | ||
$this->count = \count($this->items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Pagination; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; | ||
use Doctrine\ORM\Tools\Pagination\Paginator; | ||
|
||
abstract class PaginatedCollectionFactory | ||
{ | ||
abstract public function getRepository(): ServiceEntityRepositoryInterface; | ||
|
||
public function create(int $page, int $size): PaginatedCollection | ||
{ | ||
$query = $this->getRepository() | ||
->createQueryBuilder('u') | ||
->orderBy('u.id', 'asc') | ||
->getQuery() | ||
; | ||
|
||
$paginator = new Paginator($query); | ||
$total = count($paginator); | ||
|
||
$paginator | ||
->getQuery() | ||
->setFirstResult($size * ($page - 1)) | ||
->setMaxResults($size); | ||
|
||
return new PaginatedCollection($paginator->getIterator(), $total); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Pagination; | ||
|
||
class PaginationInformation | ||
{ | ||
public function __construct( | ||
public readonly ?int $page = 1, | ||
public readonly ?int $size = 10, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Pagination; | ||
|
||
use App\Repository\WorkshopRepository; | ||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; | ||
|
||
final class WorkshopCollectionFactory extends PaginatedCollectionFactory | ||
{ | ||
public function __construct( | ||
private readonly WorkshopRepository $workshopRepository | ||
) { | ||
} | ||
|
||
public function getRepository(): ServiceEntityRepositoryInterface | ||
{ | ||
return $this->workshopRepository; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 22 additions & 18 deletions
40
...ller/Attendee/__snapshots__/ListControllerTest__test_it_should_list_all_attendees__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
[ | ||
{ | ||
"identifier": "803449f4-9a4c-4ecb-8ce4-cebc804fe70a", | ||
"firstname": "Jan", | ||
"lastname": "Sch\u00e4dlich", | ||
"email": "[email protected]", | ||
"workshops": [ | ||
{ | ||
"identifier": "abba667a-96ae-4f75-9b71-97819b682e8d", | ||
"title": "RESTful Webservices in Symfony", | ||
"workshop_date": "2022-06-14", | ||
"attendees": [ | ||
"Jan Sch\u00e4dlich" | ||
] | ||
} | ||
] | ||
} | ||
] | ||
{ | ||
"items": [ | ||
{ | ||
"identifier": "803449f4-9a4c-4ecb-8ce4-cebc804fe70a", | ||
"firstname": "Jan", | ||
"lastname": "Sch\u00e4dlich", | ||
"email": "[email protected]", | ||
"workshops": [ | ||
{ | ||
"identifier": "abba667a-96ae-4f75-9b71-97819b682e8d", | ||
"title": "RESTful Webservices in Symfony", | ||
"workshop_date": "2022-06-14", | ||
"attendees": [ | ||
"Jan Sch\u00e4dlich" | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"total": 1, | ||
"count": 1 | ||
} |
27 changes: 27 additions & 0 deletions
27
...Test__test_it_should_paginate_attendees with data set show 1st page, 3 items each__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"identifier": "4878f198-36ab-4fe3-8189-19662a9764fa", | ||
"firstname": "a", | ||
"lastname": "1", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "e942ce16-27c2-494f-9d93-03412da980c5", | ||
"firstname": "b", | ||
"lastname": "2", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "4714fb8a-83d8-49af-abbf-7c68fc6c9656", | ||
"firstname": "c", | ||
"lastname": "3", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
} | ||
], | ||
"total": 5, | ||
"count": 3 | ||
} |
41 changes: 41 additions & 0 deletions
41
...Test__test_it_should_paginate_attendees with data set show 1st page, 5 items each__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"identifier": "4878f198-36ab-4fe3-8189-19662a9764fa", | ||
"firstname": "a", | ||
"lastname": "1", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "e942ce16-27c2-494f-9d93-03412da980c5", | ||
"firstname": "b", | ||
"lastname": "2", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "4714fb8a-83d8-49af-abbf-7c68fc6c9656", | ||
"firstname": "c", | ||
"lastname": "3", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "65445e8c-a6c6-4955-9eb2-5fb60d6a991e", | ||
"firstname": "d", | ||
"lastname": "4", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "3aacd688-5b81-4aba-a5ea-ac7668ba95b6", | ||
"firstname": "e", | ||
"lastname": "5", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
} | ||
], | ||
"total": 5, | ||
"count": 5 | ||
} |
20 changes: 20 additions & 0 deletions
20
...Test__test_it_should_paginate_attendees with data set show 2nd page, 3 items each__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"identifier": "65445e8c-a6c6-4955-9eb2-5fb60d6a991e", | ||
"firstname": "d", | ||
"lastname": "4", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
}, | ||
{ | ||
"identifier": "3aacd688-5b81-4aba-a5ea-ac7668ba95b6", | ||
"firstname": "e", | ||
"lastname": "5", | ||
"email": "[email protected]", | ||
"workshops": [] | ||
} | ||
], | ||
"total": 5, | ||
"count": 2 | ||
} |
5 changes: 5 additions & 0 deletions
5
...Test__test_it_should_paginate_attendees with data set show 2nd page, 5 items each__1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"items": [], | ||
"total": 5, | ||
"count": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
App\Entity\Attendee: | ||
attendee_1: | ||
__construct: [ '4878f198-36ab-4fe3-8189-19662a9764fa', 'a', '1', '[email protected]' ] | ||
attendee_2: | ||
__construct: [ 'e942ce16-27c2-494f-9d93-03412da980c5', 'b', '2', '[email protected]' ] | ||
attendee_3: | ||
__construct: [ '4714fb8a-83d8-49af-abbf-7c68fc6c9656', 'c', '3', '[email protected]' ] | ||
attendee_4: | ||
__construct: [ '65445e8c-a6c6-4955-9eb2-5fb60d6a991e', 'd', '4', '[email protected]' ] | ||
attendee_5: | ||
__construct: [ '3aacd688-5b81-4aba-a5ea-ac7668ba95b6', 'e', '5', '[email protected]' ] |
Oops, something went wrong.