mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 02:01:10 +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
|
||||
);
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.Models.Books;
|
||||
using Lumina.Application.Common.Models.Contributors;
|
||||
using Lumina.Domain.Common.Enums;
|
||||
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate;
|
||||
using Mediator;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Command for adding a book.
|
||||
/// </summary>
|
||||
public record AddBookCommand(
|
||||
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
|
||||
) : IRequest<ErrorOr<Book>>;
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Enums;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Common.ValueObjects.Metadata;
|
||||
using Lumina.Domain.Core.Aggregates.MediaContributor.MediaContributorAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate;
|
||||
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate.Entities;
|
||||
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate.ValueObjects;
|
||||
using Mediator;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the command to add a book.
|
||||
/// </summary>
|
||||
public class AddBookCommandHandler : IRequestHandler<AddBookCommand, ErrorOr<Book>>
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Handles the command to add a book.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{T}"/> containing either a successfully created <see cref="Book"/>, or an error message.
|
||||
/// </returns>
|
||||
public async ValueTask<ErrorOr<Book>> Handle(AddBookCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
List<MediaContributorId> contributorIds = new();
|
||||
foreach(var mediaContributor in request.Contributors)
|
||||
{
|
||||
// TODO: add logic to search the media contributors repository for existing contributors, based on the provided names
|
||||
}
|
||||
BookSeries? bookSeries = null;
|
||||
if (request.Series != null)
|
||||
{
|
||||
// TODO: add logic to search the book series repository for existing book series, based on the provided title
|
||||
}
|
||||
var domainRatingsResult = request.Ratings.ConvertAll(rating => BookRating.Create(
|
||||
rating.Value,
|
||||
rating.MaxValue,
|
||||
Optional<BookRatingSource>.FromNullable(rating.Source.HasValue ? (BookRatingSource)(int)rating.Source : default),
|
||||
Optional<int>.FromNullable(rating.VoteCount)));
|
||||
// check if any of the results contain errors
|
||||
var errors = domainRatingsResult.Where(ratingResult => ratingResult.IsError).SelectMany(ratingResult => ratingResult.Errors).ToList();
|
||||
if (errors.Count != 0) // if there are errors, return them
|
||||
return errors;
|
||||
|
||||
List<Rating> domainRatings = domainRatingsResult.Select(rating => rating.Value as Rating).ToList();
|
||||
|
||||
var domainGenresResult = request.Metadata.Genres.ConvertAll(genre => Genre.Create(genre.Name));
|
||||
errors = domainGenresResult.Where(genreResult => genreResult.IsError).SelectMany(genreResult => genreResult.Errors).ToList();
|
||||
if (errors.Count != 0)
|
||||
return errors;
|
||||
List<Genre> domainGenres = domainGenresResult.Select(genre => genre.Value).ToList();
|
||||
|
||||
var domainTagsResult = request.Metadata.Tags.ConvertAll(tag => Tag.Create(tag.Name));
|
||||
errors = domainTagsResult.Where(tagResult => tagResult.IsError).SelectMany(tagResult => tagResult.Errors).ToList();
|
||||
if (errors.Count != 0)
|
||||
return errors;
|
||||
List<Tag> domainTags = domainTagsResult.Select(tag => tag.Value).ToList();
|
||||
|
||||
|
||||
var domainIsbnsResult = request.ISBNs.ConvertAll(isbn => Isbn.Create(isbn.Value, (IsbnFormat)(int)isbn.Format));
|
||||
errors = domainIsbnsResult.Where(isbnResult => isbnResult.IsError).SelectMany(isbnResult => isbnResult.Errors).ToList();
|
||||
if (errors.Count != 0)
|
||||
return errors;
|
||||
List<Isbn> domainIsbns = domainIsbnsResult.Select(isbn => isbn.Value).ToList();
|
||||
|
||||
var releaseInfoResult = ReleaseInfo.Create(
|
||||
Optional<DateOnly>.FromNullable(request.Metadata.ReleaseInfo.OriginalReleaseDate),
|
||||
Optional<int>.FromNullable(request.Metadata.ReleaseInfo.OriginalReleaseYear),
|
||||
Optional<DateOnly>.FromNullable(request.Metadata.ReleaseInfo.ReReleaseDate),
|
||||
Optional<int>.FromNullable(request.Metadata.ReleaseInfo.ReReleaseYear),
|
||||
Optional<string>.FromNullable(request.Metadata.ReleaseInfo.ReleaseCountry),
|
||||
Optional<string>.FromNullable(request.Metadata.ReleaseInfo.ReleaseVersion)
|
||||
);
|
||||
if (releaseInfoResult.IsError)
|
||||
return releaseInfoResult.Errors;
|
||||
ReleaseInfo releaseInfo = releaseInfoResult.Value;
|
||||
LanguageInfo? languageInfo = null;
|
||||
if (request.Metadata.Language is not null)
|
||||
{
|
||||
var languageInfoResult = LanguageInfo.Create(
|
||||
request.Metadata.Language.LanguageCode,
|
||||
request.Metadata.Language.LanguageName,
|
||||
Optional<string>.FromNullable(request.Metadata.Language.NativeName));
|
||||
if (languageInfoResult.IsError)
|
||||
return languageInfoResult.Errors;
|
||||
languageInfo = languageInfoResult.Value;
|
||||
}
|
||||
LanguageInfo? originalLanguageInfo = null;
|
||||
if (request.Metadata.OriginalLanguage is not null)
|
||||
{
|
||||
var originalLanguageInfoResult = LanguageInfo.Create(
|
||||
request.Metadata.OriginalLanguage.LanguageCode,
|
||||
request.Metadata.OriginalLanguage.LanguageName,
|
||||
Optional<string>.FromNullable(request.Metadata.OriginalLanguage.NativeName));
|
||||
if (originalLanguageInfoResult.IsError)
|
||||
return originalLanguageInfoResult.Errors;
|
||||
originalLanguageInfo = originalLanguageInfoResult.Value;
|
||||
}
|
||||
WrittenContentMetadata metadata = WrittenContentMetadata.Create(
|
||||
request.Metadata.Title,
|
||||
Optional<string>.FromNullable(request.Metadata.OriginalTitle),
|
||||
Optional<string>.FromNullable(request.Metadata.Description),
|
||||
releaseInfo,
|
||||
domainGenres,
|
||||
domainTags,
|
||||
Optional<LanguageInfo>.FromNullable(languageInfo),
|
||||
Optional<LanguageInfo>.FromNullable(originalLanguageInfo),
|
||||
Optional<string>.FromNullable(request.Metadata.Publisher),
|
||||
Optional<int>.FromNullable(request.Metadata.PageCount)
|
||||
);
|
||||
|
||||
var createBookResult = Book.Create(
|
||||
metadata,
|
||||
request.Format,
|
||||
Optional<string>.FromNullable(request.Edition),
|
||||
request.VolumeNumber ?? default,
|
||||
Optional<BookSeries>.FromNullable(bookSeries),
|
||||
Optional<string>.FromNullable(request.ASIN),
|
||||
Optional<string>.FromNullable(request.GoodreadsId),
|
||||
Optional<string>.FromNullable(request.LCCN),
|
||||
Optional<string>.FromNullable(request.OCLCNumber),
|
||||
Optional<string>.FromNullable(request.OpenLibraryId),
|
||||
Optional<string>.FromNullable(request.LibraryThingId),
|
||||
Optional<string>.FromNullable(request.GoogleBooksId),
|
||||
Optional<string>.FromNullable(request.BarnesAndNobleId),
|
||||
Optional<string>.FromNullable(request.KoboId),
|
||||
Optional<string>.FromNullable(request.AppleBooksId),
|
||||
domainIsbns,
|
||||
contributorIds,
|
||||
ratings: domainRatings
|
||||
);
|
||||
if (createBookResult.IsError)
|
||||
return createBookResult.Errors;
|
||||
return createBookResult.Value;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="AddBookCommand"/>.
|
||||
/// </summary>
|
||||
public class AddBookCommandValidator : AbstractValidator<AddBookCommand>
|
||||
{
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddBookCommandValidator"/> class.
|
||||
/// </summary>
|
||||
public AddBookCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.Metadata).NotNull()
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m.Title).NotEmpty().MaximumLength(255);
|
||||
metadata.RuleFor(m => m.OriginalTitle).MaximumLength(255);
|
||||
metadata.RuleFor(m => m.Description).MaximumLength(2000);
|
||||
metadata.RuleFor(m => m.ReleaseInfo).NotNull()
|
||||
.ChildRules(ri =>
|
||||
{
|
||||
ri.RuleFor(r => r.OriginalReleaseYear).InclusiveBetween(1, 9999).When(r => r.OriginalReleaseYear.HasValue);
|
||||
ri.RuleFor(r => r.ReReleaseYear).InclusiveBetween(1, 9999).When(r => r.ReReleaseYear.HasValue);
|
||||
ri.RuleFor(r => r.ReleaseCountry).MaximumLength(50);
|
||||
ri.RuleFor(r => r.ReleaseVersion).MaximumLength(50);
|
||||
});
|
||||
metadata.RuleFor(m => m.Genres).NotEmpty();
|
||||
metadata.RuleForEach(m => m.Genres).ChildRules(g => g.RuleFor(genre => genre.Name).NotEmpty().MaximumLength(50));
|
||||
metadata.RuleForEach(m => m.Tags).ChildRules(t => t.RuleFor(tag => tag.Name).NotEmpty().MaximumLength(50));
|
||||
metadata.RuleFor(m => m.Language).ChildRules(l =>
|
||||
{
|
||||
l.RuleFor(x => x.LanguageCode).NotEmpty().MaximumLength(10);
|
||||
l.RuleFor(x => x.LanguageName).NotEmpty().MaximumLength(50);
|
||||
l.RuleFor(x => x.NativeName).MaximumLength(50);
|
||||
}).When(m => m.Language != null);
|
||||
metadata.RuleFor(m => m.OriginalLanguage).ChildRules(l =>
|
||||
{
|
||||
l.RuleFor(x => x.LanguageCode).NotEmpty().MaximumLength(10);
|
||||
l.RuleFor(x => x.LanguageName).NotEmpty().MaximumLength(50);
|
||||
l.RuleFor(x => x.NativeName).MaximumLength(50);
|
||||
}).When(m => m.OriginalLanguage != null);
|
||||
metadata.RuleFor(m => m.Publisher).MaximumLength(100);
|
||||
metadata.RuleFor(m => m.PageCount).GreaterThan(0).When(m => m.PageCount.HasValue);
|
||||
});
|
||||
|
||||
RuleFor(x => x.Format).IsInEnum();
|
||||
RuleFor(x => x.Edition).MaximumLength(50);
|
||||
RuleFor(x => x.VolumeNumber).GreaterThan(0).When(x => x.VolumeNumber.HasValue);
|
||||
|
||||
RuleFor(x => x.Series).ChildRules(s => s.RuleFor(series => series.Title).NotEmpty().MaximumLength(255)).When(x => x.Series != null);
|
||||
|
||||
RuleFor(x => x.ASIN).MaximumLength(10);
|
||||
RuleFor(x => x.GoodreadsId).MaximumLength(20);
|
||||
RuleFor(x => x.LCCN).MaximumLength(20);
|
||||
RuleFor(x => x.OCLCNumber).MaximumLength(20);
|
||||
RuleFor(x => x.OpenLibraryId).MaximumLength(20);
|
||||
RuleFor(x => x.LibraryThingId).MaximumLength(20);
|
||||
RuleFor(x => x.GoogleBooksId).MaximumLength(20);
|
||||
RuleFor(x => x.BarnesAndNobleId).MaximumLength(20);
|
||||
RuleFor(x => x.KoboId).MaximumLength(20);
|
||||
RuleFor(x => x.AppleBooksId).MaximumLength(20);
|
||||
|
||||
RuleFor(x => x.ISBNs).NotEmpty();
|
||||
RuleForEach(x => x.ISBNs).ChildRules(isbn =>
|
||||
{
|
||||
isbn.RuleFor(i => i.Value).NotEmpty().MaximumLength(13);
|
||||
isbn.RuleFor(i => i.Format).IsInEnum();
|
||||
});
|
||||
|
||||
RuleFor(x => x.Contributors).NotEmpty();
|
||||
RuleForEach(x => x.Contributors).ChildRules(contributor =>
|
||||
{
|
||||
contributor.RuleFor(c => c.Name).NotEmpty().MaximumLength(100);
|
||||
contributor.RuleFor(c => c.Role).ChildRules(role =>
|
||||
{
|
||||
role.RuleFor(r => r.Name).NotEmpty().MaximumLength(50);
|
||||
role.RuleFor(r => r.Category).NotEmpty().MaximumLength(50);
|
||||
});
|
||||
});
|
||||
|
||||
RuleForEach(x => x.Ratings).ChildRules(rating =>
|
||||
{
|
||||
rating.RuleFor(r => r.Value).Must((r, value) => value >= 0 && value <= r.MaxValue)
|
||||
.WithMessage("Value must be between 0 and MaxValue");
|
||||
rating.RuleFor(r => r.MaxValue).GreaterThan(0);
|
||||
rating.RuleFor(r => r.VoteCount).GreaterThanOrEqualTo(0).When(r => r.VoteCount.HasValue);
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -1,21 +1,5 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Lumina.Domain\Lumina.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.9.2" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
|
||||
<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>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
@@ -23,4 +7,21 @@
|
||||
<BaseOutputPath>..\..\bin\</BaseOutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Lumina.Domain\Lumina.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Mapster" Version="7.4.0" />
|
||||
<PackageReference Include="FluentValidation" Version="11.9.2" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
|
||||
<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>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in new issue
Block a user