using API.MangaConnectors; using API.Schema.MangaContext; using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using Soenneker.Utils.String.NeedlemanWunsch; using static Microsoft.AspNetCore.Http.StatusCodes; // ReSharper disable InconsistentNaming namespace API.Controllers; [ApiVersion(2)] [ApiController] [Route("v{v:apiVersion}/[controller]")] public class QueryController(MangaContext context) : Controller { /// /// Returns the with /// /// .Key /// /// with not found [HttpGet("Author/{AuthorId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetAuthor(string AuthorId) { if (context.Authors.Find(AuthorId) is not { } author) return NotFound(); return Ok(author); } /// /// Returns with /// /// .Key /// /// with not found [HttpGet("Chapter/{ChapterId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetChapter(string ChapterId) { if (context.Chapters.Find(ChapterId) is not { } chapter) return NotFound(); return Ok(chapter); } /// /// Returns the with .Key /// /// Key of /// /// with not found [HttpGet("Manga/MangaConnectorId/{MangaConnectorIdId}")] [ProducesResponseType>(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetMangaMangaConnectorId(string MangaConnectorIdId) { if(context.MangaConnectorToManga.Find(MangaConnectorIdId) is not { } mcIdManga) return NotFound(); return Ok(mcIdManga); } /// /// Returns all that are being downloaded from at least one /// /// [HttpGet("Manga/Downloading")] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetMangaDownloading() { Manga[] ret = context.MangaIncludeAll() .Where(m => m.MangaConnectorIds.Any(id => id.UseForDownload)) .ToArray(); return Ok(ret); } /// /// Returns with names similar to (identified by /// /// Key of /// /// with not found [HttpGet("Manga/{MangaId}/SimilarName")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetSimilarManga(string MangaId) { if (context.Mangas.Find(MangaId) is not { } manga) return NotFound(); string name = manga.Name; Dictionary mangaNames = context.Mangas.Where(m => m.Key != MangaId).ToDictionary(m => m.Key, m => m.Name); string[] similarIds = mangaNames .Where(kv => NeedlemanWunschStringUtil.CalculateSimilarityPercentage(name, kv.Value) > 0.8) .Select(kv => kv.Key).ToArray(); return Ok(similarIds); } /// /// Returns the with .Key /// /// Key of /// /// with not found [HttpGet("Chapter/MangaConnectorId/{MangaConnectorIdId}")] [ProducesResponseType>(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetChapterMangaConnectorId(string MangaConnectorIdId) { if(context.MangaConnectorToChapter.Find(MangaConnectorIdId) is not { } mcIdChapter) return NotFound(); return Ok(mcIdChapter); } }