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:
ext.jeremy.guillot@maxicoffee.domains
2025-06-29 16:08:53 +02:00
parent dac2f91998
commit a00858ae6e
4 changed files with 37 additions and 18 deletions

View File

@@ -75,9 +75,7 @@ export class ApiContentSourceRepository {
*/ */
async import(contentSources) { async import(contentSources) {
try { try {
const response = await this.apiClient.post('/content-sources/import', { const response = await this.apiClient.post('/content-sources/import', contentSources);
contentSources
});
return response.data; return response.data;
} catch (error) { } catch (error) {
throw new Error(error.response?.data?.message || 'Erreur lors de l\'import des sources'); throw new Error(error.response?.data?.message || 'Erreur lors de l\'import des sources');

View File

@@ -20,28 +20,40 @@ readonly class ImportContentSourceCommandHandler
throw new InvalidArgumentException('Content sources must be an array'); throw new InvalidArgumentException('Content sources must be an array');
} }
$contentSourcesToImport = [];
foreach ($command->contentSources as $data) { foreach ($command->contentSources as $data) {
if (!$this->isValidContentSourceData($data)) { if (!$this->isValidContentSourceData($data)) {
throw new InvalidArgumentException('Invalid content source data provided'); throw new InvalidArgumentException('Invalid content source data provided');
} }
$contentSource = ContentSource::create( // Recherche d'une source existante avec la même baseUrl
baseUrl: $data['baseUrl'], $existingContentSource = $this->contentSourceRepository->findByBaseUrl($data['baseUrl']);
chapterUrlFormat: $data['chapterUrlFormat'],
scrapingType: $data['scrapingType'],
imageSelector: $data['imageSelector'] ?? null,
nextPageSelector: $data['nextPageSelector'] ?? null,
chapterSelector: $data['chapterSelector'] ?? null,
);
$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 private function isValidContentSourceData(mixed $data): bool

View File

@@ -8,6 +8,7 @@ interface ContentSourceRepositoryInterface
{ {
public function findAll(): array; public function findAll(): array;
public function findById(int $id): ?ContentSource; public function findById(int $id): ?ContentSource;
public function findByBaseUrl(string $baseUrl): ?ContentSource;
public function save(ContentSource $contentSource): void; public function save(ContentSource $contentSource): void;
public function delete(ContentSource $contentSource): void; public function delete(ContentSource $contentSource): void;
public function deleteAll(): void; public function deleteAll(): void;

View File

@@ -32,6 +32,14 @@ readonly class DoctrineContentSourceRepository implements ContentSourceRepositor
return $entity ? $this->mapper->toDomain($entity) : null; return $entity ? $this->mapper->toDomain($entity) : null;
} }
public function findByBaseUrl(string $baseUrl): ?ContentSource
{
$entity = $this->entityManager->getRepository(ContentSourceEntity::class)
->findOneBy(['baseUrl' => $baseUrl]);
return $entity ? $this->mapper->toDomain($entity) : null;
}
public function save(ContentSource $contentSource): void public function save(ContentSource $contentSource): void
{ {
if ($contentSource->getId()) { if ($contentSource->getId()) {