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