feat: mise à jour de la gestion des sources de contenu pour permettre l'importation et la mise à jour des sources existantes, avec ajout de la méthode findByBaseUrl dans le repository.
This commit is contained in:
parent
dac2f91998
commit
a00858ae6e
@@ -20,28 +20,40 @@ readonly class ImportContentSourceCommandHandler
|
||||
throw new InvalidArgumentException('Content sources must be an array');
|
||||
}
|
||||
|
||||
$contentSourcesToImport = [];
|
||||
|
||||
foreach ($command->contentSources as $data) {
|
||||
if (!$this->isValidContentSourceData($data)) {
|
||||
throw new InvalidArgumentException('Invalid content source data provided');
|
||||
}
|
||||
|
||||
$contentSource = ContentSource::create(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
// Recherche d'une source existante avec la même baseUrl
|
||||
$existingContentSource = $this->contentSourceRepository->findByBaseUrl($data['baseUrl']);
|
||||
|
||||
$contentSourcesToImport[] = $contentSource;
|
||||
if ($existingContentSource) {
|
||||
// Met à jour la source existante
|
||||
$existingContentSource->update(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
|
||||
$this->contentSourceRepository->save($existingContentSource);
|
||||
} else {
|
||||
// Crée une nouvelle source
|
||||
$newContentSource = ContentSource::create(
|
||||
baseUrl: $data['baseUrl'],
|
||||
chapterUrlFormat: $data['chapterUrlFormat'],
|
||||
scrapingType: $data['scrapingType'],
|
||||
imageSelector: $data['imageSelector'] ?? null,
|
||||
nextPageSelector: $data['nextPageSelector'] ?? null,
|
||||
chapterSelector: $data['chapterSelector'] ?? null,
|
||||
);
|
||||
|
||||
$this->contentSourceRepository->save($newContentSource);
|
||||
}
|
||||
}
|
||||
|
||||
// Supprime tous les content sources existants puis importe les nouveaux
|
||||
$this->contentSourceRepository->deleteAll();
|
||||
$this->contentSourceRepository->saveMultiple($contentSourcesToImport);
|
||||
}
|
||||
|
||||
private function isValidContentSourceData(mixed $data): bool
|
||||
|
||||
Reference in New Issue
Block a user