Adds the capability of adding a Book via API

This commit is contained in:
BinaryAlley committed 2024-08-24 22:11:55 +03:00
1 parent 6459b6f2c2
commit 0eb7b453f6
61 files changed
+1276 -255

No files matched your search

@@ -0,0 +1,31 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Enums;
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Contributors;
#endregion
namespace Lumina.Presentation.Api.Common.Contracts.Books;
/// <summary>
/// Represents a request to add a book.
/// </summary>
public record AddBookRequest(
WrittenContentMetadataDto Metadata,
BookFormat Format,
string? Edition,
int? VolumeNumber,
BookSeriesDto? Series,
string? ASIN,
string? GoodreadsId,
string? LCCN,
string? OCLCNumber,
string? OpenLibraryId,
string? LibraryThingId,
string? GoogleBooksId,
string? BarnesAndNobleId,
string? KoboId,
string? AppleBooksId,
List<IsbnDto> ISBNs,
List<MediaContributorDto> Contributors,
List<BookRatingDto> Ratings
);
@@ -1,6 +1,10 @@
#region ========================================================================= USING =====================================================================================
using System.Text.Json.Serialization;
using Lumina.Application.Common.Converters;
using Lumina.Presentation.Api.Common.Configuration;
using Lumina.Presentation.Api.Common.Errors;
using Mapster;
using MapsterMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
#endregion
@@ -21,7 +25,11 @@ public static class PresentationApiLayerServices
public static IServiceCollection AddPresentationApiLayerServices(this IServiceCollection services)
{
// add services to the container
services.AddControllers();
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.Converters.Add(new OptionalJsonConverterFactory());
});
// add services to the container.
services.AddEndpointsApiExplorer();
@@ -43,6 +51,12 @@ public static class PresentationApiLayerServices
services.AddSingleton<ProblemDetailsFactory, CustomProblemDetailsFactory>();
// register mapster configs
var mapsterConfig = TypeAdapterConfig.GlobalSettings;
mapsterConfig.Scan(typeof(PresentationApiLayerServices).Assembly);
services.AddSingleton(mapsterConfig);
services.AddScoped<IMapper, ServiceMapper>();
return services;
}
#endregion
@@ -1,11 +1,7 @@
#region ========================================================================= USING =====================================================================================
using System;
using ErrorOr;
using System.Linq;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Lumina.Presentation.Api.Common.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
using Lumina.Presentation.Api.Common.Contracts.Books;
using Mapster;
#endregion
namespace Lumina.Presentation.Api.Common.Mapping;
/// <summary>
/// Mapping configuration for books.
/// </summary>
public class BookMappingConfig : IRegister
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Registers the mapping configuration.
/// </summary>
/// <param name="config">The type adapter configuration.</param>
public void Register(TypeAdapterConfig config)
{
config.NewConfig<AddBookRequest, AddBookCommand>()
.Map(dest => dest.ASIN, src => src.ASIN)
.Map(dest => dest.ISBNs, src => src.ISBNs)
.Map(dest => dest.LCCN, src => src.LCCN)
.Map(dest => dest.OCLCNumber, src => src.OCLCNumber)
.MapToConstructor(true);
}
#endregion
}
@@ -1,6 +1,8 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
using Lumina.Presentation.Api.Common.Contracts.Books;
using Lumina.Presentation.Api.Controllers.Common;
using MapsterMapper;
using Mediator;
using Microsoft.AspNetCore.Mvc;
#endregion
@@ -15,7 +17,8 @@ namespace Lumina.Presentation.Api.Controllers.Books;
public class BooksController : ApiController
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly ISender mediator;
private readonly ISender _mediator;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -23,17 +26,18 @@ public class BooksController : ApiController
/// Initializes a new instance of the <see cref="BooksController"/> class.
/// </summary>
/// <param name="mediator">Injecting service for mediating commands and queries.</param>
public BooksController(ISender mediator)
/// <param name="mapper">Injected service for mapping objects.</param>
public BooksController(ISender mediator, IMapper mapper)
{
this.mediator = mediator;
_mediator = mediator;
_mapper = mapper;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Gets all books.
/// Controller action for getting the list of all books.
/// </summary>
/// <returns>An <see cref="ErrorOr{T}"/> containing either a collection of books, or an error.</returns>
[HttpGet()]
public async Task<IActionResult> GetBooks()
{
@@ -42,15 +46,15 @@ public class BooksController : ApiController
return Ok(new List<string> { "Book 1", "Book 2", "Book 3" });
}
// /// <summary>
// /// Adds a book.
// /// </summary>
// /// <param name="command"></param>
// /// <returns>The result of the HTTP request.</returns>
// public async Task<IActionResult> AddBook(AddBookCommand command)
// {
// ErrorOr<Unit> result = await mediator.Send(command);
// return result.Match(result => Ok(result), errors => Problem(errors));
// }
/// <summary>
/// Controller action for adding a book.
/// </summary>
/// <param name="command">The command to add a book.</param>
[HttpPost()]
public async Task<IActionResult> AddBook(AddBookRequest request)
{
var result = await _mediator.Send(_mapper.Map<AddBookCommand>(request));
return result.Match(result => Ok(result), errors => Problem(errors));
}
#endregion
}
@@ -8,10 +8,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Mapster.DependencyInjection" Version="1.0.1" />
<PackageReference Include="Mediator.Abstractions" Version="2.1.7" />
<PackageReference Include="Mediator.SourceGenerator" Version="2.1.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="Mediator.SourceGenerator" Version="2.1.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />