feat: Ajout d'un endpoint getBySlug
This commit is contained in:
parent
504c62c155
commit
30d26f530d
10
src/Domain/Manga/Application/Query/GetMangaBySlug.php
Normal file
10
src/Domain/Manga/Application/Query/GetMangaBySlug.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domain\Manga\Application\Query;
|
||||||
|
|
||||||
|
readonly class GetMangaBySlug
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $slug
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domain\Manga\Application\QueryHandler;
|
||||||
|
|
||||||
|
use App\Domain\Manga\Application\Query\GetMangaBySlug;
|
||||||
|
use App\Domain\Manga\Application\Response\MangaResponse;
|
||||||
|
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||||
|
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
||||||
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||||
|
|
||||||
|
readonly class GetMangaBySlugHandler
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private MangaRepositoryInterface $mangaRepository
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(GetMangaBySlug $query): MangaResponse
|
||||||
|
{
|
||||||
|
$manga = $this->mangaRepository->findBySlug(new MangaSlug($query->slug));
|
||||||
|
|
||||||
|
if (!$manga) {
|
||||||
|
throw new MangaNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MangaResponse(
|
||||||
|
id: $manga->getId()->getValue(),
|
||||||
|
title: $manga->getTitle()->getValue(),
|
||||||
|
slug: $manga->getSlug()->getValue(),
|
||||||
|
description: $manga->getDescription(),
|
||||||
|
author: $manga->getAuthor(),
|
||||||
|
publicationYear: $manga->getPublicationYear(),
|
||||||
|
genres: $manga->getGenres(),
|
||||||
|
status: $manga->getStatus(),
|
||||||
|
externalId: $manga->getExternalId()?->getValue(),
|
||||||
|
imageUrl: $manga->getImageUrl(),
|
||||||
|
rating: $manga->getRating()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ namespace App\Domain\Manga\Domain\Contract\Repository;
|
|||||||
use App\Domain\Manga\Domain\Model\Manga;
|
use App\Domain\Manga\Domain\Model\Manga;
|
||||||
use App\Domain\Manga\Domain\Model\Chapter;
|
use App\Domain\Manga\Domain\Model\Chapter;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
||||||
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||||
|
|
||||||
interface MangaRepositoryInterface
|
interface MangaRepositoryInterface
|
||||||
{
|
{
|
||||||
@@ -17,4 +18,5 @@ interface MangaRepositoryInterface
|
|||||||
public function countChapters(string $mangaId): int;
|
public function countChapters(string $mangaId): int;
|
||||||
public function findByExternalId(ExternalId $externalId): ?Manga;
|
public function findByExternalId(ExternalId $externalId): ?Manga;
|
||||||
public function saveChapter(Chapter $chapter): void;
|
public function saveChapter(Chapter $chapter): void;
|
||||||
|
public function findBySlug(MangaSlug $slug): ?Manga;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
|
use ApiPlatform\Metadata\Get;
|
||||||
|
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaDetail;
|
||||||
|
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider\GetMangaBySlugStateProvider;
|
||||||
|
|
||||||
|
#[ApiResource(
|
||||||
|
shortName: 'Manga',
|
||||||
|
operations: [
|
||||||
|
new Get(
|
||||||
|
uriTemplate: '/mangas/by-slug/{slug}',
|
||||||
|
provider: GetMangaBySlugStateProvider::class,
|
||||||
|
output: MangaDetail::class,
|
||||||
|
name: 'get_manga_by_slug'
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)]
|
||||||
|
class GetMangaBySlugResource
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $slug
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Provider;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\Domain\Manga\Application\Query\GetMangaBySlug;
|
||||||
|
use App\Domain\Manga\Application\QueryHandler\GetMangaBySlugHandler;
|
||||||
|
use App\Domain\Manga\Infrastructure\ApiPlatform\Dto\MangaDetail;
|
||||||
|
|
||||||
|
readonly class GetMangaBySlugStateProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private GetMangaBySlugHandler $handler
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): MangaDetail
|
||||||
|
{
|
||||||
|
$query = new GetMangaBySlug($uriVariables['slug']);
|
||||||
|
$response = $this->handler->handle($query);
|
||||||
|
|
||||||
|
return new MangaDetail(
|
||||||
|
id: $response->id,
|
||||||
|
title: $response->title,
|
||||||
|
slug: $response->slug,
|
||||||
|
description: $response->description,
|
||||||
|
author: $response->author,
|
||||||
|
publicationYear: $response->publicationYear,
|
||||||
|
genres: $response->genres,
|
||||||
|
status: $response->status,
|
||||||
|
externalId: $response->externalId,
|
||||||
|
imageUrl: $response->imageUrl,
|
||||||
|
rating: $response->rating
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,6 +53,14 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
|||||||
return $entity ? $this->toDomain($entity) : null;
|
return $entity ? $this->toDomain($entity) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findBySlug(MangaSlug $slug): ?DomainManga
|
||||||
|
{
|
||||||
|
$entity = $this->entityManager->getRepository(EntityManga::class)
|
||||||
|
->findOneBy(['slug' => $slug->getValue()]);
|
||||||
|
|
||||||
|
return $entity ? $this->toDomain($entity) : null;
|
||||||
|
}
|
||||||
|
|
||||||
public function save(DomainManga $manga): void
|
public function save(DomainManga $manga): void
|
||||||
{
|
{
|
||||||
$entity = new EntityManga();
|
$entity = new EntityManga();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Domain\Manga\Domain\Model\Chapter;
|
|||||||
use App\Domain\Manga\Domain\Model\Manga;
|
use App\Domain\Manga\Domain\Model\Manga;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
use App\Domain\Manga\Domain\Model\ValueObject\ChapterId;
|
||||||
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
||||||
|
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
|
||||||
|
|
||||||
class InMemoryMangaRepository implements MangaRepositoryInterface
|
class InMemoryMangaRepository implements MangaRepositoryInterface
|
||||||
{
|
{
|
||||||
@@ -47,6 +48,16 @@ class InMemoryMangaRepository implements MangaRepositoryInterface
|
|||||||
return $this->mangas[$id] ?? null;
|
return $this->mangas[$id] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function findBySlug(MangaSlug $slug): ?Manga
|
||||||
|
{
|
||||||
|
foreach ($this->mangas as $manga) {
|
||||||
|
if ($manga->getSlug()->getValue() === $slug->getValue()) {
|
||||||
|
return $manga;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function save(Manga $manga): void
|
public function save(Manga $manga): void
|
||||||
{
|
{
|
||||||
$this->mangas[$manga->getId()->getValue()] = $manga;
|
$this->mangas[$manga->getId()->getValue()] = $manga;
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests\Domain\Manga\Application\QueryHandler;
|
||||||
|
|
||||||
|
use App\Domain\Manga\Application\Query\GetMangaBySlug;
|
||||||
|
use App\Domain\Manga\Application\QueryHandler\GetMangaBySlugHandler;
|
||||||
|
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
|
||||||
|
use App\Domain\Manga\Domain\Model\Manga;
|
||||||
|
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\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class GetMangaBySlugHandlerTest extends TestCase
|
||||||
|
{
|
||||||
|
private InMemoryMangaRepository $repository;
|
||||||
|
private GetMangaBySlugHandler $handler;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->repository = new InMemoryMangaRepository();
|
||||||
|
$this->handler = new GetMangaBySlugHandler($this->repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleReturnsCorrectMangaResponse(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$manga = new Manga(
|
||||||
|
new MangaId('1'),
|
||||||
|
new MangaTitle('One Piece'),
|
||||||
|
new MangaSlug('one-piece'),
|
||||||
|
'Description',
|
||||||
|
'Eiichiro Oda',
|
||||||
|
1997,
|
||||||
|
['Action', 'Adventure'],
|
||||||
|
'ongoing',
|
||||||
|
null,
|
||||||
|
'https://example.com/image.jpg',
|
||||||
|
4.5
|
||||||
|
);
|
||||||
|
$this->repository->save($manga);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$response = $this->handler->handle(new GetMangaBySlug('one-piece'));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertEquals('1', $response->id);
|
||||||
|
$this->assertEquals('One Piece', $response->title);
|
||||||
|
$this->assertEquals('one-piece', $response->slug);
|
||||||
|
$this->assertEquals('Description', $response->description);
|
||||||
|
$this->assertEquals('Eiichiro Oda', $response->author);
|
||||||
|
$this->assertEquals(1997, $response->publicationYear);
|
||||||
|
$this->assertEquals(['Action', 'Adventure'], $response->genres);
|
||||||
|
$this->assertEquals('ongoing', $response->status);
|
||||||
|
$this->assertNull($response->externalId);
|
||||||
|
$this->assertEquals('https://example.com/image.jpg', $response->imageUrl);
|
||||||
|
$this->assertEquals(4.5, $response->rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleThrowsExceptionWhenMangaNotFound(): void
|
||||||
|
{
|
||||||
|
// Assert
|
||||||
|
$this->expectException(MangaNotFoundException::class);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$this->handler->handle(new GetMangaBySlug('non-existent-manga'));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
$this->repository->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
59
tests/Feature/Manga/GetMangaBySlugTest.php
Normal file
59
tests/Feature/Manga/GetMangaBySlugTest.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests\Feature\Manga;
|
||||||
|
|
||||||
|
use App\Entity\Manga;
|
||||||
|
use App\Factory\MangaFactory;
|
||||||
|
use App\Tests\Feature\AbstractApiTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Zenstruck\Foundry\Test\Factories;
|
||||||
|
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||||
|
|
||||||
|
class GetMangaBySlugTest extends AbstractApiTestCase
|
||||||
|
{
|
||||||
|
use ResetDatabase, Factories;
|
||||||
|
|
||||||
|
public function testGetMangaBySlugReturnsCorrectManga(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
$manga = MangaFactory::createOne([
|
||||||
|
'title' => 'One Piece',
|
||||||
|
'slug' => 'one-piece',
|
||||||
|
'description' => 'Description',
|
||||||
|
'author' => 'Eiichiro Oda',
|
||||||
|
'publicationYear' => 1997,
|
||||||
|
'genres' => ['Action', 'Adventure'],
|
||||||
|
'status' => 'ongoing',
|
||||||
|
'imageUrl' => 'https://example.com/image.jpg',
|
||||||
|
'rating' => 4.5,
|
||||||
|
'monitored' => true
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
static::createClient()->request('GET', '/api/mangas/by-slug/one-piece');
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertResponseIsSuccessful();
|
||||||
|
$this->assertJsonContains([
|
||||||
|
'id' => (string) $manga->getId(),
|
||||||
|
'title' => 'One Piece',
|
||||||
|
'slug' => 'one-piece',
|
||||||
|
'description' => 'Description',
|
||||||
|
'author' => 'Eiichiro Oda',
|
||||||
|
'publicationYear' => 1997,
|
||||||
|
'genres' => ['Action', 'Adventure'],
|
||||||
|
'status' => 'ongoing',
|
||||||
|
'imageUrl' => 'https://example.com/image.jpg',
|
||||||
|
'rating' => 4.5
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetMangaBySlugReturns404WhenMangaNotFound(): void
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
static::createClient()->request('GET', '/api/mangas/by-slug/non-existent-manga');
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user