using API.Schema.MangaContext; 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 FileLibraryController(IServiceScope scope) : Controller { /// /// Returns all /// /// [HttpGet] [ProducesResponseType(Status200OK, "application/json")] public IActionResult GetFileLibraries() { MangaContext context = scope.ServiceProvider.GetRequiredService(); return Ok(context.FileLibraries.ToArray()); } /// /// Returns with /// /// .Key /// /// with not found. [HttpGet("{FileLibraryId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public IActionResult GetFileLibrary(string FileLibraryId) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if (context.FileLibraries.Find(FileLibraryId) is not { } library) return NotFound(); return Ok(library); } /// /// Changes the .BasePath with /// /// .Key /// New .BasePath /// /// with not found. /// Error during Database Operation [HttpPatch("{FileLibraryId}/ChangeBasePath")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult ChangeLibraryBasePath(string FileLibraryId, [FromBody]string newBasePath) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if (context.FileLibraries.Find(FileLibraryId) is not { } library) return NotFound(); //TODO Path check library.BasePath = newBasePath; if(context.Sync().Result is { } errorMessage) return StatusCode(Status500InternalServerError, errorMessage); return Ok(); } /// /// Changes the .LibraryName with /// /// .Key /// New .LibraryName /// /// with not found. /// Error during Database Operation [HttpPatch("{FileLibraryId}/ChangeName")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status400BadRequest)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult ChangeLibraryName(string FileLibraryId, [FromBody] string newName) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if (context.FileLibraries.Find(FileLibraryId) is not { } library) return NotFound(); //TODO Name check library.LibraryName = newName; if(context.Sync().Result is { } errorMessage) return StatusCode(Status500InternalServerError, errorMessage); return Ok(); } /// /// Creates new /// /// New to add /// /// Error during Database Operation [HttpPut] [ProducesResponseType(Status201Created)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult CreateNewLibrary([FromBody]FileLibrary library) { MangaContext context = scope.ServiceProvider.GetRequiredService(); //TODO Parameter check context.FileLibraries.Add(library); if(context.Sync().Result is { } errorMessage) return StatusCode(Status500InternalServerError, errorMessage); return Created(); } /// /// Deletes the .LibraryName with /// /// .Key /// /// Error during Database Operation [HttpDelete("{FileLibraryId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public IActionResult DeleteLocalLibrary(string FileLibraryId) { MangaContext context = scope.ServiceProvider.GetRequiredService(); if (context.FileLibraries.Find(FileLibraryId) is not { } library) return NotFound(); context.FileLibraries.Remove(library); if(context.Sync().Result is { } errorMessage) return StatusCode(Status500InternalServerError, errorMessage); return Ok(); } }