mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 03:48:19 +02:00
LibrarConnector DTO and CreateLibraryConnectorRecord
This commit is contained in:
15
API/Controllers/DTOs/LibraryConnector.cs
Normal file
15
API/Controllers/DTOs/LibraryConnector.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using API.Schema.LibraryContext.LibraryConnectors;
|
||||||
|
|
||||||
|
namespace API.Controllers.DTOs;
|
||||||
|
|
||||||
|
public record LibraryConnector(string Key, string BaseUrl, LibraryType Type) : Identifiable(Key)
|
||||||
|
{
|
||||||
|
[StringLength(256)]
|
||||||
|
[Required]
|
||||||
|
[Url]
|
||||||
|
public string BaseUrl {get; init;} = BaseUrl;
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public LibraryType Type { get; init; } = Type;
|
||||||
|
}
|
@@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Http.HttpResults;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using static Microsoft.AspNetCore.Http.StatusCodes;
|
using static Microsoft.AspNetCore.Http.StatusCodes;
|
||||||
|
using LibraryConnector = API.Controllers.DTOs.LibraryConnector;
|
||||||
|
|
||||||
// ReSharper disable InconsistentNaming
|
// ReSharper disable InconsistentNaming
|
||||||
|
|
||||||
namespace API.Controllers;
|
namespace API.Controllers;
|
||||||
@@ -15,7 +17,7 @@ namespace API.Controllers;
|
|||||||
public class LibraryConnectorController(LibraryContext context) : Controller
|
public class LibraryConnectorController(LibraryContext context) : Controller
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all configured <see cref="LibraryConnector"/>
|
/// Gets all configured <see cref="DTOs.LibraryConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <response code="200"></response>
|
/// <response code="200"></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
@@ -26,7 +28,9 @@ public class LibraryConnectorController(LibraryContext context) : Controller
|
|||||||
if (await context.LibraryConnectors.ToListAsync(HttpContext.RequestAborted) is not { } connectors)
|
if (await context.LibraryConnectors.ToListAsync(HttpContext.RequestAborted) is not { } connectors)
|
||||||
return TypedResults.InternalServerError();
|
return TypedResults.InternalServerError();
|
||||||
|
|
||||||
return TypedResults.Ok(connectors);
|
List<LibraryConnector> libraryConnectors = connectors.Select(c => new LibraryConnector(c.Key, c.BaseUrl, c.LibraryType)).ToList();
|
||||||
|
|
||||||
|
return TypedResults.Ok(libraryConnectors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -43,26 +47,35 @@ public class LibraryConnectorController(LibraryContext context) : Controller
|
|||||||
if (await context.LibraryConnectors.FirstOrDefaultAsync(l => l.Key == LibraryConnectorId) is not { } connector)
|
if (await context.LibraryConnectors.FirstOrDefaultAsync(l => l.Key == LibraryConnectorId) is not { } connector)
|
||||||
return TypedResults.NotFound(nameof(LibraryConnectorId));
|
return TypedResults.NotFound(nameof(LibraryConnectorId));
|
||||||
|
|
||||||
return TypedResults.Ok(connector);
|
return TypedResults.Ok(new LibraryConnector(connector.Key, connector.BaseUrl, connector.LibraryType));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new <see cref="LibraryConnector"/>
|
/// Creates a new <see cref="LibraryConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="libraryConnector"></param>
|
/// <param name="requestData"></param>
|
||||||
/// <response code="201"></response>
|
/// <response code="201"></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
[ProducesResponseType<string>(Status201Created, "text/plain")]
|
[ProducesResponseType<string>(Status201Created, "text/plain")]
|
||||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||||
public async Task<Results<Created<string>, InternalServerError<string>>> CreateConnector ([FromBody]LibraryConnector libraryConnector)
|
public async Task<Results<Created<string>, InternalServerError<string>>> CreateConnector ([FromBody]CreateLibraryConnectorRecord requestData)
|
||||||
{
|
{
|
||||||
context.LibraryConnectors.Add(libraryConnector);
|
//TODO verify data
|
||||||
|
API.Schema.LibraryContext.LibraryConnectors.LibraryConnector connector = requestData.LibraryType switch
|
||||||
|
{
|
||||||
|
LibraryType.Kavita => new Kavita(requestData.Url, requestData.Username, requestData.Password),
|
||||||
|
LibraryType.Komga => new Komga(requestData.Url, requestData.Username, requestData.Password),
|
||||||
|
_ => throw new NotImplementedException()
|
||||||
|
};
|
||||||
|
|
||||||
|
context.LibraryConnectors.Add(connector);
|
||||||
|
|
||||||
if(await context.Sync(HttpContext.RequestAborted) is { success: false } result)
|
if(await context.Sync(HttpContext.RequestAborted) is { success: false } result)
|
||||||
return TypedResults.InternalServerError(result.exceptionMessage);
|
return TypedResults.InternalServerError(result.exceptionMessage);
|
||||||
return TypedResults.Created(string.Empty, libraryConnector.Key);
|
return TypedResults.Created(string.Empty, connector.Key);
|
||||||
}
|
}
|
||||||
|
public sealed record CreateLibraryConnectorRecord(LibraryType LibraryType, string Url, string Username, string Password);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes <see cref="LibraryConnector"/> with <paramref name="LibraryConnectorId"/>
|
/// Deletes <see cref="LibraryConnector"/> with <paramref name="LibraryConnectorId"/>
|
||||||
|
@@ -9,18 +9,9 @@ namespace API.Schema.LibraryContext.LibraryConnectors;
|
|||||||
[PrimaryKey("Key")]
|
[PrimaryKey("Key")]
|
||||||
public abstract class LibraryConnector : Identifiable
|
public abstract class LibraryConnector : Identifiable
|
||||||
{
|
{
|
||||||
[Required]
|
|
||||||
public LibraryType LibraryType { get; init; }
|
public LibraryType LibraryType { get; init; }
|
||||||
[StringLength(256)]
|
|
||||||
[Required]
|
|
||||||
[Url]
|
|
||||||
public string BaseUrl { get; init; }
|
public string BaseUrl { get; init; }
|
||||||
[StringLength(256)]
|
|
||||||
[Required]
|
|
||||||
public string Auth { get; init; }
|
public string Auth { get; init; }
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
[NotMapped]
|
|
||||||
protected ILog Log { get; init; }
|
protected ILog Log { get; init; }
|
||||||
|
|
||||||
protected LibraryConnector(LibraryType libraryType, string baseUrl, string auth)
|
protected LibraryConnector(LibraryType libraryType, string baseUrl, string auth)
|
||||||
|
Reference in New Issue
Block a user