mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Refactors the update book slice across the stack
This commit is contained in:
1 parent
9a71a6d32b
commit
001146eefc
27 files changed
+4065
-632
No files matched your search
+72
-33
@@ -1,6 +1,19 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.Mapping.Common.Metadata;
|
||||
using Lumina.Application.Common.Mapping.MediaContributors;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Common;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.Common;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
@@ -11,40 +24,66 @@ namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.B
|
||||
public static class UpdateBookCommandMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="command"/> to <see cref="BookMetadataDto"/>, so that the edited data can be applied to the domain book.
|
||||
/// Converts <paramref name="command"/> to <see cref="Book"/>, preserving the identity and creation metadata of the stored book identified by <paramref name="existingBook"/>.
|
||||
/// </summary>
|
||||
/// <param name="command">The command containing the edited data of the book.</param>
|
||||
/// <returns>The built metadata.</returns>
|
||||
public static BookMetadataDto ToBookMetadataDto(this UpdateBookCommand command)
|
||||
/// <param name="command">The command to be converted.</param>
|
||||
/// <param name="existingBook">The stored book whose identity, file system path and creation metadata are preserved.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully converted <see cref="Book"/>, or an error message.
|
||||
/// </returns>
|
||||
public static Result<Book> ToDomainEntity(this UpdateBookCommand command, BookEntity existingBook)
|
||||
{
|
||||
return new BookMetadataDto(
|
||||
Title: command.Metadata!.Title,
|
||||
OriginalTitle: command.Metadata.OriginalTitle,
|
||||
Description: command.Metadata.Description,
|
||||
ReleaseInfo: command.Metadata.ReleaseInfo,
|
||||
Genres: command.Metadata.Genres,
|
||||
Tags: command.Metadata.Tags,
|
||||
Language: command.Metadata.Language,
|
||||
OriginalLanguage: command.Metadata.OriginalLanguage,
|
||||
Publisher: command.Metadata.Publisher,
|
||||
PageCount: command.Metadata.PageCount,
|
||||
Format: command.Format,
|
||||
Edition: command.Edition,
|
||||
VolumeNumber: command.VolumeNumber,
|
||||
Series: command.Series,
|
||||
ASIN: command.ASIN,
|
||||
GoodreadsId: command.GoodreadsId,
|
||||
LCCN: command.LCCN,
|
||||
OCLCNumber: command.OCLCNumber,
|
||||
OpenLibraryId: command.OpenLibraryId,
|
||||
LibraryThingId: command.LibraryThingId,
|
||||
GoogleBooksId: command.GoogleBooksId,
|
||||
BarnesAndNobleId: command.BarnesAndNobleId,
|
||||
AppleBooksId: command.AppleBooksId,
|
||||
Isbns: command.ISBNs,
|
||||
Contributors: command.Contributors,
|
||||
Ratings: command.Ratings,
|
||||
CoverImagePath: null
|
||||
IEnumerable<Result<BookRating>> domainRatingsResult = command.Ratings!.ToDomainEntities();
|
||||
List<Error> errors = [.. domainRatingsResult.Where(ratingResult => ratingResult.IsFailure).SelectMany(ratingResult => ratingResult.Errors) ?? []];
|
||||
if (errors.Count > 0)
|
||||
return errors;
|
||||
|
||||
IEnumerable<Result<Isbn>> domainIsbnsResult = command.ISBNs!.ToDomainEntities();
|
||||
errors = [.. domainIsbnsResult.Where(isbnResult => isbnResult.IsFailure).SelectMany(isbnResult => isbnResult.Errors) ?? []];
|
||||
if (errors.Count > 0)
|
||||
return errors;
|
||||
|
||||
Result<WrittenContentMetadata> metadataResult = command.Metadata!.ToDomainEntity();
|
||||
if (metadataResult.IsFailure)
|
||||
return metadataResult.Errors;
|
||||
|
||||
// TODO: update Api.Books.md documentation when the functionality is fully implemented.
|
||||
BookSeries? bookSeries = null;
|
||||
if (command.Series != null)
|
||||
{
|
||||
// TODO: add logic to search the book series repository for existing book series, based on the provided title.
|
||||
// TODO: uncomment integration and unit tests about series.
|
||||
}
|
||||
|
||||
// Map the media contributors referenced by the user to their domain counterparts; a contributor is identified by its id and carries the role it played.
|
||||
IEnumerable<Result<BookMediaContributor>> domainContributorsResult = command.Contributors!.ToBookDomainEntities();
|
||||
errors = [.. domainContributorsResult.Where(domainContributor => domainContributor.IsFailure).SelectMany(domainContributor => domainContributor.Errors) ?? []];
|
||||
if (errors.Count > 0)
|
||||
return errors;
|
||||
|
||||
return Book.Create(
|
||||
BookId.Create(existingBook.Id),
|
||||
LibraryId.Create(existingBook.LibraryId),
|
||||
existingBook.Path,
|
||||
metadataResult.Value,
|
||||
Optional<BookFormat>.FromNullable(command.Format),
|
||||
Optional<string>.FromNullable(command.Edition),
|
||||
Optional<float>.FromNullable(command.VolumeNumber),
|
||||
Optional<BookSeries>.FromNullable(bookSeries),
|
||||
Optional<string>.FromNullable(command.ASIN),
|
||||
Optional<string>.FromNullable(command.GoodreadsId),
|
||||
Optional<string>.FromNullable(command.LCCN),
|
||||
Optional<string>.FromNullable(command.OCLCNumber),
|
||||
Optional<string>.FromNullable(command.OpenLibraryId),
|
||||
Optional<string>.FromNullable(command.LibraryThingId),
|
||||
Optional<string>.FromNullable(command.GoogleBooksId),
|
||||
Optional<string>.FromNullable(command.BarnesAndNobleId),
|
||||
Optional<string>.FromNullable(command.AppleBooksId),
|
||||
existingBook.CreatedOnUtc,
|
||||
Optional<DateTime>.FromNullable(existingBook.UpdatedOnUtc),
|
||||
[.. domainIsbnsResult.Select(isbn => isbn.Value)],
|
||||
[.. domainContributorsResult.Select(domainContributor => domainContributor.Value)],
|
||||
[.. domainRatingsResult.Select(domainRating => domainRating.Value)]
|
||||
);
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -15,11 +15,14 @@ public static class UpdateBookRequestMapping
|
||||
/// Converts <paramref name="request"/> to <see cref="UpdateBookCommand"/>.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to be converted.</param>
|
||||
/// <param name="libraryId">The Id of the library the book belongs to, taken from the route.</param>
|
||||
/// <param name="bookId">The Id of the book to update, taken from the route.</param>
|
||||
/// <returns>The converted command.</returns>
|
||||
public static UpdateBookCommand ToCommand(this UpdateBookRequest request)
|
||||
public static UpdateBookCommand ToCommand(this UpdateBookRequest request, string? libraryId, string? bookId)
|
||||
{
|
||||
return new UpdateBookCommand(
|
||||
Guid.TryParse(request.Id, out Guid bookId) ? bookId : Guid.Empty,
|
||||
libraryId,
|
||||
bookId,
|
||||
request.Metadata,
|
||||
request.Format,
|
||||
request.Edition,
|
||||
|
||||
Reference in new issue
Block a user