Return new DTO "MinimalManga" for endpoints that return a lot of Manga-data

This commit is contained in:
2025-09-01 23:43:59 +02:00
parent 3b8570cf57
commit e96b2585e0
3 changed files with 26 additions and 17 deletions

View File

@@ -0,0 +1,5 @@
using API.Schema.MangaContext;
namespace API.Controllers.DTOs;
public sealed record MinimalManga(string Key, string Name, string Description, MangaReleaseStatus ReleaseStatus);

View File

@@ -1,4 +1,5 @@
using API.MangaConnectors; using API.Controllers.DTOs;
using API.MangaConnectors;
using API.Schema.MangaContext; using API.Schema.MangaContext;
using API.Workers; using API.Workers;
using Asp.Versioning; using Asp.Versioning;
@@ -24,16 +25,16 @@ public class MangaController(MangaContext context) : Controller
/// <summary> /// <summary>
/// Returns all cached <see cref="Manga"/> /// Returns all cached <see cref="Manga"/>
/// </summary> /// </summary>
/// <response code="200"></response> /// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Manga"/>. Use <see cref="GetManga"/> for more information</response>
/// <response code="500">Error during Database Operation</response> /// <response code="500">Error during Database Operation</response>
[HttpGet] [HttpGet]
[ProducesResponseType<Manga[]>(Status200OK, "application/json")] [ProducesResponseType<MinimalManga[]>(Status200OK, "application/json")]
public async Task<IActionResult> GetAllManga () public async Task<IActionResult> GetAllManga ()
{ {
if(await context.Mangas.ToArrayAsync(HttpContext.RequestAborted) is not { } result) if(await context.Mangas.ToArrayAsync(HttpContext.RequestAborted) is not { } result)
return StatusCode(Status500InternalServerError); return StatusCode(Status500InternalServerError);
return Ok(result); return Ok(result.Select(m => new MinimalManga(m.Key, m.Name, m.Description, m.ReleaseStatus)));
} }
/// <summary> /// <summary>
@@ -54,18 +55,19 @@ public class MangaController(MangaContext context) : Controller
/// <summary> /// <summary>
/// Returns all <see cref="Manga"/> that are being downloaded from at least one <see cref="MangaConnector"/> /// Returns all <see cref="Manga"/> that are being downloaded from at least one <see cref="MangaConnector"/>
/// </summary> /// </summary>
/// <response code="200"></response> /// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Manga"/>. Use <see cref="GetManga"/> for more information</response>
/// <response code="500">Error during Database Operation</response> /// <response code="500">Error during Database Operation</response>
[HttpGet("Downloading")] [HttpGet("Downloading")]
[ProducesResponseType<Manga[]>(Status200OK, "application/json")] [ProducesResponseType<MinimalManga[]>(Status200OK, "application/json")]
public async Task<IActionResult> GetMangaDownloading () public async Task<IActionResult> GetMangaDownloading ()
{ {
if(await context.MangaIncludeAll() if(await context.Mangas
.Include(m => m.MangaConnectorIds)
.Where(m => m.MangaConnectorIds.Any(id => id.UseForDownload)) .Where(m => m.MangaConnectorIds.Any(id => id.UseForDownload))
.ToArrayAsync(HttpContext.RequestAborted) is not { } result) .ToArrayAsync(HttpContext.RequestAborted) is not { } result)
return StatusCode(Status500InternalServerError); return StatusCode(Status500InternalServerError);
return Ok(result); return Ok(result.Select(m => new MinimalManga(m.Key, m.Name, m.Description, m.ReleaseStatus)));
} }
/// <summary> /// <summary>
@@ -76,7 +78,7 @@ public class MangaController(MangaContext context) : Controller
/// <response code="500">Error during Database Operation</response> /// <response code="500">Error during Database Operation</response>
[HttpPost("WithIDs")] [HttpPost("WithIDs")]
[ProducesResponseType<Manga[]>(Status200OK, "application/json")] [ProducesResponseType<Manga[]>(Status200OK, "application/json")]
public async Task<IActionResult> GetManga ([FromBody]string[] MangaIds) public async Task<IActionResult> GetMangaWithIds ([FromBody]string[] MangaIds)
{ {
if(await context.MangaIncludeAll() if(await context.MangaIncludeAll()
.Where(m => MangaIds.Contains(m.Key)) .Where(m => MangaIds.Contains(m.Key))

View File

@@ -115,11 +115,13 @@ public class MangaContext(DbContextOptions<MangaContext> options) : TrangaBaseCo
m.AltTitles.Any(t => other.AltTitles.Select(ot => ot.Title).Any(s => s.Equals(t.Title))), token); m.AltTitles.Any(t => other.AltTitles.Select(ot => ot.Title).Any(s => s.Equals(t.Title))), token);
} }
public IIncludableQueryable<Manga, ICollection<MangaConnectorId<Manga>>> MangaIncludeAll() => Mangas.Include(m => m.Library) public IIncludableQueryable<Manga, ICollection<MangaConnectorId<Manga>>> MangaIncludeAll() =>
.Include(m => m.Authors) Mangas
.Include(m => m.MangaTags) .Include(m => m.Library)
.Include(m => m.Links) .Include(m => m.Authors)
.Include(m => m.AltTitles) .Include(m => m.MangaTags)
.Include(m => m.Chapters) .Include(m => m.Links)
.Include(m => m.MangaConnectorIds); .Include(m => m.AltTitles)
.Include(m => m.Chapters)
.Include(m => m.MangaConnectorIds);
} }