mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-04 01:44:17 +02:00
141 lines
5.9 KiB
C#
141 lines
5.9 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Returns all <see cref="FileLibrary"/>
|
|
/// </summary>
|
|
/// <response code="200"></response>
|
|
[HttpGet]
|
|
[ProducesResponseType<FileLibrary[]>(Status200OK, "application/json")]
|
|
public IActionResult GetFileLibraries()
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
|
|
return Ok(context.FileLibraries.ToArray());
|
|
}
|
|
|
|
/// <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(Status404NotFound)]
|
|
public IActionResult GetFileLibrary(string FileLibraryId)
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
if (context.FileLibraries.Find(FileLibraryId) is not { } library)
|
|
return NotFound();
|
|
|
|
return 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(Status404NotFound)]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public IActionResult ChangeLibraryBasePath(string FileLibraryId, [FromBody]string newBasePath)
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
if (context.FileLibraries.Find(FileLibraryId) is not { } library)
|
|
return NotFound();
|
|
|
|
//TODO Path check
|
|
library.BasePath = newBasePath;
|
|
|
|
if(context.Sync().Result is { success: false } result)
|
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
|
return 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(Status404NotFound)]
|
|
[ProducesResponseType(Status400BadRequest)]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public IActionResult ChangeLibraryName(string FileLibraryId, [FromBody] string newName)
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
if (context.FileLibraries.Find(FileLibraryId) is not { } library)
|
|
return NotFound();
|
|
|
|
//TODO Name check
|
|
library.LibraryName = newName;
|
|
|
|
if(context.Sync().Result is { success: false } result)
|
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates new <see cref="FileLibraryId"/>
|
|
/// </summary>
|
|
/// <param name="library">New <see cref="FileLibrary"/> to add</param>
|
|
/// <response code="200"></response>
|
|
/// <response code="500">Error during Database Operation</response>
|
|
[HttpPut]
|
|
[ProducesResponseType(Status201Created)]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public IActionResult CreateNewLibrary([FromBody]FileLibrary library)
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
|
|
//TODO Parameter check
|
|
context.FileLibraries.Add(library);
|
|
|
|
if(context.Sync().Result is { success: false } result)
|
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
|
return Created();
|
|
}
|
|
|
|
/// <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="500">Error during Database Operation</response>
|
|
[HttpDelete("{FileLibraryId}")]
|
|
[ProducesResponseType(Status200OK)]
|
|
[ProducesResponseType(Status404NotFound)]
|
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
|
public IActionResult DeleteLocalLibrary(string FileLibraryId)
|
|
{
|
|
MangaContext context = scope.ServiceProvider.GetRequiredService<MangaContext>();
|
|
if (context.FileLibraries.Find(FileLibraryId) is not { } library)
|
|
return NotFound();
|
|
|
|
context.FileLibraries.Remove(library);
|
|
|
|
if(context.Sync().Result is { success: false } result)
|
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
|
return Ok();
|
|
}
|
|
} |