mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 03:48:19 +02:00
140 lines
6.8 KiB
C#
140 lines
6.8 KiB
C#
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;
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
namespace API.Controllers;
|
|
|
|
[ApiVersion(2)]
|
|
[ApiController]
|
|
[Route("v{v:apiVersion}/[controller]")]
|
|
public class FileLibraryController(MangaContext context) : Controller
|
|
{
|
|
/// <summary>
|
|
/// Returns all <see cref="FileLibrary"/>
|
|
/// </summary>
|
|
/// <response code="200"></response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpGet]
|
|
[ProducesResponseType<FileLibrary[]>(Status200OK, "application/json")]
|
|
[ProducesResponseType(Status500InternalServerError)]
|
|
public async Task<Results<Ok<List<FileLibrary>>, InternalServerError>> GetFileLibraries ()
|
|
{
|
|
if (await context.FileLibraries.ToListAsync(HttpContext.RequestAborted) is not { } result)
|
|
return TypedResults.InternalServerError();
|
|
return TypedResults.Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns <see cref="FileLibrary"/> with <paramref name="FileLibraryId"/>
|
|
/// </summary>
|
|
/// <param name="FileLibraryId"><see cref="FileLibrary"/>.Key</param>
|
|
/// <response code="200"></response>
|
|
/// <response code="404"><see cref="FileLibrary"/> with <paramref name="FileLibraryId"/> not found.</response>
|
|
[HttpGet("{FileLibraryId}")]
|
|
[ProducesResponseType<FileLibrary>(Status200OK, "application/json")]
|
|
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
|
public async Task<Results<Ok<FileLibrary>, NotFound<string>>> 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(library);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the <see cref="FileLibraryId"/>.BasePath with <paramref name="FileLibraryId"/>
|
|
/// </summary>
|
|
/// <param name="FileLibraryId"><see cref="FileLibrary"/>.Key</param>
|
|
/// <param name="newBasePath">New <see cref="FileLibraryId"/>.BasePath</param>
|
|
/// <response code="200"></response>
|
|
/// <response code="404"><see cref="FileLibrary"/> with <paramref name="FileLibraryId"/> not found.</response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpPatch("{FileLibraryId}/ChangeBasePath")]
|
|
[ProducesResponseType(Status200OK)]
|
|
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the <see cref="FileLibraryId"/>.LibraryName with <paramref name="FileLibraryId"/>
|
|
/// </summary>
|
|
/// <param name="FileLibraryId"><see cref="FileLibrary"/>.Key</param>
|
|
/// <param name="newName">New <see cref="FileLibraryId"/>.LibraryName</param>
|
|
/// <response code="200"></response>
|
|
/// <response code="404"><see cref="FileLibrary"/> with <paramref name="FileLibraryId"/> not found.</response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpPatch("{FileLibraryId}/ChangeName")]
|
|
[ProducesResponseType(Status200OK)]
|
|
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates new <see cref="FileLibrary"/>
|
|
/// </summary>
|
|
/// <param name="library">New <see cref="FileLibrary"/> to add</param>
|
|
/// <response code="200">Key of new Library</response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpPut]
|
|
[ProducesResponseType<string>(Status201Created, "text/plain")]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public async Task<Results<Created<string>, InternalServerError<string>>> CreateNewLibrary ([FromBody]FileLibrary library)
|
|
{
|
|
//TODO Parameter check
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the <see cref="FileLibraryId"/>.LibraryName with <paramref name="FileLibraryId"/>
|
|
/// </summary>
|
|
/// <param name="FileLibraryId"><see cref="FileLibrary"/>.Key</param>
|
|
/// <response code="200"></response>
|
|
/// <response code="404"><see cref="FileLibrary"/> with <paramref name="FileLibraryId"/> not found.</response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpDelete("{FileLibraryId}")]
|
|
[ProducesResponseType(Status200OK)]
|
|
[ProducesResponseType<string>(Status404NotFound, "text/plain")]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public async Task<Results<Ok, NotFound<string>, InternalServerError<string>>> 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();
|
|
}
|
|
} |