feat: GetChapters endpoint + tests
This commit is contained in:
parent
2f615a4936
commit
6667cc224b
13
src/Domain/Manga/Application/Query/GetMangaChapters.php
Normal file
13
src/Domain/Manga/Application/Query/GetMangaChapters.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Query;
|
||||
|
||||
readonly class GetMangaChapters
|
||||
{
|
||||
public function __construct(
|
||||
public string $mangaId,
|
||||
public ?int $page = 1,
|
||||
public ?int $limit = 20,
|
||||
public ?string $sortOrder = 'desc'
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\QueryHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Query\GetMangaChapters;
|
||||
use App\Domain\Manga\Application\Response\ChapterListResponse;
|
||||
use App\Domain\Manga\Application\Response\ChapterResponse;
|
||||
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
||||
|
||||
readonly class GetMangaChaptersHandler
|
||||
{
|
||||
public function __construct(
|
||||
private MangaRepositoryInterface $mangaRepository
|
||||
) {}
|
||||
|
||||
public function handle(GetMangaChapters $query): ChapterListResponse
|
||||
{
|
||||
$manga = $this->mangaRepository->findById($query->mangaId);
|
||||
if (!$manga) {
|
||||
throw new MangaNotFoundException();
|
||||
}
|
||||
|
||||
$chapters = $this->mangaRepository->findChapters(
|
||||
mangaId: $query->mangaId,
|
||||
page: $query->page,
|
||||
limit: $query->limit,
|
||||
sortOrder: $query->sortOrder
|
||||
);
|
||||
|
||||
$total = $this->mangaRepository->countChapters($query->mangaId);
|
||||
|
||||
return new ChapterListResponse(
|
||||
chapters: array_map(
|
||||
fn ($chapter) => new ChapterResponse(
|
||||
id: $chapter->getId(),
|
||||
number: $chapter->getNumber(),
|
||||
title: $chapter->getTitle(),
|
||||
volume: $chapter->getVolume(),
|
||||
isVisible: $chapter->isVisible(),
|
||||
createdAt: $chapter->getCreatedAt()
|
||||
),
|
||||
$chapters
|
||||
),
|
||||
total: $total,
|
||||
page: $query->page,
|
||||
limit: $query->limit
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Response;
|
||||
|
||||
readonly class ChapterListResponse
|
||||
{
|
||||
public function __construct(
|
||||
public array $chapters,
|
||||
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;
|
||||
}
|
||||
}
|
||||
15
src/Domain/Manga/Application/Response/ChapterResponse.php
Normal file
15
src/Domain/Manga/Application/Response/ChapterResponse.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Response;
|
||||
|
||||
readonly class ChapterResponse
|
||||
{
|
||||
public function __construct(
|
||||
public string $id,
|
||||
public float $number,
|
||||
public ?string $title,
|
||||
public ?int $volume,
|
||||
public bool $isVisible,
|
||||
public \DateTimeImmutable $createdAt
|
||||
) {}
|
||||
}
|
||||
Reference in New Issue
Block a user