Added abstraction layeer IHtmlLoader. Finished reorganizing test project folder structure.

This commit is contained in:
2025-06-09 00:09:59 -04:00
parent b5d22c3c7e
commit c26ed11bfc
30 changed files with 1966 additions and 132 deletions

View File

@@ -7,6 +7,12 @@ namespace MangaReader.Core.Pipeline;
public partial class MangaPipeline(MangaContext context) : IMangaPipeline
{
enum TitleType
{
Primary,
Secondary
}
public async Task RunAsync(MangaPipelineRequest request)
{
string sourceName = request.SourceName;
@@ -17,10 +23,12 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
Manga manga = await GetOrAddMangaAsync(sourceManga);
await AddMangaSourceAsync(sourceUrl, manga, source);
await AddTitleAsync(manga, sourceManga.Title, TitleType.Primary);
await AddDescriptionAsync(manga, sourceManga.Description);
foreach (SourceMangaTitle alternateTitle in sourceManga.AlternateTitles)
{
await AddTitleAsync(manga, alternateTitle);
await AddTitleAsync(manga, alternateTitle, TitleType.Secondary);
}
foreach (string genre in sourceManga.Genres)
@@ -55,15 +63,15 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
private async Task<Manga> GetOrAddMangaAsync(SourceManga sourceManga)
{
Manga? manga = await context.Mangas.FirstOrDefaultAsync(manga => manga.Title == sourceManga.Title);
Manga? manga = await context.Mangas.FirstOrDefaultAsync(manga =>
manga.Titles.Any(mangaTitle => mangaTitle.Name == sourceManga.Title.Name));
if (manga != null)
return manga;
manga = new()
{
Title = sourceManga.Title,
Slug = GenerateSlug(sourceManga.Title),
Slug = GenerateSlug(sourceManga.Title.Name),
};
context.Add(manga);
@@ -104,10 +112,10 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
context.MangaSources.Add(mangaSource);
}
private async Task AddTitleAsync(Manga manga, SourceMangaTitle sourceMangaTitle)
private async Task AddTitleAsync(Manga manga, SourceMangaTitle sourceMangaTitle, TitleType titleType)
{
MangaTitle? mangaTitle = await context.MangaTitles.FirstOrDefaultAsync(mt =>
mt.Manga == manga && mt.Name == sourceMangaTitle.Title);
mt.Manga == manga && mt.Name == sourceMangaTitle.Name);
if (mangaTitle != null)
return;
@@ -115,12 +123,35 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
mangaTitle = new()
{
Manga = manga,
Name = sourceMangaTitle.Title,
Name = sourceMangaTitle.Name,
Language = sourceMangaTitle.Language,
IsPrimary = titleType == TitleType.Primary
};
context.MangaTitles.Add(mangaTitle);
}
private async Task AddDescriptionAsync(Manga manga, SourceMangaDescription? sourceMangaDescription)
{
if (sourceMangaDescription == null)
return;
MangaDescription? mangaDescription = await context.MangaDescriptions.FirstOrDefaultAsync(md =>
md.Manga == manga && md.Name == sourceMangaDescription.Name);
if (mangaDescription != null)
return;
mangaDescription = new()
{
Manga = manga,
Name = sourceMangaDescription.Name,
Language = sourceMangaDescription.Language
};
context.MangaDescriptions.Add(mangaDescription);
}
private async Task LinkGenreAsync(Manga manga, string genreName)
{
Genre genre = await GetOrAddGenreAsync(genreName);
@@ -156,28 +187,28 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
return genre;
}
private async Task AddChapterAsync(Manga manga, SourceMangaChapter sourceeMangaChapter)
private async Task AddChapterAsync(Manga manga, SourceMangaChapter sourceMangaChapter)
{
MangaChapter mangaChapter = await context.MangaChapters.FirstOrDefaultAsync(x => x.ChapterNumber == sourceeMangaChapter.Number)
?? AddMangaChapter(manga, sourceeMangaChapter);
MangaChapter mangaChapter = await context.MangaChapters.FirstOrDefaultAsync(x => x.ChapterNumber == sourceMangaChapter.Number)
?? AddMangaChapter(manga, sourceMangaChapter);
if (mangaChapter.VolumeNumber is null && sourceeMangaChapter.Volume is not null)
if (mangaChapter.VolumeNumber is null && sourceMangaChapter.Volume is not null)
{
mangaChapter.VolumeNumber = sourceeMangaChapter.Volume;
mangaChapter.VolumeNumber = sourceMangaChapter.Volume;
}
if (mangaChapter.Title is null && sourceeMangaChapter.Title is not null)
if (mangaChapter.Title is null && sourceMangaChapter.Title is not null)
{
mangaChapter.Title = sourceeMangaChapter.Title;
mangaChapter.Title = sourceMangaChapter.Title;
}
}
private MangaChapter AddMangaChapter(Manga manga, SourceMangaChapter sourceeMangaChapter)
private MangaChapter AddMangaChapter(Manga manga, SourceMangaChapter sourceMangaChapter)
{
MangaChapter mangaChapter = new()
{
Manga = manga,
ChapterNumber = sourceeMangaChapter.Number
ChapterNumber = sourceMangaChapter.Number
};
context.MangaChapters.Add(mangaChapter);