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:
parent
34dfa57dc0
commit
0111f1b5f1
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,5 @@ interface MangaRepositoryInterface
|
||||
public function findBySlug(MangaSlug $slug): ?Manga;
|
||||
public function search(string $query, int $page = 1, int $limit = 20): array;
|
||||
public function countSearch(string $query): int;
|
||||
}
|
||||
public function findExistingChaptersByNumbers(string $mangaId, array $chapterNumbers): array;
|
||||
}
|
||||
|
||||
@@ -200,6 +200,26 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findExistingChaptersByNumbers(string $mangaId, array $chapterNumbers): array
|
||||
{
|
||||
$queryBuilder = $this->entityManager->createQueryBuilder()
|
||||
->select('c')
|
||||
->from(EntityChapter::class, 'c')
|
||||
->where('c.manga = :mangaId')
|
||||
->andWhere('c.number IN (:chapterNumbers)')
|
||||
->setParameter('mangaId', $mangaId)
|
||||
->setParameter('chapterNumbers', $chapterNumbers);
|
||||
|
||||
$chapters = $queryBuilder->getQuery()->getResult();
|
||||
|
||||
$chaptersByNumber = [];
|
||||
foreach ($chapters as $chapter) {
|
||||
$chaptersByNumber[$chapter->getNumber()] = $this->toChapterDomain($chapter);
|
||||
}
|
||||
|
||||
return $chaptersByNumber;
|
||||
}
|
||||
|
||||
private function toDomain(EntityManga $entity): DomainManga
|
||||
{
|
||||
return new DomainManga(
|
||||
|
||||
Reference in New Issue
Block a user