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;
[ApiVersion(2)]
[ApiController]
[Route("v{v:apiVersion}/[controller]")]
public class SearchController(MangaContext context) : Controller
{
///
/// Initiate a search for a on with searchTerm
///
/// .Name
/// searchTerm
/// exert of
/// with Name not found
/// with Name is disabled
[HttpGet("{MangaConnectorName}/{Query}")]
[ProducesResponseType>(Status200OK, "application/json")]
[ProducesResponseType(Status404NotFound, "text/plain")]
[ProducesResponseType(Status406NotAcceptable)]
public Results>, NotFound, StatusCodeHttpResult> SearchManga (string MangaConnectorName, string Query)
{
if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals(MangaConnectorName, StringComparison.InvariantCultureIgnoreCase)) is not { } connector)
return TypedResults.NotFound(nameof(MangaConnectorName));
if (connector.Enabled is false)
return TypedResults.StatusCode(Status412PreconditionFailed);
(Manga manga, MangaConnectorId id)[] mangas = connector.SearchManga(Query);
IEnumerable result = mangas.Select(manga => manga.manga).Select(m =>
{
IEnumerable 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());
}
///
/// Returns from the associated with
///
///
/// exert of .
/// not found
/// Error during Database Operation
[HttpPost("Url")]
[ProducesResponseType(Status200OK, "application/json")]
[ProducesResponseType(Status404NotFound, "text/plain")]
[ProducesResponseType(Status500InternalServerError, "text/plain")]
public Results, NotFound, InternalServerError> GetMangaFromUrl ([FromBody]string url)
{
if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals("Global", StringComparison.InvariantCultureIgnoreCase)) is not { } connector)
return TypedResults.InternalServerError("Could not find Global Connector.");
if(connector.GetMangaFromUrl(url) is not { } manga)
return TypedResults.NotFound("Could not retrieve Manga");
if(Tranga.AddMangaToContext(manga, context, out Manga? m, HttpContext.RequestAborted) == false)
return TypedResults.InternalServerError("Could not add Manga to context");
IEnumerable 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);
}
}