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}")] [ProducesResponseType(Status200OK)] [ProducesResponseType(Status404NotFound, "text/plain")] [ProducesResponseType(Status500InternalServerError, "text/plain")] public async Task, InternalServerError>> ChangeLibraryBasePath (string FileLibraryId, [FromBody]PatchFileLibraryRecord requestData) { if(await context.FileLibraries.FirstOrDefaultAsync(l => l.Key == FileLibraryId, HttpContext.RequestAborted) is not { } library) return TypedResults.NotFound(nameof(FileLibraryId)); if (requestData.Path is { } path) library.BasePath = path; if(requestData.Name is { } name) library.LibraryName = name; if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } public record PatchFileLibraryRecord(string? Path, string? Name) { /// /// Directory Path /// public required string? Path { get; init; } = Path; /// /// Library Name /// public required string? Name { get; init; } = Name; } /// /// 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 (requestData.BasePath, requestData.LibraryName); context.FileLibraries.Add(library); if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) 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.Where(l => l.Key == FileLibraryId).ExecuteDeleteAsync(HttpContext.RequestAborted) < 1) return TypedResults.NotFound(nameof(FileLibraryId)); if(await context.Sync(HttpContext.RequestAborted, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } result) return TypedResults.InternalServerError(result.exceptionMessage); return TypedResults.Ok(); } }