using API.Schema; using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using static Microsoft.AspNetCore.Http.StatusCodes; namespace API.Controllers; [ApiVersion(2)] [ApiController] [Produces("application/json")] [Route("v{v:apiVersion}/[controller]")] public class MangaController(PgsqlContext context) : Controller { /// /// Returns all cached Manga /// /// Array of Manga [HttpGet] [ProducesResponseType(Status200OK)] public IActionResult GetAllManga() { Manga[] ret = context.Manga.ToArray(); return Ok(ret); } /// /// Returns all cached Manga with IDs /// /// Array of Manga-IDs /// Array of Manga [HttpPost("WithIDs")] [ProducesResponseType(Status200OK)] public IActionResult GetManga([FromBody]string[] ids) { Manga[] ret = context.Manga.Where(m => ids.Contains(m.MangaId)).ToArray(); return Ok(ret); } /// /// Return Manga with ID /// /// Manga-ID /// Manga [HttpGet("{id}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] public IActionResult GetManga(string id) { Manga? ret = context.Manga.Find(id); return (ret is not null) switch { true => Ok(ret), false => NotFound() }; } /// /// Delete Manga with ID /// /// Manga-ID /// Nothing [HttpDelete("{id}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError)] public IActionResult DeleteManga(string id) { try { Manga? ret = context.Manga.Find(id); switch (ret is not null) { case true: context.Remove(ret); context.SaveChanges(); return Ok(); case false: return NotFound(); } } catch (Exception e) { return StatusCode(500, e.Message); } } /// /// Returns URL of Cover of Manga /// /// Manga-ID /// URL of Cover [HttpGet("{id}/Cover")] [ProducesResponseType(Status500InternalServerError)] public IActionResult GetCover(string id) { return StatusCode(500, "Not implemented"); //TODO } /// /// Returns all Chapters of Manga /// /// Manga-ID /// Array of Chapters [HttpGet("{id}/Chapters")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] public IActionResult GetChapters(string id) { Manga? m = context.Manga.Find(id); if (m is null) return NotFound("Manga could not be found"); Chapter[] ret = context.Chapters.Where(c => c.ParentManga.MangaId == m.MangaId).ToArray(); return Ok(ret); } /// /// Returns the latest Chapter of requested Manga /// /// Manga-ID /// Latest Chapter [HttpGet("{id}/Chapter/Latest")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] public IActionResult GetLatestChapter(string id) { Manga? m = context.Manga.Find(id); if (m is null) return NotFound("Manga could not be found"); List chapters = context.Chapters.Where(c => c.ParentManga.MangaId == m.MangaId).ToList(); Chapter? max = chapters.Max(); if (max is null) return NotFound("Chapter could not be found"); return Ok(max); } /// /// Configure the cut-off for Manga /// /// This is important for the DownloadNewChapters-Job /// Manga-ID /// Nothing [HttpPatch("{id}/IgnoreChaptersBefore")] [ProducesResponseType(Status200OK)] public IActionResult IgnoreChaptersBefore(string id) { Manga? m = context.Manga.Find(id); if (m is null) return NotFound("Manga could not be found"); return Ok(m.IgnoreChapterBefore); } /// /// Move the Directory the .cbz-files are located in /// /// Manga-ID /// New Directory-Path /// Nothing [HttpPost("{id}/MoveFolder")] [ProducesResponseType(Status500InternalServerError)] public IActionResult MoveFolder(string id, [FromBody]string folder) { return StatusCode(500, "Not implemented"); //TODO } }