Fix redundant keys, MangaSearch

This commit is contained in:
2024-12-16 19:25:22 +01:00
parent 87274aca19
commit 3b58e0498b
16 changed files with 126 additions and 98 deletions

View File

@ -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());
}
/// <summary>
@ -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<MangaTag> newTags = tags.Where(mt => context.Tags.All(t => !t.Tag.Equals(mt.Tag)));
context.Tags.AddRange(newTags);
}
if (authors is not null)
{
IEnumerable<Author> mergedAuthors = authors.Select(ma =>
{
Author? inDb = context.Authors.FirstOrDefault(a => a.Equals(ma));
return inDb ?? ma;
});
manga.Authors = mergedAuthors.ToArray();
IEnumerable<Author> 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();
}
}

View File

@ -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);
}
/// <summary>