mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-11 05:09:49 +02:00
Change AddMangaToContextLogic
This commit is contained in:
@@ -36,8 +36,11 @@ public class SearchController(MangaContext context) : Controller
|
|||||||
|
|
||||||
(Manga manga, MangaConnectorId<Manga> id)[] mangas = connector.SearchManga(Query);
|
(Manga manga, MangaConnectorId<Manga> id)[] mangas = connector.SearchManga(Query);
|
||||||
|
|
||||||
IEnumerable<(Manga manga, MangaConnectorId<Manga> id)> addedManga = mangas.Where(kv => context.AddMangaToContext(kv, HttpContext.RequestAborted).GetAwaiter().GetResult());
|
IEnumerable<(Manga manga, MangaConnectorId<Manga> id)> addedManga =
|
||||||
|
mangas.Select(kv => context.AddMangaToContext(kv, HttpContext.RequestAborted))
|
||||||
|
.Where(t => t.Result is not null)
|
||||||
|
.Select(t => t.Result)
|
||||||
|
.Cast<(Manga manga, MangaConnectorId<Manga> id)>();
|
||||||
IEnumerable<MinimalManga> result = addedManga.Select(manga => manga.manga).Select(m =>
|
IEnumerable<MinimalManga> result = addedManga.Select(manga => manga.manga).Select(m =>
|
||||||
{
|
{
|
||||||
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
||||||
@@ -67,7 +70,7 @@ public class SearchController(MangaContext context) : Controller
|
|||||||
if(connector.GetMangaFromUrl(url) is not ({ } m, not null) manga)
|
if(connector.GetMangaFromUrl(url) is not ({ } m, not null) manga)
|
||||||
return TypedResults.NotFound("Could not retrieve Manga");
|
return TypedResults.NotFound("Could not retrieve Manga");
|
||||||
|
|
||||||
if(await context.AddMangaToContext(manga, HttpContext.RequestAborted) == false)
|
if(await context.AddMangaToContext(manga, HttpContext.RequestAborted) is not { } addedManga)
|
||||||
return TypedResults.InternalServerError("Could not add Manga to context");
|
return TypedResults.InternalServerError("Could not add Manga to context");
|
||||||
|
|
||||||
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
||||||
|
@@ -185,16 +185,15 @@ public static class Tranga
|
|||||||
RunningWorkers.Remove(worker, out _);
|
RunningWorkers.Remove(worker, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task<bool> AddMangaToContext(this MangaContext context, (Manga, MangaConnectorId<Manga>) addManga, CancellationToken token) =>
|
internal static async Task<(Manga manga, MangaConnectorId<Manga> id)?> AddMangaToContext(this MangaContext context, (Manga, MangaConnectorId<Manga>) addManga, CancellationToken token) =>
|
||||||
await AddMangaToContext(context, addManga.Item1, addManga.Item2, token);
|
await AddMangaToContext(context, addManga.Item1, addManga.Item2, token);
|
||||||
|
|
||||||
internal static async Task<bool> AddMangaToContext(this MangaContext context, Manga addManga, MangaConnectorId<Manga> addMcId,
|
internal static async Task<(Manga manga, MangaConnectorId<Manga> id)?> AddMangaToContext(this MangaContext context, Manga addManga, MangaConnectorId<Manga> addMcId, CancellationToken token)
|
||||||
CancellationToken token)
|
|
||||||
{
|
{
|
||||||
context.ChangeTracker.Clear();
|
context.ChangeTracker.Clear();
|
||||||
Log.Debug($"Adding Manga to Context: {addManga}");
|
Log.Debug($"Adding Manga to Context: {addManga}");
|
||||||
Manga? manga = await context.FindMangaLike(addManga, token);
|
(Manga,MangaConnectorId<Manga>)? result;
|
||||||
if (manga is not null)
|
if (await context.FindMangaLike(addManga, token) is { } manga)
|
||||||
{
|
{
|
||||||
Log.Debug($"Merging with existing Manga: {manga}");
|
Log.Debug($"Merging with existing Manga: {manga}");
|
||||||
foreach (MangaConnectorId<Manga> mcId in addManga.MangaConnectorIds)
|
foreach (MangaConnectorId<Manga> mcId in addManga.MangaConnectorIds)
|
||||||
@@ -208,6 +207,8 @@ public static class Tranga
|
|||||||
manga.AltTitles = manga.AltTitles.UnionBy(addManga.AltTitles, altTitle => altTitle.Key).ToList();
|
manga.AltTitles = manga.AltTitles.UnionBy(addManga.AltTitles, altTitle => altTitle.Key).ToList();
|
||||||
manga.Chapters = manga.Chapters.UnionBy(addManga.Chapters, chapter => chapter.Key).ToList();
|
manga.Chapters = manga.Chapters.UnionBy(addManga.Chapters, chapter => chapter.Key).ToList();
|
||||||
manga.MangaConnectorIds = manga.MangaConnectorIds.UnionBy(addManga.MangaConnectorIds, id => id.MangaConnectorName).ToList();
|
manga.MangaConnectorIds = manga.MangaConnectorIds.UnionBy(addManga.MangaConnectorIds, id => id.MangaConnectorName).ToList();
|
||||||
|
|
||||||
|
result = (manga, manga.MangaConnectorIds.First(id => id.MangaConnectorName == addMcId.MangaConnectorName));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -227,14 +228,15 @@ public static class Tranga
|
|||||||
addManga.Authors = mergedAuthors.ToList();
|
addManga.Authors = mergedAuthors.ToList();
|
||||||
|
|
||||||
context.Mangas.Add(addManga);
|
context.Mangas.Add(addManga);
|
||||||
|
result = (addManga, addMcId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await context.Sync(token, reason: "AddMangaToContext") is { success: false })
|
if (await context.Sync(token, reason: "AddMangaToContext") is { success: false })
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
DownloadCoverFromMangaconnectorWorker downloadCoverWorker = new (addMcId);
|
DownloadCoverFromMangaconnectorWorker downloadCoverWorker = new (result.Value.Item2);
|
||||||
AddWorker(downloadCoverWorker);
|
AddWorker(downloadCoverWorker);
|
||||||
|
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user