feat: GetMangaList endpoint + tests + test db

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-10 19:21:14 +01:00
parent 073439163b
commit e3d380eadd
34 changed files with 932 additions and 23 deletions

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
use ApiPlatform\Metadata\ApiProperty;
readonly class MangaCollection
{
public function __construct(
/** @var MangaListItem[] */
public array $items,
public int $total,
public int $page,
public int $limit,
public bool $hasNextPage,
public bool $hasPreviousPage
) {}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Dto;
use ApiPlatform\Metadata\ApiProperty;
readonly class MangaListItem
{
public function __construct(
#[ApiProperty(identifier: true)]
public string $id,
public string $title,
public string $slug,
public ?string $imageUrl,
public string $author,
public int $publicationYear,
public array $genres,
public string $status,
public ?float $rating
) {}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaCollection;
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\GetMangaListStateProvider;
#[ApiResource(
shortName: 'Manga',
operations: [
new GetCollection(
uriTemplate: '/mangas',
provider: GetMangaListStateProvider::class,
output: MangaCollection::class
)
]
)]
class MangaListResource
{
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Domain\Manga\Application\Query\GetMangaList;
use App\Domain\Manga\Application\QueryHandler\GetMangaListHandler;
use App\Domain\Manga\Domain\Model\Manga;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaCollection;
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaListItem;
readonly class GetMangaListStateProvider implements ProviderInterface
{
public function __construct(
private GetMangaListHandler $handler
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MangaCollection
{
$page = $context['filters']['page'] ?? 1;
$limit = $context['filters']['limit'] ?? 20;
$sortBy = $context['filters']['sortBy'] ?? 'title';
$sortOrder = $context['filters']['sortOrder'] ?? 'asc';
$query = new GetMangaList($page, $limit, $sortBy, $sortOrder);
$response = $this->handler->handle($query);
return new MangaCollection(
items: array_map(
fn (Manga $manga) => $this->createMangaListItem($manga),
$response->mangas
),
total: $response->total,
page: $response->page,
limit: $response->limit,
hasNextPage: $response->hasNextPage(),
hasPreviousPage: $response->hasPreviousPage()
);
}
private function createMangaListItem(Manga $manga): MangaListItem
{
return new MangaListItem(
id: $manga->getId()->getValue(),
title: $manga->getTitle()->getValue(),
slug: $manga->getSlug()->getValue(),
imageUrl: $manga->getImageUrl(),
author: $manga->getAuthor(),
publicationYear: $manga->getPublicationYear(),
genres: $manga->getGenres(),
status: $manga->getStatus(),
rating: $manga->getRating()
);
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace App\Domain\Manga\Infrastructure\Persistence;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Model\Manga as DomainManga;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
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;
readonly class LegacyMangaRepository implements MangaRepositoryInterface
{
public function __construct(
private EntityManagerInterface $entityManager
) {}
public function findAll(int $page = 1, int $limit = 20, string $sortBy = 'title', string $sortOrder = 'asc'): array
{
$offset = ($page - 1) * $limit;
$queryBuilder = $this->entityManager->createQueryBuilder()
->select('m')
->from(EntityManga::class, 'm')
->orderBy("m.$sortBy", $sortOrder)
->setFirstResult($offset)
->setMaxResults($limit);
return array_map(
fn (EntityManga $entity) => $this->toDomain($entity),
$queryBuilder->getQuery()->getResult()
);
}
public function count(): int
{
return $this->entityManager->createQueryBuilder()
->select('COUNT(m.id)')
->from(EntityManga::class, 'm')
->getQuery()
->getSingleScalarResult();
}
public function findById(string $id): ?DomainManga
{
$entity = $this->entityManager->find(EntityManga::class, $id);
return $entity ? $this->toDomain($entity) : null;
}
public function save(DomainManga $manga): void
{
$entity = $this->entityManager->find(EntityManga::class, $manga->getId()->getValue())
?? new EntityManga();
$this->updateEntity($entity, $manga);
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
public function delete(DomainManga $manga): void
{
$entity = $this->entityManager->find(EntityManga::class, $manga->getId()->getValue());
if ($entity) {
$this->entityManager->remove($entity);
$this->entityManager->flush();
}
}
private function toDomain(EntityManga $entity): DomainManga
{
return new DomainManga(
new MangaId((string) $entity->getId()),
new MangaTitle($entity->getTitle()),
new MangaSlug($entity->getSlug()),
$entity->getDescription(),
$entity->getAuthor(),
$entity->getPublicationYear(),
$entity->getGenres(),
$entity->getStatus(),
$entity->getExternalId() ? new ExternalId($entity->getExternalId()) : null,
$entity->getImageUrl(),
$entity->getRating()
);
}
private function updateEntity(EntityManga $entity, DomainManga $manga): void
{
$entity->setTitle($manga->getTitle()->getValue())
->setSlug($manga->getSlug()->getValue())
->setDescription($manga->getDescription())
->setAuthor($manga->getAuthor())
->setPublicationYear($manga->getPublicationYear())
->setGenres($manga->getGenres())
->setStatus($manga->getStatus());
if ($manga->getExternalId()) {
$entity->setExternalId($manga->getExternalId()->getValue());
}
if ($manga->getImageUrl()) {
$entity->setImageUrl($manga->getImageUrl());
}
if ($manga->getRating()) {
$entity->setRating($manga->getRating());
}
}
}