diff --git a/API/Controllers/SearchController.cs b/API/Controllers/SearchController.cs index 1fc1c4b..b5d997e 100644 --- a/API/Controllers/SearchController.cs +++ b/API/Controllers/SearchController.cs @@ -36,7 +36,7 @@ public class SearchController(IServiceScope scope) : Controller List retMangas = new(); foreach ((Manga manga, MangaConnectorId mcId) manga in mangas) { - if(AddMangaToContext(manga, context) is { } add) + if(Tranga.AddMangaToContext(manga, context, out Manga? add)) retMangas.Add(add); } @@ -64,40 +64,9 @@ public class SearchController(IServiceScope scope) : Controller if(connector.GetMangaFromUrl(url) is not { } manga) return NotFound(); - if(AddMangaToContext(manga, context) is not { } add) + if(Tranga.AddMangaToContext(manga, context, out Manga? add) == false) return StatusCode(Status500InternalServerError); return Ok(add); } - - private Manga? AddMangaToContext((Manga, MangaConnectorId) manga, MangaContext context) => AddMangaToContext(manga.Item1, manga.Item2, context); - - private static Manga? AddMangaToContext(Manga addManga, MangaConnectorId addMcId, MangaContext context) - { - Manga manga = context.Mangas.Find(addManga.Key) ?? addManga; - MangaConnectorId mcId = context.MangaConnectorToManga.Find(addMcId.Key) ?? addMcId; - mcId.Obj = manga; - - IEnumerable mergedTags = manga.MangaTags.Select(mt => - { - MangaTag? inDb = context.Tags.Find(mt.Tag); - return inDb ?? mt; - }); - manga.MangaTags = mergedTags.ToList(); - - IEnumerable mergedAuthors = manga.Authors.Select(ma => - { - Author? inDb = context.Authors.Find(ma.Key); - return inDb ?? ma; - }); - manga.Authors = mergedAuthors.ToList(); - - - if(context.MangaConnectorToManga.Find(addMcId.Key) is null) - context.MangaConnectorToManga.Add(mcId); - - if (context.Sync().Result is { success: false } ) - return null; - return manga; - } } \ No newline at end of file diff --git a/API/Tranga.cs b/API/Tranga.cs index ab2566c..b66ab77 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -1,4 +1,6 @@ -using API.Schema.MangaContext.MetadataFetchers; +using System.Diagnostics.CodeAnalysis; +using API.Schema.MangaContext; +using API.Schema.MangaContext.MetadataFetchers; using API.Workers; using log4net; using log4net.Config; @@ -100,4 +102,51 @@ public static class Tranga } internal static void MarkWorkerForStart(BaseWorker worker) => StartWorkers.Add(worker); + + internal static bool AddMangaToContext((Manga, MangaConnectorId) addManga, MangaContext context, [NotNullWhen(true)]out Manga? manga) => AddMangaToContext(addManga.Item1, addManga.Item2, context, out manga); + + internal static bool AddMangaToContext(Manga addManga, MangaConnectorId addMcId, MangaContext context, [NotNullWhen(true)]out Manga? manga) + { + manga = context.Mangas.Find(addManga.Key) ?? addManga; + MangaConnectorId mcId = context.MangaConnectorToManga.Find(addMcId.Key) ?? addMcId; + mcId.Obj = manga; + + IEnumerable mergedTags = manga.MangaTags.Select(mt => + { + MangaTag? inDb = context.Tags.Find(mt.Tag); + return inDb ?? mt; + }); + manga.MangaTags = mergedTags.ToList(); + + IEnumerable mergedAuthors = manga.Authors.Select(ma => + { + Author? inDb = context.Authors.Find(ma.Key); + return inDb ?? ma; + }); + manga.Authors = mergedAuthors.ToList(); + + if(context.MangaConnectorToManga.Find(addMcId.Key) is null) + context.MangaConnectorToManga.Add(mcId); + + if (context.Sync().Result is { success: false }) + return false; + return true; + } + + internal static bool AddChapterToContext((Chapter, MangaConnectorId) addChapter, MangaContext context, + [NotNullWhen(true)] out Chapter? chapter) => AddChapterToContext(addChapter.Item1, addChapter.Item2, context, out chapter); + + internal static bool AddChapterToContext(Chapter addChapter, MangaConnectorId addChId, MangaContext context, [NotNullWhen(true)] out Chapter? chapter) + { + chapter = context.Chapters.Find(addChapter.Key) ?? addChapter; + MangaConnectorId chId = context.MangaConnectorToChapter.Find(addChId.Key) ?? addChId; + chId.Obj = chapter; + + if(context.MangaConnectorToChapter.Find(chId.Key) is null) + context.MangaConnectorToChapter.Add(chId); + + if (context.Sync().Result is { success: false }) + return false; + return true; + } } \ No newline at end of file diff --git a/API/Workers/MangaDownloadWorkers/RetrieveMangaChaptersFromMangaconnectorWorker.cs b/API/Workers/MangaDownloadWorkers/RetrieveMangaChaptersFromMangaconnectorWorker.cs index e2448f9..ec500a6 100644 --- a/API/Workers/MangaDownloadWorkers/RetrieveMangaChaptersFromMangaconnectorWorker.cs +++ b/API/Workers/MangaDownloadWorkers/RetrieveMangaChaptersFromMangaconnectorWorker.cs @@ -14,15 +14,15 @@ public class RetrieveMangaChaptersFromMangaconnectorWorker(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) + int addedChapters = 0; + foreach ((Chapter chapter, MangaConnectorId mcId) newChapter in allChapters) { - manga.Chapters.Add(newChapter.chapter); - DbContext.MangaConnectorToChapter.Add(newChapter.mcId); + if (Tranga.AddChapterToContext(newChapter, DbContext, out Chapter? addedChapter) == false) + continue; + manga.Chapters.Add(addedChapter); } + Log.Info($"{manga.Chapters.Count} existing + {addedChapters} new chapters."); DbContext.Sync();