feat: ajout de la gestion des chapitres de manga, incluant la récupération et la sauvegarde des chapitres en français et en anglais, ainsi que l'optimisation de la logique de sauvegarde pour éviter les doublons

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-04-01 16:01:55 +02:00
parent 34dfa57dc0
commit 0111f1b5f1
12 changed files with 254 additions and 78 deletions

View File

@@ -20,7 +20,7 @@ readonly class FetchMangaChaptersHandler
public function handle(FetchMangaChapters $command): void
{
$manga = $this->mangaRepository->findByExternalId(new ExternalId($command->externalId));
if ($manga === null) {
throw new \RuntimeException('Manga not found');
}
@@ -28,6 +28,8 @@ readonly class FetchMangaChaptersHandler
$offset = 0;
$limit = 500;
$hasMore = true;
$chaptersByNumber = [];
$chapterNumbers = [];
while ($hasMore) {
$feed = $this->mangadexClient->getMangaFeed(
@@ -37,22 +39,47 @@ readonly class FetchMangaChaptersHandler
);
foreach ($feed['data'] as $chapterData) {
$chapter = new Chapter(
new ChapterId((string) Uuid::uuid4()),
$manga->getId()->getValue(),
(float) $chapterData['attributes']['chapter'],
$chapterData['attributes']['title'],
isset($chapterData['attributes']['volume']) ? (int) $chapterData['attributes']['volume'] : null,
true,
false,
new \DateTimeImmutable()
);
$chapterNumber = (float) $chapterData['attributes']['chapter'];
$language = $chapterData['attributes']['translatedLanguage'];
$this->mangaRepository->saveChapter($chapter);
// On ne traite que les chapitres en français ou en anglais
if (!in_array($language, ['fr', 'en'])) {
continue;
}
// Si le chapitre n'existe pas encore ou si c'est une version française
if (!isset($chaptersByNumber[$chapterNumber]) || $language === 'fr') {
$chapter = new Chapter(
new ChapterId((string) Uuid::uuid4()),
$manga->getId()->getValue(),
$chapterNumber,
$chapterData['attributes']['title'],
isset($chapterData['attributes']['volume']) ? (int) $chapterData['attributes']['volume'] : null,
true,
false,
new \DateTimeImmutable()
);
$chaptersByNumber[$chapterNumber] = $chapter;
$chapterNumbers[] = $chapterNumber;
}
}
$offset += $limit;
$hasMore = count($feed['data']) === $limit;
}
// Récupère les chapitres existants
$existingChapters = $this->mangaRepository->findExistingChaptersByNumbers(
$manga->getId()->getValue(),
$chapterNumbers
);
// Sauvegarde uniquement les nouveaux chapitres
foreach ($chaptersByNumber as $chapterNumber => $chapter) {
if (!isset($existingChapters[$chapterNumber])) {
$this->mangaRepository->saveChapter($chapter);
}
}
}
}
}