@@ -1374,7 +1374,7 @@ <h2 id="file-creation-and-contents">File creation and contents</h2>
13741374 ], true);
13751375
13761376$this->add($nameInput);</ code > </ pre >
1377- < p > Now it's time to create the handler .</ p >
1377+ < p > Now it's time to create the handlers for the book entity and book collection .</ p >
13781378< ul >
13791379< li > < code > src/Book/src/Handler/BookHandler.php</ code > </ li >
13801380</ ul >
@@ -1394,19 +1394,18 @@ <h2 id="file-creation-and-contents">File creation and contents</h2>
13941394use Psr\Http\Message\ResponseInterface;
13951395use Psr\Http\Message\ServerRequestInterface;
13961396use Psr\Http\Server\RequestHandlerInterface;
1397- use Dot\DependencyInjection\Attribute\Inject;
13981397
13991398class BookHandler extends AbstractHandler implements RequestHandlerInterface
14001399{
14011400 #[Inject(
14021401 BookServiceInterface::class,
1403- "config"
1402+ "config",
14041403 HalResponseFactory::class,
14051404 ResourceGenerator::class,
14061405 )]
14071406 public function __construct(
14081407 protected BookServiceInterface $bookService,
1409- protected array $config
1408+ protected array $config,
14101409 protected ?HalResponseFactory $responseFactory = null,
14111410 protected ?ResourceGenerator $resourceGenerator = null,
14121411 ) {
@@ -1441,16 +1440,63 @@ <h2 id="file-creation-and-contents">File creation and contents</h2>
14411440
14421441 return $this->createResponse($request, $book);
14431442 }
1444- }
1445- </ code > </ pre >
1443+ }</ code > </ pre >
1444+ < ul >
1445+ < li > < code > src/Book/src/Handler/BookCollectionHandler.php</ code > </ li >
1446+ </ ul >
1447+ < pre class ="highlight "> < code class ="language-php "> <?php
1448+
1449+ declare(strict_types=1);
1450+
1451+ namespace Api\Book\Handler;
1452+
1453+ use Api\App\Exception\BadRequestException;
1454+ use Api\App\Handler\AbstractHandler;
1455+ use Api\Book\Service\BookServiceInterface;
1456+ use Dot\DependencyInjection\Attribute\Inject;
1457+ use Mezzio\Hal\HalResponseFactory;
1458+ use Mezzio\Hal\ResourceGenerator;
1459+ use Psr\Http\Message\ResponseInterface;
1460+ use Psr\Http\Message\ServerRequestInterface;
1461+
1462+ class BookCollectionHandler extends AbstractHandler
1463+ {
1464+ #[Inject(
1465+ BookServiceInterface::class,
1466+ "config",
1467+ HalResponseFactory::class,
1468+ ResourceGenerator::class,
1469+ )]
1470+ public function __construct(
1471+ protected BookServiceInterface $bookService,
1472+ protected array $config,
1473+ protected ?HalResponseFactory $responseFactory = null,
1474+ protected ?ResourceGenerator $resourceGenerator = null,
1475+ ) {
1476+ }
1477+
1478+ /**
1479+ * @throws BadRequestException
1480+ */
1481+ public function get(ServerRequestInterface $request): ResponseInterface
1482+ {
1483+ return $this->createResponse($request, $this->bookService->getBooks(
1484+ $request->getQueryParams()
1485+ ));
1486+ }
1487+ }</ code > </ pre >
14461488< p > After we have the handler, we need to register some routes in the < code > RoutesDelegator</ code > , the same we created when we registered the module.</ p >
14471489< ul >
14481490< li > < code > src/Book/src/RoutesDelegator.php</ code > </ li >
14491491</ ul >
14501492< pre class ="highlight "> < code class ="language-php "> <?php
14511493
1494+ declare(strict_types=1);
1495+
14521496namespace Api\Book;
14531497
1498+ use Api\Book\Collection\BookCollection;
1499+ use Api\Book\Handler\BookCollectionHandler;
14541500use Api\Book\Handler\BookHandler;
14551501use Mezzio\Application;
14561502use Psr\Container\ContainerInterface;
@@ -1466,13 +1512,13 @@ <h2 id="file-creation-and-contents">File creation and contents</h2>
14661512
14671513 $app->get(
14681514 '/books',
1469- BookHandler ::class,
1515+ BookCollectionHandler ::class,
14701516 'books.list'
14711517 );
14721518
14731519 $app->get(
1474- '/book/'. $uuid,
1475- BookHandler ::class,
1520+ '/book/' . $uuid,
1521+ BookCollection ::class,
14761522 'book.show'
14771523 );
14781524
@@ -1535,6 +1581,7 @@ <h2 id="file-creation-and-contents">File creation and contents</h2>
15351581 BookHandler::class => AttributedServiceFactory::class,
15361582 BookService::class => AttributedServiceFactory::class,
15371583 BookRepository::class => AttributedRepositoryFactory::class,
1584+ BookCollectionHandler::class => AttributedServiceFactory::class,
15381585 ],
15391586 'aliases' => [
15401587 BookServiceInterface::class => BookService::class,
0 commit comments