using API.Schema.MangaContext; using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; 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 async Task GetAuthor (string AuthorId) { if (await context.Authors.FirstOrDefaultAsync(a => a.Key == AuthorId, HttpContext.RequestAborted) is not { } author) return NotFound(nameof(AuthorId)); return Ok(author); } /// /// Returns with /// /// .Key /// /// with not found [HttpGet("Chapter/{ChapterId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public async Task GetChapter (string ChapterId) { if (await context.Chapters.FirstOrDefaultAsync(c => c.Key == ChapterId, HttpContext.RequestAborted) is not { } chapter) return NotFound(nameof(ChapterId)); return Ok(chapter); } /// /// Returns the with .Key /// /// Key of /// /// with not found [HttpGet("Manga/MangaConnectorId/{MangaConnectorIdId}")] [ProducesResponseType>(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public async Task GetMangaMangaConnectorId (string MangaConnectorIdId) { if (await context.MangaConnectorToManga.FirstOrDefaultAsync(c => c.Key == MangaConnectorIdId, HttpContext.RequestAborted) is not { } mcIdManga) return NotFound(nameof(MangaConnectorIdId)); return Ok(mcIdManga); } /// /// Returns with names similar to (identified by ) /// /// Key of /// /// with not found /// Error during Database Operation [HttpGet("Manga/{MangaId}/SimilarName")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public async Task GetSimilarManga (string MangaId) { if (await context.Mangas.FirstOrDefaultAsync(m => m.Key == MangaId, HttpContext.RequestAborted) is not { } manga) return NotFound(nameof(MangaId)); string name = manga.Name; if(await context.Mangas.Where(m => m.Key != MangaId).ToDictionaryAsync(m => m.Key, m => m.Name, HttpContext.RequestAborted) is not { } mangaNames) return StatusCode(Status500InternalServerError); 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 async Task GetChapterMangaConnectorId (string MangaConnectorIdId) { if (await context.MangaConnectorToManga.FirstOrDefaultAsync(c => c.Key == MangaConnectorIdId, HttpContext.RequestAborted) is not { } mcIdChapter) return NotFound(nameof(MangaConnectorIdId)); return Ok(mcIdChapter); } }