feat: GetMangaList endpoint + tests + test db
This commit is contained in:
parent
073439163b
commit
e3d380eadd
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user