diff --git a/API/Controllers/ConnectorController.cs b/API/Controllers/ConnectorController.cs index a2e6511..4a1662d 100644 --- a/API/Controllers/ConnectorController.cs +++ b/API/Controllers/ConnectorController.cs @@ -43,19 +43,14 @@ public class ConnectorController(PgsqlContext context) : Controller { try { - context.Tags.AddRange(tags); - context.Authors.AddRange(authors); - context.Link.AddRange(links); - context.AltTitles.AddRange(altTitles); - context.Manga.AddRange(manga); - context.SaveChanges(); + AddMangaToContext(manga, authors, tags, links, altTitles); } catch (DbUpdateException) { return StatusCode(500, new ProblemResponse("An error occurred while processing your request.")); } } - return Ok(allManga.Select(m => m.Item1).ToArray()); + return Ok(allManga.Select(m => context.Manga.Find(m.Item1.MangaId)).ToArray()); } /// @@ -78,18 +73,49 @@ public class ConnectorController(PgsqlContext context) : Controller { try { - context.Tags.AddRange(tags); - context.Authors.AddRange(authors); - context.Link.AddRange(links); - context.AltTitles.AddRange(altTitles); - context.Manga.AddRange(manga); - context.SaveChanges(); + AddMangaToContext(manga, authors, tags, links, altTitles); } catch (DbUpdateException) { return StatusCode(500, new ProblemResponse("An error occurred while processing your request.")); } } - return Ok(mangas.Select(m => m.Item1).ToArray()); + + return Ok(mangas.Select(m => context.Manga.Find(m.Item1.MangaId)).ToArray()); + } + + private void AddMangaToContext(Manga? manga, Author[]? authors, MangaTag[]? tags, Link[]? links, + MangaAltTitle[]? altTitles) + { + if (manga is null) + return; + + if (tags is not null) + { + IEnumerable newTags = tags.Where(mt => context.Tags.All(t => !t.Tag.Equals(mt.Tag))); + context.Tags.AddRange(newTags); + } + + if (authors is not null) + { + IEnumerable mergedAuthors = authors.Select(ma => + { + Author? inDb = context.Authors.FirstOrDefault(a => a.Equals(ma)); + return inDb ?? ma; + }); + manga.Authors = mergedAuthors.ToArray(); + IEnumerable newAuthors = authors.Where(ma => context.Authors.All(a => !a.Equals(ma))); + context.Authors.AddRange(newAuthors); + } + + if (links is not null) + context.Link.AddRange(links); + + if(altTitles is not null) + context.AltTitles.AddRange(altTitles); + + context.Manga.Add(manga); + + context.SaveChanges(); } } \ No newline at end of file diff --git a/API/Controllers/MangaController.cs b/API/Controllers/MangaController.cs index 530f3f1..a4162fc 100644 --- a/API/Controllers/MangaController.cs +++ b/API/Controllers/MangaController.cs @@ -149,7 +149,7 @@ public class MangaController(PgsqlContext context) : Controller Manga? m = context.Manga.Find(id); if (m is null) return NotFound("Manga could not be found"); - Chapter[] ret = context.Chapters.Where(c => c.ParentMangaId == m.MangaId).ToArray(); + Chapter[] ret = context.Chapters.Where(c => c.ParentManga.MangaId == m.MangaId).ToArray(); return Ok(ret); } @@ -171,7 +171,7 @@ public class MangaController(PgsqlContext context) : Controller Manga? ret = context.Manga.Find(id); if(ret is null) return NotFound("Manga could not be found"); - if(chapters.All(c => c.ParentMangaId == ret.MangaId)) + if(chapters.All(c => c.ParentManga.MangaId == ret.MangaId)) return BadRequest("Chapters belong to different Manga."); context.Chapters.AddRange(chapters); @@ -197,10 +197,9 @@ public class MangaController(PgsqlContext context) : Controller Manga? m = context.Manga.Find(id); if (m is null) return NotFound("Manga could not be found"); - Chapter? c = context.Chapters.Find(m.LatestChapterAvailableId); - if (c is null) + if (m.LatestChapterAvailable is null) return NotFound("Chapter could not be found"); - return Ok(c); + return Ok(m.LatestChapterAvailable); } /// diff --git a/API/Schema/Author.cs b/API/Schema/Author.cs index 9055c63..233e6c6 100644 --- a/API/Schema/Author.cs +++ b/API/Schema/Author.cs @@ -10,6 +10,10 @@ public class Author(string authorName) public string AuthorId { get; init; } = TokenGen.CreateToken(typeof(Author), 64); public string AuthorName { get; init; } = authorName; - [ForeignKey("MangaIds")] - public virtual Manga[] Mangas { get; internal set; } = []; + public override bool Equals(object? obj) + { + if (obj is not Author other) + return false; + return other.AuthorName == AuthorName; + } } \ No newline at end of file diff --git a/API/Schema/Chapter.cs b/API/Schema/Chapter.cs index 5976bd7..b398152 100644 --- a/API/Schema/Chapter.cs +++ b/API/Schema/Chapter.cs @@ -18,15 +18,13 @@ public class Chapter : IComparable public string ArchiveFileName { get; private set; } public bool Downloaded { get; internal set; } = false; - [MaxLength(64)] - public string ParentMangaId { get; init; } - [ForeignKey("ParentMangaId")] - public virtual Manga ParentManga { get; init; } + [ForeignKey("MangaId")] + public Manga ParentManga { get; init; } - public Chapter(string parentMangaId, string url, float chapterNumber, + public Chapter(Manga parentManga, string url, float chapterNumber, float? volumeNumber = null, string? title = null) { - this.ParentMangaId = parentMangaId; + this.ParentManga = parentManga; this.Url = url; this.ChapterNumber = chapterNumber; this.VolumeNumber = volumeNumber; @@ -34,10 +32,8 @@ public class Chapter : IComparable this.ArchiveFileName = BuildArchiveFileName(); } - public Chapter(Manga parentManga, string url, float chapterNumber, - float? volumeNumber = null, string? title = null) : this(parentManga.MangaId, url, chapterNumber, volumeNumber, title) - { - } + public Chapter(string url, float chapterNumber, float? volumeNumber = null, string? title = null) + : this(null, url, chapterNumber, volumeNumber, title){} public MoveFileOrFolderJob? UpdateChapterNumber(float chapterNumber) { diff --git a/API/Schema/Manga.cs b/API/Schema/Manga.cs index ddf4fb3..a33d3cf 100644 --- a/API/Schema/Manga.cs +++ b/API/Schema/Manga.cs @@ -22,13 +22,13 @@ public class Manga( string? originalLanguage, MangaReleaseStatus releaseStatus, float ignoreChapterBefore, - string? latestChapterDownloadedId, - string? latestChapterAvailableId, - string mangaConnectorName, - string[] authorIds, - string[] tagIds, - string[] linkIds, - string[] altTitleIds) + Chapter? latestChapterDownloaded, + Chapter? latestChapterAvailable, + MangaConnector mangaConnector, + Author[] authors, + MangaTag[] tags, + Link[] links, + MangaAltTitle[] altTitles) { [MaxLength(64)] public string MangaId { get; init; } = TokenGen.CreateToken(typeof(Manga), 64); @@ -46,30 +46,33 @@ public class Manga( public string FolderName { get; private set; } = BuildFolderName(name); public float IgnoreChapterBefore { get; internal set; } = ignoreChapterBefore; - public string? LatestChapterDownloadedId { get; internal set; } = latestChapterDownloadedId; - public virtual Chapter? LatestChapterDownloaded { get; } - - public string? LatestChapterAvailableId { get; internal set; } = latestChapterAvailableId; - public virtual Chapter? LatestChapterAvailable { get; } + public Chapter? LatestChapterDownloaded { get; private set; } = latestChapterDownloaded; - public string MangaConnectorName { get; init; } = mangaConnectorName; - public virtual MangaConnector MangaConnector { get; } + public Chapter? LatestChapterAvailable { get; private set; } = latestChapterAvailable; + + public MangaConnector MangaConnector { get; private set; } = mangaConnector; - public string[] AuthorIds { get; internal set; } = authorIds; [ForeignKey("AuthorIds")] - public virtual Author[] Authors { get; } + public ICollection Authors { get; internal set; } = authors; - public string[] TagIds { get; internal set; } = tagIds; [ForeignKey("TagIds")] - public virtual MangaTag[] Tags { get; } + public ICollection Tags { get; private set; } = tags; - public string[] LinkIds { get; internal set; } = linkIds; [ForeignKey("LinkIds")] - public virtual Link[] Links { get; } + public ICollection Links { get; private set; } = links; - public string[] AltTitleIds { get; internal set; } = altTitleIds; [ForeignKey("AltTitleIds")] - public virtual MangaAltTitle[] AltTitles { get; } + public ICollection AltTitles { get; private set; } = altTitles; + + public Manga(string connectorId, string name, string description, string websiteUrl, string coverUrl, + string? coverFileNameInCache, + uint year, string? originalLanguage, MangaReleaseStatus releaseStatus, float ignoreChapterBefore) + : this(connectorId, name, description, websiteUrl, coverUrl, coverFileNameInCache, year, originalLanguage, + releaseStatus, + ignoreChapterBefore, null, null, null, null, null, null, null) + { + + } public MoveFileOrFolderJob UpdateFolderName(string downloadLocation, string newName) { @@ -85,11 +88,11 @@ public class Manga( this.Description = other.Description; this.CoverUrl = other.CoverUrl; this.OriginalLanguage = other.OriginalLanguage; - this.AuthorIds = other.AuthorIds; - this.LinkIds = other.LinkIds; - this.TagIds = other.TagIds; - this.AltTitleIds = other.AltTitleIds; - this.LatestChapterAvailableId = other.LatestChapterAvailableId; + this.Authors = other.Authors; + this.Links = other.Links; + this.Tags = other.Tags; + this.AltTitles = other.AltTitles; + this.LatestChapterAvailable = other.LatestChapterAvailable; this.ReleaseStatus = other.ReleaseStatus; } diff --git a/API/Schema/MangaConnectors/AsuraToon.cs b/API/Schema/MangaConnectors/AsuraToon.cs index c95bfca..4e3c5ee 100644 --- a/API/Schema/MangaConnectors/AsuraToon.cs +++ b/API/Schema/MangaConnectors/AsuraToon.cs @@ -111,9 +111,9 @@ public class AsuraToon : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], []); diff --git a/API/Schema/MangaConnectors/Bato.cs b/API/Schema/MangaConnectors/Bato.cs index 3e923bf..5f4d08f 100644 --- a/API/Schema/MangaConnectors/Bato.cs +++ b/API/Schema/MangaConnectors/Bato.cs @@ -113,11 +113,11 @@ public class Bato : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], - altTitles.Select(a => a.AltTitleId).ToArray()); + altTitles); return (manga, authors, mangaTags, [], altTitles); } diff --git a/API/Schema/MangaConnectors/MangaDex.cs b/API/Schema/MangaConnectors/MangaDex.cs index 99fd651..f7b9ce8 100644 --- a/API/Schema/MangaConnectors/MangaDex.cs +++ b/API/Schema/MangaConnectors/MangaDex.cs @@ -176,11 +176,11 @@ public class MangaDex : MangaConnector Manga pub = new (publicationId, sortName, description, $"https://mangadex.org/title/{publicationId}", coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), - links.Select(l => l.LinkId).ToArray(), - altTitles.Select(a => a.AltTitleId).ToArray()); + this, + authors, + mangaTags, + links, + altTitles); return (pub, authors, mangaTags, links, altTitles); } diff --git a/API/Schema/MangaConnectors/MangaHere.cs b/API/Schema/MangaConnectors/MangaHere.cs index 2263eff..cf37048 100644 --- a/API/Schema/MangaConnectors/MangaHere.cs +++ b/API/Schema/MangaConnectors/MangaHere.cs @@ -101,9 +101,9 @@ public class MangaHere : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, 0, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], []); diff --git a/API/Schema/MangaConnectors/MangaKatana.cs b/API/Schema/MangaConnectors/MangaKatana.cs index 3d7ebd8..a4065c7 100644 --- a/API/Schema/MangaConnectors/MangaKatana.cs +++ b/API/Schema/MangaConnectors/MangaKatana.cs @@ -141,11 +141,11 @@ public class MangaKatana : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], - altTitles.Select(a => a.AltTitleId).ToArray()); + altTitles); return (manga, authors, mangaTags, [], altTitles); } diff --git a/API/Schema/MangaConnectors/MangaLife.cs b/API/Schema/MangaConnectors/MangaLife.cs index cbc870d..57770f8 100644 --- a/API/Schema/MangaConnectors/MangaLife.cs +++ b/API/Schema/MangaConnectors/MangaLife.cs @@ -120,9 +120,9 @@ public class MangaLife : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], []); diff --git a/API/Schema/MangaConnectors/Manganato.cs b/API/Schema/MangaConnectors/Manganato.cs index 1edafa9..4078d55 100644 --- a/API/Schema/MangaConnectors/Manganato.cs +++ b/API/Schema/MangaConnectors/Manganato.cs @@ -136,11 +136,11 @@ public class Manganato : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], - mangaAltTitles.Select(a => a.AltTitleId).ToArray()); + mangaAltTitles); return (manga, authors, mangaTags, [], mangaAltTitles); } diff --git a/API/Schema/MangaConnectors/Mangasee.cs b/API/Schema/MangaConnectors/Mangasee.cs index 000ced4..ac0de54 100644 --- a/API/Schema/MangaConnectors/Mangasee.cs +++ b/API/Schema/MangaConnectors/Mangasee.cs @@ -150,9 +150,9 @@ public class Mangasee : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], []); diff --git a/API/Schema/MangaConnectors/Mangaworld.cs b/API/Schema/MangaConnectors/Mangaworld.cs index 84289d8..762faea 100644 --- a/API/Schema/MangaConnectors/Mangaworld.cs +++ b/API/Schema/MangaConnectors/Mangaworld.cs @@ -118,11 +118,11 @@ public class Mangaworld : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], - altTitles.Select(a => a.AltTitleId).ToArray()); + altTitles); return (manga, authors, mangaTags, [], altTitles); } diff --git a/API/Schema/MangaConnectors/ManhuaPlus.cs b/API/Schema/MangaConnectors/ManhuaPlus.cs index dbab475..dbace83 100644 --- a/API/Schema/MangaConnectors/ManhuaPlus.cs +++ b/API/Schema/MangaConnectors/ManhuaPlus.cs @@ -123,9 +123,9 @@ public class ManhuaPlus : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], []); diff --git a/API/Schema/MangaConnectors/WeebCentral.cs b/API/Schema/MangaConnectors/WeebCentral.cs index c5fa31c..067df88 100644 --- a/API/Schema/MangaConnectors/WeebCentral.cs +++ b/API/Schema/MangaConnectors/WeebCentral.cs @@ -114,11 +114,11 @@ public class Weebcentral : MangaConnector Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year, originalLanguage, releaseStatus, -1, null, null, - this.Name, - authors.Select(a => a.AuthorId).ToArray(), - mangaTags.Select(t => t.Tag).ToArray(), + this, + authors, + mangaTags, [], - altTitles.Select(a => a.AltTitleId).ToArray()); + altTitles); return (manga, authors, mangaTags, [], altTitles); }