MangaConnectors in API

This commit is contained in:
2024-12-15 23:00:35 +01:00
parent 8c5bcd2665
commit 538e6fa60b
32 changed files with 339 additions and 3375 deletions

View File

@ -3,6 +3,7 @@ using API.Schema.Jobs;
using API.Schema.MangaConnectors;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Soenneker.Utils.String.NeedlemanWunsch;
using static Microsoft.AspNetCore.Http.StatusCodes;
@ -35,12 +36,26 @@ public class ConnectorController(PgsqlContext context) : Controller
[ProducesResponseType<Manga[]>(Status500InternalServerError)]
public IActionResult SearchMangaGlobal(string name)
{
List<Manga> allManga = new List<Manga>();
List<(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])> allManga = new();
foreach (MangaConnector contextMangaConnector in context.MangaConnectors)
{
allManga.AddRange(contextMangaConnector.GetManga(name));
foreach ((Manga? manga, Author[]? authors, MangaTag[]? tags, Link[]? links, MangaAltTitle[]? altTitles) in allManga)
{
try
{
context.Tags.AddRange(tags);
context.Authors.AddRange(authors);
context.Link.AddRange(links);
context.AltTitles.AddRange(altTitles);
context.Manga.AddRange(manga);
context.SaveChanges();
}
catch (DbUpdateException)
{
return StatusCode(500, new ProblemResponse("An error occurred while processing your request."));
}
}
return Ok(allManga.ToArray());
return Ok(allManga.Select(m => m.Item1).ToArray());
}
/// <summary>
@ -52,12 +67,29 @@ public class ConnectorController(PgsqlContext context) : Controller
[HttpPost("{id}/SearchManga")]
[ProducesResponseType<Manga[]>(Status200OK)]
[ProducesResponseType<ProblemResponse>(Status404NotFound)]
[ProducesResponseType<ProblemResponse>(Status500InternalServerError)]
public IActionResult SearchManga(string id, [FromBody]string name)
{
MangaConnector? connector = context.MangaConnectors.Find(id);
if (connector is null)
return NotFound(new ProblemResponse("Connector not found."));
Manga[] manga = connector.GetManga(name);
return Ok(manga);
(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] mangas = connector.GetManga(name);
foreach ((Manga? manga, Author[]? authors, MangaTag[]? tags, Link[]? links, MangaAltTitle[]? altTitles) in mangas)
{
try
{
context.Tags.AddRange(tags);
context.Authors.AddRange(authors);
context.Link.AddRange(links);
context.AltTitles.AddRange(altTitles);
context.Manga.AddRange(manga);
context.SaveChanges();
}
catch (DbUpdateException)
{
return StatusCode(500, new ProblemResponse("An error occurred while processing your request."));
}
}
return Ok(mangas.Select(m => m.Item1).ToArray());
}
}