feat: endpoint pour la création d'un manga directement via l'api
This commit is contained in:
parent
4017cabff2
commit
3dc0a0b406
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user