Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions docs/book/v6/tutorials/create-book-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ use Dot\DependencyInjection\Attribute\Entity;
#[Entity(name: Book::class)]
class BookRepository extends AbstractRepository
{
public function getBooks(array $params = [], array $filters = []): QueryBuilder
public function getBooks(array $params = []): QueryBuilder
{
return $this
->getQueryBuilder()
->select('book')
->from(Book::class, 'book')
->orderBy($filters['order'] ?? 'book.created', $filters['dir'] ?? 'desc')
->orderBy($params['sort'], $params['dir'])
->setFirstResult($params['offset'])
->setMaxResults($params['limit']);
}
Expand All @@ -203,13 +203,16 @@ declare(strict_types=1);
namespace Api\Book\Service;

use Core\Book\Entity\Book;
use Core\Book\Repository\BookRepository;
use Doctrine\ORM\QueryBuilder;

interface BookServiceInterface
{
public function getBookRepository(): BookRepository;

public function saveBook(array $data): Book;

public function getBooks(array $filters = []): QueryBuilder;
public function getBooks(array $params): QueryBuilder;
}
```

Expand All @@ -230,11 +233,19 @@ use Doctrine\ORM\QueryBuilder;
use Dot\DependencyInjection\Attribute\Inject;
use Exception;

use function in_array;

class BookService implements BookServiceInterface
{
#[Inject(BookRepository::class)]
public function __construct(protected BookRepository $bookRepository)
public function __construct(
protected BookRepository $bookRepository
) {
}

public function getBookRepository(): BookRepository
{
return $this->bookRepository;
}

/**
Expand All @@ -253,11 +264,23 @@ class BookService implements BookServiceInterface
return $book;
}

public function getBooks(array $filters = []): QueryBuilder
public function getBooks(array $params = []): QueryBuilder
{
$params = Paginator::getParams($filters, 'book.created');
$filters = $params['filters'] ?? [];
$params = Paginator::getParams($filters, 'book.created');

$sortableColumns = [
'book.name',
'book.author',
'book.releaseDate',
'book.created',
];

if (! in_array($params['sort'], $sortableColumns, true)) {
$params['sort'] = 'book.created';
}

return $this->bookRepository->getBooks($params, $filters);
return $this->bookRepository->getBooks($params);
}
}
```
Expand Down Expand Up @@ -596,13 +619,13 @@ class ConfigProvider
return [
'delegators' => [
Application::class => [RoutesDelegator::class],
PostBookResourceHandler::class => [HandlerDelegatorFactory::class],
GetBookResourceHandler::class => [HandlerDelegatorFactory::class],
PostBookResourceHandler::class => [HandlerDelegatorFactory::class],
GetBookResourceHandler::class => [HandlerDelegatorFactory::class],
GetBookCollectionHandler::class => [HandlerDelegatorFactory::class],
],
'factories' => [
PostBookResourceHandler::class => AttributedServiceFactory::class,
GetBookResourceHandler::class => AttributedServiceFactory::class,
PostBookResourceHandler::class => AttributedServiceFactory::class,
GetBookResourceHandler::class => AttributedServiceFactory::class,
GetBookCollectionHandler::class => AttributedServiceFactory::class,
BookService::class => AttributedServiceFactory::class,
],
Expand Down