using API.Controllers.Requests; using API.Schema.MangaContext; using Asp.Versioning; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using static Microsoft.AspNetCore.Http.StatusCodes; using FileLibrary = API.Controllers.DTOs.FileLibrary; // 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")] [ProducesResponseType(Status500InternalServerError)] public async Task>, InternalServerError>> GetFileLibraries () { if (await context.FileLibraries.ToListAsync(HttpContext.RequestAborted) is not { } result) return TypedResults.InternalServerError(); List fileLibraries = result.Select(f => new FileLibrary(f.Key, f.BasePath, f.LibraryName)).ToList(); return TypedResults.Ok(fileLibraries); } /// /// Returns with /// /// .Key /// /// with not found. [HttpGet("{FileLibraryId}")] [ProducesResponseType(Status200OK, "application/json")] [ProducesResponseType(Status404NotFound, "text/plain")] public async Task, NotFound>> GetFileLibrary (string FileLibraryId) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return TypedResults.NotFound(nameof(FileLibraryId)); return TypedResults.Ok(new FileLibrary(library.Key, library.BasePath, library.LibraryName)); } /// /// Changes the .BasePath with /// /// .Key /// New .BasePath /// /// with not found. /// Error during Database Operation [HttpPatch("{FileLibraryId}/ChangeBasePath")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> ChangeLibraryBasePath (string FileLibraryId, [FromBody]string newBasePath) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return TypedResults.NotFound(nameof(FileLibraryId)); //TODO Path check library.BasePath = newBasePath; if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } /// /// Changes the .LibraryName with /// /// .Key /// New .LibraryName /// /// with not found. /// Error during Database Operation [HttpPatch("{FileLibraryId}/ChangeName")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> ChangeLibraryName (string FileLibraryId, [FromBody] string newName) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return TypedResults.NotFound(nameof(FileLibraryId)); //TODO Name check library.LibraryName = newName; if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } /// /// Creates new /// /// New to add /// Key of new Library /// Error during Database Operation [HttpPut] [ProducesResponseType(Status201Created, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> CreateNewLibrary ([FromBody]CreateLibraryRecord requestData) { //TODO Parameter check Schema.MangaContext.FileLibrary library = new Schema.MangaContext.FileLibrary(requestData.BasePath, requestData.LibraryName); context.FileLibraries.Add(library); if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Created(string.Empty, library.Key); } /// /// Deletes the .LibraryName with /// /// .Key /// /// with not found. /// Error during Database Operation [HttpDelete("{FileLibraryId}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> DeleteLocalLibrary (string FileLibraryId) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return TypedResults.NotFound(nameof(FileLibraryId)); context.FileLibraries.Remove(library); if(await context.Sync(HttpContext.RequestAborted) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } }