feat: GetMangaList endpoint + tests + test db
This commit is contained in:
parent
073439163b
commit
e3d380eadd
13
src/Domain/Manga/Application/Query/GetMangaList.php
Normal file
13
src/Domain/Manga/Application/Query/GetMangaList.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Query;
|
||||
|
||||
readonly class GetMangaList
|
||||
{
|
||||
public function __construct(
|
||||
public ?int $page = 1,
|
||||
public ?int $limit = 20,
|
||||
public ?string $sortBy = 'title',
|
||||
public ?string $sortOrder = 'asc'
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\QueryHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Query\GetMangaList;
|
||||
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||
use App\Domain\Manga\Application\Response\MangaListResponse;
|
||||
|
||||
readonly class GetMangaListHandler
|
||||
{
|
||||
public function __construct(
|
||||
private MangaRepositoryInterface $mangaRepository
|
||||
) {}
|
||||
|
||||
public function handle(GetMangaList $query): MangaListResponse
|
||||
{
|
||||
$mangas = $this->mangaRepository->findAll(
|
||||
page: $query->page,
|
||||
limit: $query->limit,
|
||||
sortBy: $query->sortBy,
|
||||
sortOrder: $query->sortOrder
|
||||
);
|
||||
|
||||
$total = $this->mangaRepository->count();
|
||||
|
||||
return new MangaListResponse(
|
||||
mangas: $mangas,
|
||||
total: $total,
|
||||
page: $query->page,
|
||||
limit: $query->limit
|
||||
);
|
||||
}
|
||||
}
|
||||
28
src/Domain/Manga/Application/Response/MangaListResponse.php
Normal file
28
src/Domain/Manga/Application/Response/MangaListResponse.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Response;
|
||||
|
||||
readonly class MangaListResponse
|
||||
{
|
||||
public function __construct(
|
||||
public array $mangas,
|
||||
public int $total,
|
||||
public int $page,
|
||||
public int $limit
|
||||
) {}
|
||||
|
||||
public function getTotalPages(): int
|
||||
{
|
||||
return (int) ceil($this->total / $this->limit);
|
||||
}
|
||||
|
||||
public function hasNextPage(): bool
|
||||
{
|
||||
return $this->page < $this->getTotalPages();
|
||||
}
|
||||
|
||||
public function hasPreviousPage(): bool
|
||||
{
|
||||
return $this->page > 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user