Skip to content

Commit 721a2a7

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

1 file changed

Lines changed: 29 additions & 33 deletions

File tree

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

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ The below files structure is what we will have at the end of this tutorial and i
5656
In `src` and `src/Core/src` folders we will create one `Book` folder and in those we will create the `src` folder.
5757
So the final structure will be like this: `src/Book/src` and `src/Core/src/Book/src`.
5858

59-
Each file below have a summary description above of what that file does.
60-
6159
* `src/Book/src/Collection/BookCollection.php`
6260

6361
```php
@@ -176,30 +174,21 @@ namespace Core\Book\Repository;
176174

177175
use Core\App\Repository\AbstractRepository;
178176
use Core\Book\Entity\Book;
179-
use Doctrine\ORM\Query;
177+
use Doctrine\ORM\QueryBuilder;
180178
use Dot\DependencyInjection\Attribute\Entity;
181179

182180
#[Entity(name: Book::class)]
183181
class BookRepository extends AbstractRepository
184182
{
185-
public function saveBook(Book $book): Book
186-
{
187-
$this->getEntityManager()->persist($book);
188-
$this->getEntityManager()->flush();
189-
190-
return $book;
191-
}
192-
193-
public function getBooks(array $params = [], array $filters = []): Query
183+
public function getBooks(array $params = [], array $filters = []): QueryBuilder
194184
{
195185
return $this
196186
->getQueryBuilder()
197187
->select('book')
198188
->from(Book::class, 'book')
199189
->orderBy($filters['order'] ?? 'book.created', $filters['dir'] ?? 'desc')
200-
->setMaxResults($params['limit'])
201-
->getQuery()
202-
->useQueryCache(true);
190+
->setFirstResult($params['offset'])
191+
->setMaxResults($params['limit']);
203192
}
204193
}
205194
```
@@ -213,11 +202,14 @@ declare(strict_types=1);
213202

214203
namespace Api\Book\Service;
215204

216-
use Core\Book\Repository\BookRepository;
205+
use Core\Book\Entity\Book;
206+
use Doctrine\ORM\QueryBuilder;
217207

218208
interface BookServiceInterface
219209
{
220-
public function getRepository(): BookRepository;
210+
public function saveBook(array $data): Book;
211+
212+
public function getBooks(array $filters = []): QueryBuilder;
221213
}
222214
```
223215

@@ -234,6 +226,7 @@ use Core\App\Helper\Paginator;
234226
use Core\Book\Entity\Book;
235227
use Core\Book\Repository\BookRepository;
236228
use DateTimeImmutable;
229+
use Doctrine\ORM\QueryBuilder;
237230
use Dot\DependencyInjection\Attribute\Inject;
238231
use Exception;
239232

@@ -244,29 +237,26 @@ class BookService implements BookServiceInterface
244237
{
245238
}
246239

247-
public function getRepository(): BookRepository
248-
{
249-
return $this->bookRepository;
250-
}
251-
252240
/**
253241
* @throws Exception
254242
*/
255-
public function createBook(array $data): Book
243+
public function saveBook(array $data): Book
256244
{
257245
$book = new Book(
258246
$data['name'],
259247
$data['author'],
260248
new DateTimeImmutable($data['releaseDate'])
261249
);
262250

263-
return $this->bookRepository->saveBook($book);
251+
$this->bookRepository->saveResource($book);
252+
253+
return $book;
264254
}
265255

266-
public function getBooks(array $filters = [])
256+
public function getBooks(array $filters = []): QueryBuilder
267257
{
268-
$params = Paginator::getParams($filters, 'book.created');
269-
258+
$params = Paginator::getParams($filters, 'book.created');
259+
270260
return $this->bookRepository->getBooks($params, $filters);
271261
}
272262
}
@@ -560,7 +550,7 @@ class PostBookResourceHandler extends AbstractHandler implements RequestHandlerI
560550
/** @var non-empty-array<non-empty-string, mixed> $data */
561551
$data = (array) $this->inputFilter->getValues();
562552

563-
return $this->createdResponse($request, $this->bookService->createBook($data));
553+
return $this->createdResponse($request, $this->bookService->saveBook($data));
564554
}
565555
}
566556
```
@@ -642,8 +632,8 @@ declare(strict_types=1);
642632
namespace Api\Book;
643633

644634
use Api\Book\Handler\GetBookCollectionHandler;
645-
use Api\Book\Handler\GetBookHandler;
646-
use Api\Book\Handler\PostBookHandler;
635+
use Api\Book\Handler\GetBookResourceHandler;
636+
use Api\Book\Handler\PostBookResourceHandler;
647637
use Core\App\ConfigProvider;
648638
use Dot\Router\RouteCollectorInterface;
649639
use Mezzio\Application;
@@ -739,7 +729,13 @@ composer dump-autoload
739729

740730
That's it. The module is now registered.
741731

742-
We need to configure access to the newly created endpoints, add `books::list-books`, `book::view-book` and `book::create-book` to the authorization rbac array, under the `UserRole::ROLE_GUEST` key.
732+
We need to configure access to the newly created endpoints.
733+
Open `config/autoload/authorization.global.php` and append the below route names to the `UserRoleEnum::Guest->value` key:
734+
735+
- `books::list-books`
736+
- `book::view-book`
737+
- `book::create-book`
738+
743739
> Make sure you read and understand the rbac [documentation](https://docs.dotkernel.org/dot-rbac-guard/v4/configuration/).
744740
745741
## Migrations
@@ -748,8 +744,8 @@ We created the `Book` entity, but we didn't create the associated table for it.
748744

749745
> You can check the mapping files by running:
750746
751-
```shel
752-
php bin/doctrine orm:validate-schema
747+
```shell
748+
php ./bin/doctrine orm:validate-schema
753749
```
754750

755751
Doctrine can handle the table creation, run the following command:

0 commit comments

Comments
 (0)