feat: GetChapters endpoint + tests
This commit is contained in:
parent
2f615a4936
commit
6667cc224b
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
|
||||
|
||||
readonly class ChapterCollection
|
||||
{
|
||||
public function __construct(
|
||||
/** @var ChapterListItem[] */
|
||||
public array $items,
|
||||
public int $total,
|
||||
public int $page,
|
||||
public int $limit,
|
||||
public bool $hasNextPage,
|
||||
public bool $hasPreviousPage
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
|
||||
readonly class ChapterListItem
|
||||
{
|
||||
public function __construct(
|
||||
#[ApiProperty(identifier: true)]
|
||||
public string $id,
|
||||
public float $number,
|
||||
public ?string $title,
|
||||
public ?int $volume,
|
||||
public bool $isVisible,
|
||||
public string $createdAt
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\ChapterCollection;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\GetMangaChaptersStateProvider;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'MangaChapters',
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/mangas/{id}/chapters',
|
||||
provider: GetMangaChaptersStateProvider::class,
|
||||
output: ChapterCollection::class
|
||||
)
|
||||
]
|
||||
)]
|
||||
class MangaChaptersResource
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Domain\Manga\Application\Query\GetMangaChapters;
|
||||
use App\Domain\Manga\Application\QueryHandler\GetMangaChaptersHandler;
|
||||
use App\Domain\Manga\Application\Response\ChapterResponse;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\ChapterCollection;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\ChapterListItem;
|
||||
|
||||
readonly class GetMangaChaptersStateProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private GetMangaChaptersHandler $handler
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ChapterCollection
|
||||
{
|
||||
$page = $context['filters']['page'] ?? 1;
|
||||
$limit = $context['filters']['limit'] ?? 20;
|
||||
$sortOrder = $context['filters']['sortOrder'] ?? 'desc';
|
||||
|
||||
$query = new GetMangaChapters(
|
||||
mangaId: $uriVariables['id'],
|
||||
page: $page,
|
||||
limit: $limit,
|
||||
sortOrder: $sortOrder
|
||||
);
|
||||
|
||||
$response = $this->handler->handle($query);
|
||||
|
||||
return new ChapterCollection(
|
||||
items: array_map(
|
||||
fn (ChapterResponse $chapter) => $this->createChapterListItem($chapter),
|
||||
$response->chapters
|
||||
),
|
||||
total: $response->total,
|
||||
page: $response->page,
|
||||
limit: $response->limit,
|
||||
hasNextPage: $response->hasNextPage(),
|
||||
hasPreviousPage: $response->hasPreviousPage()
|
||||
);
|
||||
}
|
||||
|
||||
private function createChapterListItem(ChapterResponse $chapter): ChapterListItem
|
||||
{
|
||||
return new ChapterListItem(
|
||||
id: $chapter->id,
|
||||
number: $chapter->number,
|
||||
title: $chapter->title,
|
||||
volume: $chapter->volume,
|
||||
isVisible: $chapter->isVisible,
|
||||
createdAt: $chapter->createdAt->format(\DateTimeInterface::RFC3339)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,9 @@ use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
|
||||
use App\Entity\Manga as EntityManga;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Domain\Manga\Domain\Model\Chapter;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||
use App\Entity\Chapter as EntityChapter;
|
||||
|
||||
readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
{
|
||||
@@ -71,6 +74,36 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function findChapters(string $mangaId, int $page = 1, int $limit = 20, string $sortOrder = 'desc'): array
|
||||
{
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
$queryBuilder = $this->entityManager->createQueryBuilder()
|
||||
->select('c')
|
||||
->from(EntityChapter::class, 'c')
|
||||
->where('c.manga = :mangaId')
|
||||
->orderBy('c.number', $sortOrder)
|
||||
->setParameter('mangaId', $mangaId)
|
||||
->setFirstResult($offset)
|
||||
->setMaxResults($limit);
|
||||
|
||||
return array_map(
|
||||
fn (EntityChapter $entity) => $this->toChapterDomain($entity),
|
||||
$queryBuilder->getQuery()->getResult()
|
||||
);
|
||||
}
|
||||
|
||||
public function countChapters(string $mangaId): int
|
||||
{
|
||||
return $this->entityManager->createQueryBuilder()
|
||||
->select('COUNT(c.id)')
|
||||
->from(EntityChapter::class, 'c')
|
||||
->where('c.manga = :mangaId')
|
||||
->setParameter('mangaId', $mangaId)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function toDomain(EntityManga $entity): DomainManga
|
||||
{
|
||||
return new DomainManga(
|
||||
@@ -110,4 +143,17 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
$entity->setRating($manga->getRating());
|
||||
}
|
||||
}
|
||||
|
||||
private function toChapterDomain(EntityChapter $entity): Chapter
|
||||
{
|
||||
return new Chapter(
|
||||
id: new ChapterId((string)$entity->getId()),
|
||||
mangaId: $entity->getManga()->getId(),
|
||||
number: $entity->getNumber(),
|
||||
title: $entity->getTitle(),
|
||||
volume: $entity->getVolume(),
|
||||
isVisible: $entity->isVisible(),
|
||||
createdAt: new \DateTimeImmutable()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user