using API.Schema.MangaContext; using API.Schema.MangaContext.MangaConnectors; using Asp.Versioning; 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(IServiceScope scope) : Controller { /// /// Get all (Scanlation-Sites) /// /// Names of (Scanlation-Sites) [HttpGet] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetConnectors() { MangaContext context = scope.ServiceProvider.GetRequiredService(); return Ok(context.MangaConnectors.Select(c => c.Name).ToArray()); } /// /// Returns the (Scanlation-Sites) with the requested Name /// /// .Name /// /// (Scanlation-Sites) with Name not found. [HttpGet("{MangaConnectorName}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetConnector(string MangaConnectorName) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if(context.MangaConnectors.Find(MangaConnectorName) is not { } connector) return NotFound(); return Ok(connector); } /// /// Get all enabled (Scanlation-Sites) /// /// [HttpGet("Enabled")] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetEnabledConnectors() { MangaContext context = scope.ServiceProvider.GetRequiredService(); return Ok(context.MangaConnectors.Where(c => c.Enabled).ToArray()); } /// /// Get all disabled (Scanlation-Sites) /// /// [HttpGet("Disabled")] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetDisabledConnectors() { MangaContext context = scope.ServiceProvider.GetRequiredService(); return Ok(context.MangaConnectors.Where(c => c.Enabled == false).ToArray()); } /// /// 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(Status202Accepted)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult SetEnabled(string MangaConnectorName, bool Enabled) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if(context.MangaConnectors.Find(MangaConnectorName) is not { } connector) return NotFound(); connector.Enabled = Enabled; if(context.Sync().Result is { } errorMessage) return StatusCode(Status500InternalServerError, errorMessage); return Accepted(); } }