using API.Schema.MangaContext; using Asp.Versioning; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using static Microsoft.AspNetCore.Http.StatusCodes; // ReSharper disable InconsistentNaming namespace API.Controllers; [ApiVersion(2)] [ApiController] [Route("v{v:apiVersion}/[controller]")] public class FileLibraryController(MangaContext context) : Controller { /// /// Returns all /// /// /// Error during Database Operation [HttpGet] [ProducesResponseType(Status200OK, "application/json")] public async Task GetFileLibraries () { if(await context.FileLibraries.ToArrayAsync(HttpContext.RequestAborted) is not { } result) return StatusCode(Status500InternalServerError); return Ok(result); } /// /// Returns with /// /// .Key /// /// with not found. [HttpGet("{FileLibraryId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound)] public async Task GetFileLibrary (string FileLibraryId) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) 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 async Task ChangeLibraryBasePath (string FileLibraryId, [FromBody]string newBasePath) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return NotFound(); //TODO Path check library.BasePath = newBasePath; if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return StatusCode(Status500InternalServerError, result.exceptionMessage); 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 async Task ChangeLibraryName (string FileLibraryId, [FromBody] string newName) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return NotFound(); //TODO Name check library.LibraryName = newName; if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return StatusCode(Status500InternalServerError, result.exceptionMessage); return Ok(); } /// /// Creates new /// /// New to add /// /// Error during Database Operation [HttpPut] [ProducesResponseType(Status201Created)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task CreateNewLibrary ([FromBody]FileLibrary library) { //TODO Parameter check context.FileLibraries.Add(library); if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return StatusCode(Status500InternalServerError, result.exceptionMessage); return Created(); } /// /// Deletes the .LibraryName with /// /// .Key /// /// Error during Database Operation [HttpDelete("{FileLibraryId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound)] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task DeleteLocalLibrary (string FileLibraryId) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return NotFound(); context.FileLibraries.Remove(library); if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return StatusCode(Status500InternalServerError, result.exceptionMessage); return Ok(); } }