Skip to content

Commit e31e348

Browse files
committed
Issue #110: update book tutorial
Signed-off-by: horea <horea@rospace.com>
1 parent 94f9083 commit e31e348

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

docs/book/v6/tutorials/create-book-module.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use Api\App\Collection\ResourceCollection;
7070
class BookCollection extends ResourceCollection
7171
{
7272
}
73+
7374
```
7475

7576
* `src/Core/src/Book/src/Entity/Book.php`
@@ -191,6 +192,7 @@ class BookRepository extends AbstractRepository
191192
->setMaxResults($params['limit']);
192193
}
193194
}
195+
194196
```
195197

196198
* `src/Book/src/Service/BookServiceInterface.php`
@@ -214,6 +216,7 @@ interface BookServiceInterface
214216

215217
public function getBooks(array $params): QueryBuilder;
216218
}
219+
217220
```
218221

219222
* `src/Book/src/Service/BookService.php`
@@ -283,6 +286,7 @@ class BookService implements BookServiceInterface
283286
return $this->bookRepository->getBooks($params);
284287
}
285288
}
289+
286290
```
287291

288292
When creating or updating a book, we will need some validators, so we will create input filters that will be used to validate the data received in the request
@@ -320,6 +324,7 @@ class AuthorInput extends Input
320324
], true);
321325
}
322326
}
327+
323328
```
324329

325330
* `src/Book/src/InputFilter/Input/NameInput.php`
@@ -355,6 +360,7 @@ class NameInput extends Input
355360
], true);
356361
}
357362
}
363+
358364
```
359365

360366
* `src/Book/src/InputFilter/Input/ReleaseDateInput.php`
@@ -390,6 +396,7 @@ class ReleaseDateInput extends Input
390396
], true);
391397
}
392398
}
399+
393400
```
394401

395402
Now we add all the inputs together in a parent input filter.
@@ -406,7 +413,7 @@ namespace Api\Book\InputFilter;
406413
use Api\Book\InputFilter\Input\AuthorInput;
407414
use Api\Book\InputFilter\Input\NameInput;
408415
use Api\Book\InputFilter\Input\ReleaseDateInput;
409-
use Core\Book\InputFilter\AbstractInputFilter;
416+
use Core\App\InputFilter\AbstractInputFilter;
410417

411418
class CreateBookInputFilter extends AbstractInputFilter
412419
{
@@ -417,6 +424,7 @@ class CreateBookInputFilter extends AbstractInputFilter
417424
$this->add(new ReleaseDateInput('releaseDate'));
418425
}
419426
}
427+
420428
```
421429

422430
We create separate `Input` files to demonstrate their reusability and obtain a clean `CreateBookInputFilter` but you could have all the inputs created directly in the `CreateBookInputFilter` like this:
@@ -501,13 +509,16 @@ class GetBookCollectionHandler extends AbstractHandler
501509
);
502510
}
503511
}
512+
504513
```
505514

506515
* `src/Book/src/Handler/GetBookResourceHandler.php`
507516

508517
```php
509518
<?php
510519

520+
declare(strict_types=1);
521+
511522
namespace Api\Book\Handler;
512523

513524
use Api\App\Attribute\Resource;
@@ -527,6 +538,7 @@ class GetBookResourceHandler extends AbstractHandler
527538
);
528539
}
529540
}
541+
530542
```
531543

532544
* `src/Book/src/Handler/PostBookResourceHandler.php`
@@ -546,9 +558,8 @@ use Core\App\Message;
546558
use Dot\DependencyInjection\Attribute\Inject;
547559
use Psr\Http\Message\ResponseInterface;
548560
use Psr\Http\Message\ServerRequestInterface;
549-
use Psr\Http\Server\RequestHandlerInterface;
550561

551-
class PostBookResourceHandler extends AbstractHandler implements RequestHandlerInterface
562+
class PostBookResourceHandler extends AbstractHandler
552563
{
553564
#[Inject(
554565
CreateBookInputFilter::class,
@@ -560,6 +571,9 @@ class PostBookResourceHandler extends AbstractHandler implements RequestHandlerI
560571
) {
561572
}
562573

574+
/**
575+
* @throws BadRequestException
576+
*/
563577
public function handle(ServerRequestInterface $request): ResponseInterface
564578
{
565579
$this->inputFilter->setData((array) $request->getParsedBody());
@@ -576,6 +590,7 @@ class PostBookResourceHandler extends AbstractHandler implements RequestHandlerI
576590
return $this->createdResponse($request, $this->bookService->saveBook($data));
577591
}
578592
}
593+
579594
```
580595

581596
In `src/Book/src` we now create the 2 PHP files: `RoutesDelegator.php` and `ConfigProvider.php`.
@@ -643,6 +658,7 @@ class ConfigProvider
643658
];
644659
}
645660
}
661+
646662
```
647663

648664
* `src/Book/src/RoutesDelegator.php`
@@ -684,6 +700,7 @@ class RoutesDelegator
684700
return $callback();
685701
}
686702
}
703+
687704
```
688705

689706
In `src/Core/src/Book/src` we will create `ConfigProvider.php` where we configure Doctrine ORM.
@@ -738,12 +755,13 @@ class ConfigProvider
738755
];
739756
}
740757
}
758+
741759
```
742760

743761
### Registering the module
744762

745-
* register the module config by adding `Api\Book\ConfigProvider::class` and `Core\Book\ConfigProvider::class` in `config/config.php` under the `Api\User\ConfigProvider::class`
746-
* register the namespace by adding this line `"Api\\Book\\": "src/Book/src/"` and `"Core\\Book\\": "src/Core/src/Book/src/"`, in composer.json under the autoload.psr-4 key
763+
* register the module config by adding `Api\Book\ConfigProvider::class,` and `Core\Book\ConfigProvider::class,` in `config/config.php` under the `Api\User\ConfigProvider::class,`
764+
* register the namespace by adding this line `"Api\\Book\\": "src/Book/src/"` and `"Core\\Book\\": "src/Core/src/Book/src/"`, in `composer.json` under the `autoload`.`psr-4` key
747765
* update Composer autoloader by running the command:
748766

749767
```shell
@@ -755,7 +773,7 @@ That's it. The module is now registered.
755773
We need to configure access to the newly created endpoints.
756774
Open `config/autoload/authorization.global.php` and append the below route names to the `UserRoleEnum::Guest->value` key:
757775

758-
* `books::list-books`
776+
* `book::list-books`
759777
* `book::view-book`
760778
* `book::create-book`
761779

@@ -787,7 +805,13 @@ php ./vendor/bin/doctrine-migrations migrate
787805

788806
## Checking endpoints
789807

790-
If we did everything as planned, we can call the `http://0.0.0.0:8080/book` endpoint and create a new book:
808+
First, we start a local server by executing:
809+
810+
```shell
811+
composer serve
812+
```
813+
814+
If we did everything as planned, we should be able to create a new book by executing the below command:
791815

792816
```shell
793817
curl -X POST http://0.0.0.0:8080/book
@@ -801,7 +825,7 @@ To list the books use:
801825
curl http://0.0.0.0:8080/books
802826
```
803827

804-
To retrieve a book use:
828+
To retrieve a book, `curl` one of the links found in the output of the **list books** command, under `_embedded` . `books` . * . `_links` . `self` . `href`
805829

806830
```shell
807831
curl http://0.0.0.0:8080/book/{uuid}

0 commit comments

Comments
 (0)