using API.Controllers.DTOs; using API.Schema.MangaContext; using Asp.Versioning; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using static Microsoft.AspNetCore.Http.StatusCodes; // ReSharper disable InconsistentNaming namespace API.Controllers; [ApiVersion(2)] [ApiController] [Route("v{v:apiVersion}/[controller]")] public class MangaConnectorController(MangaContext context) : Controller { /// /// Get all (Scanlation-Sites) /// /// Names of (Scanlation-Sites) [HttpGet] [ProducesResponseType>(Status200OK, "application/json")] public Ok> GetConnectors() { return TypedResults.Ok(Tranga.MangaConnectors .Select(c => new MangaConnector(c.Name, c.Enabled, c.IconUrl, c.SupportedLanguages)) .ToList()); } /// /// Returns the (Scanlation-Sites) with the requested Name /// /// .Name /// /// (Scanlation-Sites) with Name not found. [HttpGet("{MangaConnectorName}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound, "text/plain")] public Results, NotFound> GetConnector(string MangaConnectorName) { if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals(MangaConnectorName, StringComparison.InvariantCultureIgnoreCase)) is not { } connector) return TypedResults.NotFound(nameof(MangaConnectorName)); return TypedResults.Ok(new MangaConnector(connector.Name, connector.Enabled, connector.IconUrl, connector.SupportedLanguages)); } /// /// Get all enabled (Scanlation-Sites) /// /// [HttpGet("Enabled")] [ProducesResponseType>(Status200OK, "application/json")] public Ok> GetEnabledConnectors() { return TypedResults.Ok(Tranga.MangaConnectors .Where(c => c.Enabled) .Select(c => new MangaConnector(c.Name, c.Enabled, c.IconUrl, c.SupportedLanguages)) .ToList()); } /// /// Get all disabled (Scanlation-Sites) /// /// [HttpGet("Disabled")] [ProducesResponseType>(Status200OK, "application/json")] public Ok> GetDisabledConnectors() { return TypedResults.Ok(Tranga.MangaConnectors .Where(c => c.Enabled == false) .Select(c => new MangaConnector(c.Name, c.Enabled, c.IconUrl, c.SupportedLanguages)) .ToList()); } /// /// Enabled or disables (Scanlation-Sites) with Name /// /// .Name /// Set true to enable, false to disable /// /// (Scanlation-Sites) with Name not found. /// Error during Database Operation [HttpPatch("{MangaConnectorName}/SetEnabled/{Enabled}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> SetEnabled(string MangaConnectorName, bool Enabled) { if(Tranga.MangaConnectors.FirstOrDefault(c => c.Name.Equals(MangaConnectorName, StringComparison.InvariantCultureIgnoreCase)) is not { } connector) return TypedResults.NotFound(nameof(MangaConnectorName)); connector.Enabled = Enabled; if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } }