using API.Schema.MangaContext; using API.Schema.MangaContext.MangaConnectors; namespace API.Workers; public class RetrieveMangaChaptersFromMangaconnectorWorker(MangaConnectorId mcId, string language, IEnumerable? dependsOn = null) : BaseWorkerWithContext(dependsOn) { public MangaConnectorId MangaConnectorId { get; init; } = mcId; protected override BaseWorker[] DoWorkInternal() { MangaConnector mangaConnector = MangaConnectorId.MangaConnector; Manga manga = MangaConnectorId.Obj; // This gets all chapters that are not downloaded (Chapter, MangaConnectorId)[] allChapters = mangaConnector.GetChapters(MangaConnectorId, language).DistinctBy(c => c.Item1.Key).ToArray(); (Chapter, MangaConnectorId)[] newChapters = allChapters.Where(chapter => manga.Chapters.Any(ch => chapter.Item1.Key == ch.Key && ch.Downloaded) == false).ToArray(); Log.Info($"{manga.Chapters.Count} existing + {newChapters.Length} new chapters."); foreach ((Chapter chapter, MangaConnectorId mcId) newChapter in newChapters) { manga.Chapters.Add(newChapter.chapter); DbContext.MangaConnectorToChapter.Add(newChapter.mcId); } DbContext.Sync(); return []; } }