mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 14:01:11 +00:00
Adds the capability of adding a Book via API
This commit is contained in:
1 parent
6459b6f2c2
commit
0eb7b453f6
61 files changed
+1276
-255
No files matched your search
@@ -47,7 +47,7 @@ public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TReques
|
||||
if (validationResult.IsValid)
|
||||
return await next(request, cancellationToken);
|
||||
// after the command handler is executed
|
||||
var errors = validationResult.Errors.ConvertAll(validationFailure => Error.Validation(validationFailure.ErrorMessage));
|
||||
var errors = validationResult.Errors.ConvertAll(validationFailure => Error.Validation(description: validationFailure.ErrorMessage));
|
||||
// the compiler doesn't know there is an implicit converter from a list of errors to the ErrorOr object, and unfortunately, there is no way around this but to use some magic
|
||||
// this is acceptable because we DO know that we will always have a list of errors of type ErrorOr (check the generic constraint at the top of the class!)
|
||||
return (dynamic)errors;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Converts an <see cref="Optional{T}"/> to and from JSON.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value represented by the <see cref="Optional{T}"/> structure.</typeparam>
|
||||
public class OptionalJsonConverter<T> : JsonConverter<Optional<T>>
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Reads and converts the JSON to an instance of <see cref="Optional{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
|
||||
/// <param name="typeToConvert">The type to convert.</param>
|
||||
/// <param name="options">An object that specifies serialization options to use.</param>
|
||||
/// <returns>The converted <see cref="Optional{T}"/> value.</returns>
|
||||
public override Optional<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
return Optional<T>.None();
|
||||
var value = JsonSerializer.Deserialize<T>(ref reader, options);
|
||||
return Optional<T>.Some(value!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an <see cref="Optional{T}"/> value as JSON.
|
||||
/// </summary>
|
||||
/// <param name="writer">The <see cref="Utf8JsonWriter"/> to write to.</param>
|
||||
/// <param name="value">The value to convert to JSON.</param>
|
||||
/// <param name="options">An object that specifies serialization options to use.</param>
|
||||
public override void Write(Utf8JsonWriter writer, Optional<T> value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.HasValue)
|
||||
JsonSerializer.Serialize(writer, value.Value, options);
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// A factory for creating <see cref="OptionalJsonConverter{T}"/> instances.
|
||||
/// </summary>
|
||||
public class OptionalJsonConverterFactory : JsonConverterFactory
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Determines whether the converter can convert the specified type.
|
||||
/// </summary>
|
||||
/// <param name="typeToConvert">The type to check for convertibility.</param>
|
||||
/// <returns><see langword="true"/> if the type is <see cref="Optional{T}"/>, <see langword="false"/> otherwise.</returns>
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
if (!typeToConvert.IsGenericType)
|
||||
return false;
|
||||
return typeToConvert.GetGenericTypeDefinition() == typeof(Optional<>);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a converter for the specified type.
|
||||
/// </summary>
|
||||
/// <param name="typeToConvert">The type for which to create a converter.</param>
|
||||
/// <param name="options">The serializer options to use.</param>
|
||||
/// <returns>A converter for <see cref="Optional{T}"/>.</returns>
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
Type valueType = typeToConvert.GetGenericArguments()[0];
|
||||
JsonConverter converter = (JsonConverter)Activator.CreateInstance(
|
||||
typeof(OptionalJsonConverter<>).MakeGenericType(valueType))!;
|
||||
return converter;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -3,6 +3,8 @@ using Lumina.Application.Common.Behaviors;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FluentValidation;
|
||||
using MapsterMapper;
|
||||
using Mapster;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DependencyInjection;
|
||||
@@ -32,6 +34,12 @@ public static class ApplicationLayerServices
|
||||
// register fluent validators
|
||||
services.AddValidatorsFromAssembly(typeof(ApplicationLayerServices).Assembly);
|
||||
|
||||
// register mapster configs in this assembly
|
||||
var mapsterConfig = TypeAdapterConfig.GlobalSettings;
|
||||
mapsterConfig.Scan(typeof(ApplicationLayerServices).Assembly);
|
||||
services.AddSingleton(mapsterConfig);
|
||||
services.AddScoped<IMapper, ServiceMapper>();
|
||||
|
||||
return services;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the format of a book.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum BookFormat
|
||||
{
|
||||
Hardcover,
|
||||
Paperback,
|
||||
MassMarketPaperback,
|
||||
TradePaperback,
|
||||
eBook,
|
||||
Audiobook,
|
||||
LargePrint,
|
||||
BoardBook,
|
||||
SpiralBound,
|
||||
LibraryBinding,
|
||||
LeatherBound,
|
||||
PopupBook
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration for the various sources of book ratings.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum BookRatingSource
|
||||
{
|
||||
User,
|
||||
Goodreads,
|
||||
Amazon,
|
||||
GoogleBooks,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration for the different ISBN (International Standard Book Number) formats.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum IsbnFormat
|
||||
{
|
||||
Isbn10,
|
||||
Isbn13
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Enumeration for the various roles of media contributors across different types of media.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum MediaContributorRole
|
||||
{
|
||||
Actor,
|
||||
Director,
|
||||
Singer,
|
||||
Author,
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Enums;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Models.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a book.
|
||||
/// </summary>
|
||||
public record BookDto(
|
||||
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<Guid> Contributors,
|
||||
List<BookRatingDto> Ratings
|
||||
);
|
||||
@@ -0,0 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Enums;
|
||||
using Lumina.Application.Common.Models.Common;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Models.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a book rating.
|
||||
/// </summary>
|
||||
public record BookRatingDto(
|
||||
decimal Value,
|
||||
decimal MaxValue,
|
||||
BookRatingSource? Source,
|
||||
int? VoteCount
|
||||
) : RatingDto(Value, MaxValue, VoteCount);
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Lumina.Application.Common.Models.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get book series information.
|
||||
/// </summary>
|
||||
public record BookSeriesDto(
|
||||
string Title
|
||||
);
|
||||
@@ -0,0 +1,13 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Enums;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Models.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get ISBN information.
|
||||
/// </summary>
|
||||
public record IsbnDto(
|
||||
string Value,
|
||||
IsbnFormat Format
|
||||
);
|
||||
@@ -0,0 +1,21 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Models.Common;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Models.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Represents written content metadata.
|
||||
/// </summary>
|
||||
public record WrittenContentMetadataDto(
|
||||
string Title,
|
||||
string? OriginalTitle,
|
||||
string? Description,
|
||||
ReleaseInfoDto ReleaseInfo,
|
||||
List<GenreDto> Genres,
|
||||
List<TagDto> Tags,
|
||||
LanguageInfoDto? Language,
|
||||
LanguageInfoDto? OriginalLanguage,
|
||||
string? Publisher,
|
||||
int? PageCount
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Lumina.Application.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get genre information.
|
||||
/// </summary>
|
||||
public record GenreDto(
|
||||
string Name
|
||||
);
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Lumina.Application.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get language information.
|
||||
/// </summary>
|
||||
public record LanguageInfoDto(
|
||||
string LanguageCode,
|
||||
string LanguageName,
|
||||
string? NativeName
|
||||
);
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Lumina.Application.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a rating.
|
||||
/// </summary>
|
||||
public abstract record RatingDto(
|
||||
decimal Value,
|
||||
decimal MaxValue,
|
||||
int? VoteCount
|
||||
);
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Lumina.Application.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get release information.
|
||||
/// </summary>
|
||||
public record ReleaseInfoDto(
|
||||
DateOnly? OriginalReleaseDate,
|
||||
int? OriginalReleaseYear,
|
||||
DateOnly? ReReleaseDate,
|
||||
int? ReReleaseYear,
|
||||
string? ReleaseCountry,
|
||||
string? ReleaseVersion
|
||||
);
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Lumina.Application.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to get tag information.
|
||||
/// </summary>
|
||||
public record TagDto(
|
||||
string Name
|
||||
);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Lumina.Application.Common.Models.Contributors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a media contributor.
|
||||
/// </summary>
|
||||
public record MediaContributorDto(
|
||||
string Name,
|
||||
MediaContributorRoleDto Role
|
||||
);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Lumina.Application.Common.Models.Contributors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a media contributor name.
|
||||
/// </summary>
|
||||
public record MediaContributorNameDto(
|
||||
string DisplayName,
|
||||
string? LegalName
|
||||
);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Lumina.Application.Common.Models.Contributors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a media contributor role.
|
||||
/// </summary>
|
||||
public record MediaContributorRoleDto(
|
||||
string Name,
|
||||
string Category
|
||||
);
|
||||
Reference in new issue
Block a user