mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-15 15:57:54 +02:00
Fix Arrays shall not be added to context
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
using API.Schema;
|
||||
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;
|
||||
|
||||
namespace API.Controllers;
|
||||
@ -36,21 +34,24 @@ public class ConnectorController(PgsqlContext context) : Controller
|
||||
[ProducesResponseType<Manga[]>(Status500InternalServerError)]
|
||||
public IActionResult SearchMangaGlobal(string name)
|
||||
{
|
||||
List<(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])> allManga = new();
|
||||
List<(Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<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)
|
||||
List<Manga> retMangas = new();
|
||||
foreach ((Manga? manga, List<Author>? authors, List<MangaTag>? tags, List<Link>? links, List<MangaAltTitle>? altTitles) in allManga)
|
||||
{
|
||||
try
|
||||
{
|
||||
AddMangaToContext(manga, authors, tags, links, altTitles);
|
||||
Manga? add = AddMangaToContext(manga, authors, tags, links, altTitles);
|
||||
if(add is not null)
|
||||
retMangas.Add(add);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
return StatusCode(500, new ProblemResponse("An error occurred while processing your request."));
|
||||
}
|
||||
}
|
||||
return Ok(allManga.Select(m => context.Manga.Find(m.Item1.MangaId)).ToArray());
|
||||
return Ok(retMangas.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -68,31 +69,41 @@ public class ConnectorController(PgsqlContext context) : Controller
|
||||
MangaConnector? connector = context.MangaConnectors.Find(id);
|
||||
if (connector is null)
|
||||
return NotFound(new ProblemResponse("Connector not found."));
|
||||
(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] mangas = connector.GetManga(name);
|
||||
foreach ((Manga? manga, Author[]? authors, MangaTag[]? tags, Link[]? links, MangaAltTitle[]? altTitles) in mangas)
|
||||
(Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)[] mangas = connector.GetManga(name);
|
||||
List<Manga> retMangas = new();
|
||||
foreach ((Manga? manga, List<Author>? authors, List<MangaTag>? tags, List<Link>? links, List<MangaAltTitle>? altTitles) in mangas)
|
||||
{
|
||||
try
|
||||
{
|
||||
AddMangaToContext(manga, authors, tags, links, altTitles);
|
||||
Manga? add = AddMangaToContext(manga, authors, tags, links, altTitles);
|
||||
if(add is not null)
|
||||
retMangas.Add(add);
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
catch (DbUpdateException e)
|
||||
{
|
||||
return StatusCode(500, new ProblemResponse("An error occurred while processing your request."));
|
||||
return StatusCode(500, new ProblemResponse("An error occurred while processing your request.", e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(mangas.Select(m => context.Manga.Find(m.Item1.MangaId)).ToArray());
|
||||
return Ok(retMangas.ToArray());
|
||||
}
|
||||
|
||||
private void AddMangaToContext(Manga? manga, Author[]? authors, MangaTag[]? tags, Link[]? links,
|
||||
MangaAltTitle[]? altTitles)
|
||||
private Manga? AddMangaToContext(Manga? manga, List<Author>? authors, List<MangaTag>? tags, List<Link>? links,
|
||||
List<MangaAltTitle>? altTitles)
|
||||
{
|
||||
if (manga is null)
|
||||
return;
|
||||
|
||||
return null;
|
||||
Manga? existing = context.Manga.FirstOrDefault(m => m.ConnectorId == manga.ConnectorId);
|
||||
|
||||
if (tags is not null)
|
||||
{
|
||||
IEnumerable<MangaTag> newTags = tags.Where(mt => context.Tags.All(t => !t.Tag.Equals(mt.Tag)));
|
||||
IEnumerable<MangaTag> mergedTags = tags.Select(mt =>
|
||||
{
|
||||
MangaTag? inDb = context.Tags.FirstOrDefault(t => t.Equals(mt));
|
||||
return inDb ?? mt;
|
||||
});
|
||||
manga.Tags = mergedTags.ToList();
|
||||
IEnumerable<MangaTag> newTags = manga.Tags.Where(mt => !context.Tags.Any(t => t.Tag.Equals(mt.Tag)));
|
||||
context.Tags.AddRange(newTags);
|
||||
}
|
||||
|
||||
@ -100,22 +111,50 @@ public class ConnectorController(PgsqlContext context) : Controller
|
||||
{
|
||||
IEnumerable<Author> mergedAuthors = authors.Select(ma =>
|
||||
{
|
||||
Author? inDb = context.Authors.FirstOrDefault(a => a.Equals(ma));
|
||||
Author? inDb = context.Authors.FirstOrDefault(a => a.AuthorName == ma.AuthorName);
|
||||
return inDb ?? ma;
|
||||
});
|
||||
manga.Authors = mergedAuthors.ToArray();
|
||||
IEnumerable<Author> newAuthors = authors.Where(ma => context.Authors.All(a => !a.Equals(ma)));
|
||||
manga.Authors = mergedAuthors.ToList();
|
||||
IEnumerable<Author> newAuthors = manga.Authors.Where(ma => !context.Authors.Any(a =>
|
||||
a.AuthorName == ma.AuthorName));
|
||||
context.Authors.AddRange(newAuthors);
|
||||
}
|
||||
|
||||
if (links is not null)
|
||||
context.Link.AddRange(links);
|
||||
{
|
||||
IEnumerable<Link> mergedLinks = links.Select(ml =>
|
||||
{
|
||||
Link? inDb = context.Link.FirstOrDefault(l =>
|
||||
l.LinkProvider == ml.LinkProvider && l.LinkUrl == ml.LinkUrl);
|
||||
return inDb ?? ml;
|
||||
});
|
||||
manga.Links = mergedLinks.ToList();
|
||||
IEnumerable<Link> newLinks = manga.Links.Where(ml => !context.Link.Any(l =>
|
||||
l.LinkProvider == ml.LinkProvider && l.LinkUrl == ml.LinkUrl));
|
||||
context.Link.AddRange(newLinks);
|
||||
}
|
||||
|
||||
if (altTitles is not null)
|
||||
{
|
||||
IEnumerable<MangaAltTitle> mergedAltTitles = altTitles.Select(mat =>
|
||||
{
|
||||
MangaAltTitle? inDb = context.AltTitles.FirstOrDefault(at =>
|
||||
at.Language == mat.Language && at.Title == mat.Title);
|
||||
return inDb ?? mat;
|
||||
});
|
||||
manga.AltTitles = mergedAltTitles.ToList();
|
||||
IEnumerable<MangaAltTitle> newAltTitles = manga.AltTitles.Where(mat =>
|
||||
!context.AltTitles.Any(at => at.Language == mat.Language && at.Title == mat.Title));
|
||||
context.AltTitles.AddRange(newAltTitles);
|
||||
}
|
||||
|
||||
if(altTitles is not null)
|
||||
context.AltTitles.AddRange(altTitles);
|
||||
|
||||
context.Manga.Add(manga);
|
||||
existing?.UpdateWithInfo(manga);
|
||||
if(existing is not null)
|
||||
context.Manga.Update(existing);
|
||||
else
|
||||
context.Manga.Add(manga);
|
||||
|
||||
context.SaveChanges();
|
||||
return existing ?? manga;
|
||||
}
|
||||
}
|
@ -19,25 +19,12 @@ public class JobController(PgsqlContext context) : Controller
|
||||
/// <returns>Array of Jobs</returns>
|
||||
[HttpPost("WithIDs")]
|
||||
[ProducesResponseType<Job[]>(Status200OK)]
|
||||
public IActionResult GetJobs([FromBody] string[] ids)
|
||||
public IActionResult GetJobs([FromBody]string[] ids)
|
||||
{
|
||||
Job[] ret = context.Jobs.Where(job => ids.Contains(job.JobId)).ToArray();
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all due Jobs (NextExecution > CurrentTime)
|
||||
/// </summary>
|
||||
/// <returns>Array of Jobs</returns>
|
||||
[HttpGet("Due")]
|
||||
[ProducesResponseType<Job[]>(Status200OK)]
|
||||
public IActionResult GetDueJobs()
|
||||
{
|
||||
DateTime now = DateTime.Now.ToUniversalTime();
|
||||
Job[] dueJobs = context.Jobs.Where(job => job.NextExecution < now && job.state < JobState.Running).ToArray();
|
||||
return Ok(dueJobs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all Jobs in requested State
|
||||
/// </summary>
|
||||
@ -56,7 +43,7 @@ public class JobController(PgsqlContext context) : Controller
|
||||
/// </summary>
|
||||
/// <param name="type">Requested Job-Type</param>
|
||||
/// <returns>Array of Jobs</returns>
|
||||
[HttpPost("Type/{type}")]
|
||||
[HttpGet("Type/{type}")]
|
||||
[ProducesResponseType<Job[]>(Status200OK)]
|
||||
public IActionResult GetJobsOfType(JobType type)
|
||||
{
|
||||
@ -83,46 +70,71 @@ public class JobController(PgsqlContext context) : Controller
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the State of a Job
|
||||
/// Create a new CreateNewDownloadChapterJob
|
||||
/// </summary>
|
||||
/// <param name="id">Job-ID</param>
|
||||
/// <param name="state">New State</param>
|
||||
/// <param name="request">ID of the Manga, and how often we check again</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPatch("{id}/Status")]
|
||||
[ProducesResponseType(Status200OK)]
|
||||
[ProducesResponseType<string>(Status404NotFound)]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public IActionResult UpdateJobStatus(string id, [FromBody]JobState state)
|
||||
[HttpPut("NewDownloadChapterJob/{mangaId}")]
|
||||
[ProducesResponseType(Status201Created)]
|
||||
[ProducesResponseType<string>(Status500InternalServerError)]
|
||||
public IActionResult CreateNewDownloadChapterJob(string mangaId, [FromBody]ulong recurrenceTime)
|
||||
{
|
||||
Job job = new DownloadNewChaptersJob(recurrenceTime, mangaId);
|
||||
return AddJob(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new DownloadSingleChapterJob
|
||||
/// </summary>
|
||||
/// <param name="chapterId">ID of the Chapter</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut("DownloadSingleChapterJob/{chapterId}")]
|
||||
[ProducesResponseType(Status201Created)]
|
||||
[ProducesResponseType<string>(Status500InternalServerError)]
|
||||
public IActionResult CreateNewDownloadChapterJob(string chapterId)
|
||||
{
|
||||
Job job = new DownloadSingleChapterJob(chapterId);
|
||||
return AddJob(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new UpdateMetadataJob
|
||||
/// </summary>
|
||||
/// <param name="mangaId">ID of the Manga</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut("UpdateMetadataJob/{mangaId}")]
|
||||
[ProducesResponseType(Status201Created)]
|
||||
[ProducesResponseType<string>(Status500InternalServerError)]
|
||||
public IActionResult CreateUpdateMetadataJob(string mangaId)
|
||||
{
|
||||
Job job = new UpdateMetadataJob(0, mangaId);
|
||||
return AddJob(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new UpdateMetadataJob for all Manga
|
||||
/// </summary>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut("UpdateMetadataJob")]
|
||||
[ProducesResponseType(Status201Created)]
|
||||
[ProducesResponseType<string>(Status500InternalServerError)]
|
||||
public IActionResult CreateUpdateAllMetadataJob()
|
||||
{
|
||||
List<string> ids = context.Manga.Select(m => m.MangaId).ToList();
|
||||
List<UpdateMetadataJob> jobs = ids.Select(id => new UpdateMetadataJob(0, id)).ToList();
|
||||
try
|
||||
{
|
||||
Job? ret = context.Jobs.Find(id);
|
||||
switch (ret is not null)
|
||||
{
|
||||
case true:
|
||||
ret.state = state;
|
||||
context.Update(ret);
|
||||
context.SaveChanges();
|
||||
return Ok();
|
||||
case false: return NotFound();
|
||||
}
|
||||
context.Jobs.AddRange(jobs);
|
||||
context.SaveChanges();
|
||||
return Created();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new Job
|
||||
/// </summary>
|
||||
/// <param name="job">Job</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut]
|
||||
[ProducesResponseType(Status201Created)]
|
||||
[ProducesResponseType<string>(Status500InternalServerError)]
|
||||
public IActionResult CreateJob([FromBody]Job job)
|
||||
|
||||
private IActionResult AddJob(Job job)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -71,59 +71,6 @@ public class MangaController(PgsqlContext context) : Controller
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create new Manga
|
||||
/// </summary>
|
||||
/// <param name="manga">Manga</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut]
|
||||
[ProducesResponseType(Status200OK)]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public IActionResult CreateManga([FromBody] Manga manga)
|
||||
{
|
||||
try
|
||||
{
|
||||
context.Manga.Add(manga);
|
||||
context.SaveChanges();
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Manga MetaData
|
||||
/// </summary>
|
||||
/// <param name="id">Manga-ID</param>
|
||||
/// <param name="manga">New Manga-Info</param>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPatch("{id}")]
|
||||
[ProducesResponseType(Status200OK)]
|
||||
[ProducesResponseType(Status404NotFound)]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public IActionResult UpdateMangaMetadata(string id, [FromBody]Manga manga)
|
||||
{
|
||||
try
|
||||
{
|
||||
Manga? ret = context.Manga.Find(id);
|
||||
switch (ret is not null)
|
||||
{
|
||||
case true:
|
||||
ret.UpdateWithInfo(manga);
|
||||
context.Update(ret);
|
||||
context.SaveChanges();
|
||||
return Ok();
|
||||
case false: return NotFound();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns URL of Cover of Manga
|
||||
/// </summary>
|
||||
@ -152,37 +99,6 @@ public class MangaController(PgsqlContext context) : Controller
|
||||
Chapter[] ret = context.Chapters.Where(c => c.ParentManga.MangaId == m.MangaId).ToArray();
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds/Creates new Chapter for Manga
|
||||
/// </summary>
|
||||
/// <param name="id">Manga-ID</param>
|
||||
/// <param name="chapters">Array of Chapters</param>
|
||||
/// <remarks>Manga-ID and all Chapters have to be the same</remarks>
|
||||
/// <returns>Nothing</returns>
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(Status200OK)]
|
||||
[ProducesResponseType<string>(Status404NotFound)]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public IActionResult CreateChapters(string id, [FromBody]Chapter[] chapters)
|
||||
{
|
||||
try
|
||||
{
|
||||
Manga? ret = context.Manga.Find(id);
|
||||
if(ret is null)
|
||||
return NotFound("Manga could not be found");
|
||||
if(chapters.All(c => c.ParentManga.MangaId == ret.MangaId))
|
||||
return BadRequest("Chapters belong to different Manga.");
|
||||
|
||||
context.Chapters.AddRange(chapters);
|
||||
context.SaveChanges();
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return StatusCode(500, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the latest Chapter of requested Manga
|
||||
|
Reference in New Issue
Block a user