feat: endpoint pour la création d'un manga directement via l'api
This commit is contained in:
parent
4017cabff2
commit
3dc0a0b406
@@ -22,4 +22,8 @@ services:
|
||||
$projectDir: '%kernel.project_dir%'
|
||||
public: true
|
||||
|
||||
App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface:
|
||||
class: App\Tests\Domain\Manga\Adapter\InMemoryImageProcessor
|
||||
public: true
|
||||
|
||||
|
||||
|
||||
19
src/Domain/Manga/Application/Command/CreateManga.php
Normal file
19
src/Domain/Manga/Application/Command/CreateManga.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\Command;
|
||||
|
||||
readonly class CreateManga
|
||||
{
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public string $slug,
|
||||
public string $description,
|
||||
public string $author,
|
||||
public int $publicationYear,
|
||||
public array $genres,
|
||||
public string $status,
|
||||
public ?string $externalId,
|
||||
public ?string $imageUrl,
|
||||
public ?float $rating
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Command\CreateManga;
|
||||
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
|
||||
use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
|
||||
use App\Domain\Manga\Domain\Model\Manga;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
|
||||
use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls;
|
||||
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 Ramsey\Uuid\Uuid;
|
||||
|
||||
readonly class CreateMangaHandler
|
||||
{
|
||||
public function __construct(
|
||||
private MangaRepositoryInterface $mangaRepository,
|
||||
private ImageProcessorInterface $imageProcessor
|
||||
) {}
|
||||
|
||||
public function handle(CreateManga $command): void
|
||||
{
|
||||
$manga = new Manga(
|
||||
new MangaId(Uuid::uuid4()->toString()),
|
||||
new MangaTitle($command->title),
|
||||
new MangaSlug($command->slug),
|
||||
$command->description,
|
||||
$command->author,
|
||||
$command->publicationYear,
|
||||
$command->genres,
|
||||
$command->status,
|
||||
$command->externalId ? new ExternalId($command->externalId) : null,
|
||||
$command->imageUrl,
|
||||
$command->rating
|
||||
);
|
||||
|
||||
if (!is_null($command->imageUrl)) {
|
||||
try {
|
||||
$fullImagePath = $this->imageProcessor->downloadImage($command->imageUrl);
|
||||
$thumbnailPath = $this->imageProcessor->createThumbnail($fullImagePath);
|
||||
$manga->updateImageUrls(new ImageUrls($fullImagePath, $thumbnailPath));
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException('Erreur lors du traitement de l\'image : ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$this->mangaRepository->save($manga);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\State\Processor\CreateMangaDirectlyProcessor;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'CreateMangaDirectly',
|
||||
operations: [
|
||||
new Post(
|
||||
uriTemplate: '/mangas/create',
|
||||
processor: CreateMangaDirectlyProcessor::class,
|
||||
openapiContext: [
|
||||
'summary' => 'Create a new manga directly',
|
||||
'description' => 'Creates a new manga with provided data'
|
||||
]
|
||||
)
|
||||
]
|
||||
)]
|
||||
class CreateMangaDirectlyResource
|
||||
{
|
||||
#[Assert\NotBlank(message: 'Le titre ne peut pas être vide')]
|
||||
#[Assert\Length(min: 1, max: 255, minMessage: 'Le titre doit contenir au moins {{ limit }} caractère', maxMessage: 'Le titre ne peut pas dépasser {{ limit }} caractères')]
|
||||
public string $title;
|
||||
|
||||
#[Assert\NotBlank(message: 'Le slug ne peut pas être vide')]
|
||||
#[Assert\Regex(pattern: '/^[a-z0-9-]+$/', message: 'Le slug ne peut contenir que des lettres minuscules, des chiffres et des tirets')]
|
||||
public string $slug;
|
||||
|
||||
#[Assert\NotBlank(message: 'La description ne peut pas être vide')]
|
||||
public string $description;
|
||||
|
||||
#[Assert\NotBlank(message: 'L\'auteur ne peut pas être vide')]
|
||||
public string $author;
|
||||
|
||||
#[Assert\NotBlank(message: 'L\'année de publication ne peut pas être vide')]
|
||||
#[Assert\Type(type: 'integer', message: 'L\'année de publication doit être un nombre entier')]
|
||||
#[Assert\Range(min: 1900, max: 2100, notInRangeMessage: 'L\'année de publication doit être comprise entre {{ min }} et {{ max }}')]
|
||||
public int $publicationYear;
|
||||
|
||||
#[Assert\NotBlank(message: 'Les genres ne peuvent pas être vides')]
|
||||
#[Assert\Type(type: 'array', message: 'Les genres doivent être une liste')]
|
||||
#[Assert\Count(min: 1, minMessage: 'Vous devez spécifier au moins un genre')]
|
||||
public array $genres;
|
||||
|
||||
#[Assert\NotBlank(message: 'Le statut ne peut pas être vide')]
|
||||
#[Assert\Choice(choices: ['ongoing', 'completed', 'hiatus'], message: 'Le statut doit être l\'un des suivants : ongoing, completed, hiatus')]
|
||||
public string $status;
|
||||
|
||||
public ?string $externalId = null;
|
||||
|
||||
#[Assert\Url(message: 'L\'URL de l\'image n\'est pas valide')]
|
||||
public ?string $imageUrl = null;
|
||||
|
||||
#[Assert\Type(type: 'float', message: 'La note doit être un nombre décimal')]
|
||||
#[Assert\Range(min: 0, max: 5, notInRangeMessage: 'La note doit être comprise entre {{ min }} et {{ max }}')]
|
||||
public ?float $rating = null;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\ApiPlatform\State\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Domain\Manga\Application\Command\CreateManga;
|
||||
use App\Domain\Manga\Application\CommandHandler\CreateMangaHandler;
|
||||
use App\Domain\Manga\Infrastructure\ApiPlatform\Resource\CreateMangaDirectlyResource;
|
||||
|
||||
readonly class CreateMangaDirectlyProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private CreateMangaHandler $handler
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
|
||||
{
|
||||
if (!$data instanceof CreateMangaDirectlyResource) {
|
||||
throw new \InvalidArgumentException('Invalid resource type');
|
||||
}
|
||||
|
||||
$command = new CreateManga(
|
||||
title: $data->title,
|
||||
slug: $data->slug,
|
||||
description: $data->description,
|
||||
author: $data->author,
|
||||
publicationYear: $data->publicationYear,
|
||||
genres: $data->genres,
|
||||
status: $data->status,
|
||||
externalId: $data->externalId,
|
||||
imageUrl: $data->imageUrl,
|
||||
rating: $data->rating
|
||||
);
|
||||
|
||||
$this->handler->handle($command);
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,10 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
public function save(DomainManga $manga): void
|
||||
{
|
||||
$entity = new EntityManga();
|
||||
|
||||
$imageUrls = $manga->getImageUrls();
|
||||
$fullImageUrl = $imageUrls?->getFull();
|
||||
$thumbnailUrl = $imageUrls?->getThumbnail();
|
||||
|
||||
$entity->setTitle($manga->getTitle()->getValue())
|
||||
->setSlug($manga->getSlug()->getValue())
|
||||
@@ -65,8 +69,8 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
->setGenres($manga->getGenres())
|
||||
->setStatus($manga->getStatus())
|
||||
->setExternalId($manga->getExternalId()->getValue())
|
||||
->setImageUrl($manga->getImageUrls()->getFull())
|
||||
->setThumbnailUrl($manga->getImageUrls()->getThumbnail())
|
||||
->setImageUrl($fullImageUrl ?? null)
|
||||
->setThumbnailUrl($thumbnailUrl ?? null)
|
||||
->setMonitored(false);
|
||||
|
||||
if ($manga->getRating() !== null) {
|
||||
|
||||
@@ -6,18 +6,34 @@ use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
|
||||
|
||||
class InMemoryImageProcessor implements ImageProcessorInterface
|
||||
{
|
||||
private const string FULL_IMAGE_PATH = '/images/full';
|
||||
private const string THUMBNAIL_PATH = '/images/thumbnails';
|
||||
private const string FAKE_FULL_IMAGE_PATH = '/images/full/test-image.jpg';
|
||||
private const string FAKE_THUMBNAIL_PATH = '/images/thumbnails/test-image.jpg';
|
||||
|
||||
/** @var array<string, string> */
|
||||
private array $downloadedImages = [];
|
||||
|
||||
/** @var array<string, string> */
|
||||
private array $thumbnails = [];
|
||||
|
||||
public function downloadImage(string $imageUrl): string
|
||||
{
|
||||
$filename = sprintf('%s/%s.jpg', self::FULL_IMAGE_PATH, uniqid());
|
||||
return $filename;
|
||||
$this->downloadedImages[$imageUrl] = self::FAKE_FULL_IMAGE_PATH;
|
||||
return self::FAKE_FULL_IMAGE_PATH;
|
||||
}
|
||||
|
||||
public function createThumbnail(string $originalImagePath): string
|
||||
{
|
||||
$filename = basename($originalImagePath);
|
||||
return sprintf('%s/%s', self::THUMBNAIL_PATH, $filename);
|
||||
$this->thumbnails[$originalImagePath] = self::FAKE_THUMBNAIL_PATH;
|
||||
return self::FAKE_THUMBNAIL_PATH;
|
||||
}
|
||||
|
||||
public function getDownloadedImages(): array
|
||||
{
|
||||
return $this->downloadedImages;
|
||||
}
|
||||
|
||||
public function getThumbnails(): array
|
||||
{
|
||||
return $this->thumbnails;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Domain\Manga\Application\CommandHandler;
|
||||
|
||||
use App\Domain\Manga\Application\Command\CreateManga;
|
||||
use App\Domain\Manga\Application\CommandHandler\CreateMangaHandler;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryMangaRepository;
|
||||
use App\Tests\Domain\Manga\Adapter\InMemoryImageProcessor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CreateMangaHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryMangaRepository $repository;
|
||||
private InMemoryImageProcessor $imageProcessor;
|
||||
private CreateMangaHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryMangaRepository();
|
||||
$this->imageProcessor = new InMemoryImageProcessor();
|
||||
$this->handler = new CreateMangaHandler(
|
||||
$this->repository,
|
||||
$this->imageProcessor
|
||||
);
|
||||
}
|
||||
|
||||
public function testHandleSuccess(): void
|
||||
{
|
||||
// Arrange
|
||||
$command = new CreateManga(
|
||||
title: 'One Piece',
|
||||
slug: 'one-piece',
|
||||
description: 'Description test',
|
||||
author: 'Eiichiro Oda',
|
||||
publicationYear: 1997,
|
||||
genres: ['action', 'adventure'],
|
||||
status: 'ongoing',
|
||||
externalId: 'external-123',
|
||||
imageUrl: 'http://example.com/image.jpg',
|
||||
rating: 4.5
|
||||
);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$savedManga = $this->repository->findAll()[0];
|
||||
$this->assertEquals('One Piece', $savedManga->getTitle()->getValue());
|
||||
$this->assertEquals('one-piece', $savedManga->getSlug()->getValue());
|
||||
$this->assertEquals('external-123', $savedManga->getExternalId()?->getValue());
|
||||
$this->assertNotNull($savedManga->getImageUrls());
|
||||
$this->assertStringStartsWith('/images/full/', $savedManga->getImageUrls()->getFull());
|
||||
$this->assertStringStartsWith('/images/thumbnails/', $savedManga->getImageUrls()->getThumbnail());
|
||||
}
|
||||
|
||||
public function testHandleWithoutImage(): void
|
||||
{
|
||||
// Arrange
|
||||
$command = new CreateManga(
|
||||
title: 'One Piece',
|
||||
slug: 'one-piece',
|
||||
description: 'Description test',
|
||||
author: 'Eiichiro Oda',
|
||||
publicationYear: 1997,
|
||||
genres: ['action', 'adventure'],
|
||||
status: 'ongoing',
|
||||
externalId: 'external-123',
|
||||
imageUrl: null,
|
||||
rating: 4.5
|
||||
);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($command);
|
||||
|
||||
// Assert
|
||||
$savedManga = $this->repository->findAll()[0];
|
||||
$this->assertEquals('One Piece', $savedManga->getTitle()->getValue());
|
||||
$this->assertNull($savedManga->getImageUrls());
|
||||
}
|
||||
}
|
||||
128
tests/Feature/Manga/CreateMangaDirectlyTest.php
Normal file
128
tests/Feature/Manga/CreateMangaDirectlyTest.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Feature\Manga;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
|
||||
use App\Tests\Feature\AbstractApiTestCase;
|
||||
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||
|
||||
class CreateMangaDirectlyTest extends AbstractApiTestCase
|
||||
{
|
||||
use ResetDatabase;
|
||||
|
||||
public function testCreateMangaDirectly(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => 'One Piece',
|
||||
'slug' => 'one-piece',
|
||||
'description' => 'Test description',
|
||||
'author' => 'Eiichiro Oda',
|
||||
'publicationYear' => 1997,
|
||||
'genres' => ['action', 'adventure'],
|
||||
'status' => 'ongoing',
|
||||
'externalId' => 'external-123',
|
||||
'imageUrl' => 'http://example.com/image.jpg',
|
||||
'rating' => 4.5
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
// Verify the manga was created in database
|
||||
$entityManager = static::getContainer()->get('doctrine')->getManager();
|
||||
$manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
|
||||
|
||||
$this->assertNotNull($manga);
|
||||
$this->assertEquals('One Piece', $manga->getTitle());
|
||||
$this->assertEquals('Test description', $manga->getDescription());
|
||||
$this->assertEquals('Eiichiro Oda', $manga->getAuthor());
|
||||
$this->assertEquals(1997, $manga->getPublicationYear());
|
||||
$this->assertEquals(['action', 'adventure'], $manga->getGenres());
|
||||
$this->assertEquals('ongoing', $manga->getStatus());
|
||||
$this->assertEquals('external-123', $manga->getExternalId());
|
||||
$this->assertEquals('/images/full/test-image.jpg', $manga->getImageUrl());
|
||||
$this->assertEquals('/images/thumbnails/test-image.jpg', $manga->getThumbnailUrl());
|
||||
$this->assertEquals(4.5, $manga->getRating());
|
||||
}
|
||||
|
||||
public function testCreateMangaWithoutImage(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => 'One Piece',
|
||||
'slug' => 'one-piece',
|
||||
'description' => 'Test description',
|
||||
'author' => 'Eiichiro Oda',
|
||||
'publicationYear' => 1997,
|
||||
'genres' => ['action', 'adventure'],
|
||||
'status' => 'ongoing',
|
||||
'externalId' => 'external-123',
|
||||
'imageUrl' => null,
|
||||
'rating' => 4.5
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseIsSuccessful();
|
||||
|
||||
$entityManager = static::getContainer()->get('doctrine')->getManager();
|
||||
$manga = $entityManager->getRepository(\App\Entity\Manga::class)->findOneBy(['slug' => 'one-piece']);
|
||||
|
||||
$this->assertNotNull($manga);
|
||||
$this->assertNull($manga->getImageUrl());
|
||||
$this->assertNull($manga->getThumbnailUrl());
|
||||
}
|
||||
|
||||
public function testCreateMangaWithInvalidData(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => '', // Invalid: empty title
|
||||
'publicationYear' => 'invalid', // Invalid: not a number
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(400);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'The type of the "publicationYear" attribute must be "int", "string" given.'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testCreateMangaWithInvalidYear(): void
|
||||
{
|
||||
// When
|
||||
$client = static::createClient();
|
||||
$response = $client->request('POST', '/api/mangas/create', [
|
||||
'json' => [
|
||||
'title' => 'One Piece',
|
||||
'slug' => 'one-piece',
|
||||
'description' => 'Test description',
|
||||
'author' => 'Eiichiro Oda',
|
||||
'publicationYear' => 2200, // Année invalide > 2100
|
||||
'genres' => ['action', 'adventure'],
|
||||
'status' => 'ongoing',
|
||||
'externalId' => 'external-123',
|
||||
'imageUrl' => null,
|
||||
'rating' => 4.5
|
||||
]
|
||||
]);
|
||||
|
||||
// Then
|
||||
$this->assertResponseStatusCodeSame(422);
|
||||
$this->assertJsonContains([
|
||||
'hydra:title' => 'An error occurred',
|
||||
'hydra:description' => 'publicationYear: L\'année de publication doit être comprise entre 1900 et 2100',
|
||||
'title' => 'An error occurred'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user