mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 11:58:19 +02:00
Use DTOs to return API requests instead of Database Schema types.
Make use of IHttpStatusCodeResults
This commit is contained in:
@@ -2,8 +2,11 @@ using API.Controllers.DTOs;
|
||||
using API.MangaConnectors;
|
||||
using API.Schema.MangaContext;
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using static Microsoft.AspNetCore.Http.StatusCodes;
|
||||
using Manga = API.Schema.MangaContext.Manga;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace API.Controllers;
|
||||
@@ -14,58 +17,62 @@ namespace API.Controllers;
|
||||
public class SearchController(MangaContext context) : Controller
|
||||
{
|
||||
/// <summary>
|
||||
/// Initiate a search for a <see cref="Manga"/> on <see cref="MangaConnector"/> with searchTerm
|
||||
/// Initiate a search for a <see cref="Schema.MangaContext.Manga"/> on <see cref="MangaConnector"/> with searchTerm
|
||||
/// </summary>
|
||||
/// <param name="MangaConnectorName"><see cref="MangaConnector"/>.Name</param>
|
||||
/// <param name="Query">searchTerm</param>
|
||||
/// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Manga"/>. Use <see cref="GetManga"/> for more information</response>
|
||||
/// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Schema.MangaContext.Manga"/></response>
|
||||
/// <response code="404"><see cref="MangaConnector"/> with Name not found</response>
|
||||
/// <response code="412"><see cref="MangaConnector"/> with Name is disabled</response>
|
||||
[HttpGet("{MangaConnectorName}/{Query}")]
|
||||
[ProducesResponseType<MinimalManga[]>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status404NotFound)]
|
||||
[ProducesResponseType<List<MinimalManga>>(Status200OK, "application/json")]
|
||||
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
||||
[ProducesResponseType(Status406NotAcceptable)]
|
||||
public IActionResult SearchManga (string MangaConnectorName, string Query)
|
||||
public Results<Ok<List<MinimalManga>>, NotFound<string>, StatusCodeHttpResult> SearchManga (string MangaConnectorName, string Query)
|
||||
{
|
||||
if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals(MangaConnectorName, StringComparison.InvariantCultureIgnoreCase)) is not { } connector)
|
||||
return NotFound();
|
||||
return TypedResults.NotFound(nameof(MangaConnectorName));
|
||||
if (connector.Enabled is false)
|
||||
return StatusCode(Status412PreconditionFailed);
|
||||
return TypedResults.StatusCode(Status412PreconditionFailed);
|
||||
|
||||
(Manga, MangaConnectorId<Manga>)[] mangas = connector.SearchManga(Query);
|
||||
List<Manga> retMangas = new();
|
||||
foreach ((Manga manga, MangaConnectorId<Manga> mcId) manga in mangas)
|
||||
{
|
||||
if(Tranga.AddMangaToContext(manga, context, out Manga? add, HttpContext.RequestAborted))
|
||||
retMangas.Add(add);
|
||||
}
|
||||
(Manga manga, MangaConnectorId<Manga> id)[] mangas = connector.SearchManga(Query);
|
||||
|
||||
return Ok(retMangas.Select(m => new MinimalManga(m.Key, m.Name, m.Description, m.ReleaseStatus, m.MangaConnectorIds)));
|
||||
IEnumerable<MinimalManga> result = mangas.Select(manga => manga.manga).Select(m =>
|
||||
{
|
||||
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
||||
new MangaConnectorId(id.Key, id.MangaConnectorName, id.ObjId, id.WebsiteUrl, id.UseForDownload));
|
||||
return new MinimalManga(m.Key, m.Name, m.Description, m.ReleaseStatus, ids);
|
||||
});
|
||||
|
||||
return TypedResults.Ok(result.ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see cref="Manga"/> from the <see cref="MangaConnector"/> associated with <paramref name="url"/>
|
||||
/// Returns <see cref="Schema.MangaContext.Manga"/> from the <see cref="MangaConnector"/> associated with <paramref name="url"/>
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Manga"/>. Use <see cref="GetManga"/> for more information</response>
|
||||
/// <response code="300">Multiple <see cref="MangaConnector"/> found for URL</response>
|
||||
/// <response code="200"><see cref="MinimalManga"/> exert of <see cref="Schema.MangaContext.Manga"/>.</response>
|
||||
/// <response code="404"><see cref="Manga"/> not found</response>
|
||||
/// <response code="500">Error during Database Operation</response>
|
||||
[HttpPost("Url")]
|
||||
[ProducesResponseType<MinimalManga>(Status200OK, "application/json")]
|
||||
[ProducesResponseType(Status404NotFound)]
|
||||
[ProducesResponseType(Status500InternalServerError)]
|
||||
public IActionResult GetMangaFromUrl ([FromBody]string url)
|
||||
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||
public Results<Ok<MinimalManga>, NotFound<string>, InternalServerError<string>> GetMangaFromUrl ([FromBody]string url)
|
||||
{
|
||||
if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals("Global", StringComparison.InvariantCultureIgnoreCase)) is not { } connector)
|
||||
return StatusCode(Status500InternalServerError, "Could not find Global Connector.");
|
||||
return TypedResults.InternalServerError("Could not find Global Connector.");
|
||||
|
||||
if(connector.GetMangaFromUrl(url) is not { } manga)
|
||||
return NotFound();
|
||||
return TypedResults.NotFound("Could not retrieve Manga");
|
||||
|
||||
if(Tranga.AddMangaToContext(manga, context, out Manga? add, HttpContext.RequestAborted) == false)
|
||||
return StatusCode(Status500InternalServerError);
|
||||
if(Tranga.AddMangaToContext(manga, context, out Manga? m, HttpContext.RequestAborted) == false)
|
||||
return TypedResults.InternalServerError("Could not add Manga to context");
|
||||
|
||||
return Ok(new MinimalManga(add.Key, add.Name, add.Description, add.ReleaseStatus, add.MangaConnectorIds));
|
||||
IEnumerable<MangaConnectorId> ids = m.MangaConnectorIds.Select(id =>
|
||||
new MangaConnectorId(id.Key, id.MangaConnectorName, id.ObjId, id.WebsiteUrl, id.UseForDownload));
|
||||
MinimalManga result = new (m.Key, m.Name, m.Description, m.ReleaseStatus, ids);
|
||||
|
||||
return TypedResults.Ok(result);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user