#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.Mapping.Common.Metadata;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Common;
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary;
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
using Lumina.Domain.Common.Enums.BookLibrary;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Common.ValueObjects.Metadata;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
///
/// Extension methods for converting .
///
public static class BookEntityMapping
{
///
/// Converts to .
///
/// The repository entity to be converted.
///
/// An containing either a successfuly converted , or an error message.
///
public static ErrorOr ToDomainEntity(this BookEntity repositoryEntity)
{
IEnumerable> tagsResult = repositoryEntity.Tags.ToDomainEntities();
foreach (ErrorOr tagResult in tagsResult)
if (tagResult.IsError)
return tagResult.Errors;
IEnumerable> genresResult = repositoryEntity.Genres.ToDomainEntities();
foreach (ErrorOr genreResult in genresResult)
if (genreResult.IsError)
return genreResult.Errors;
IEnumerable> isbnsResult = repositoryEntity.ISBNs.ToDomainEntities();
foreach (ErrorOr isbnResult in isbnsResult)
if (isbnResult.IsError)
return isbnResult.Errors;
IEnumerable> bookRatingsResult = repositoryEntity.Ratings.ToDomainEntities();
foreach (ErrorOr bookRatingResult in bookRatingsResult)
if (bookRatingResult.IsError)
return bookRatingResult.Errors;
ErrorOr releaseInfoResult = ReleaseInfo.Create(
Optional.FromNullable(repositoryEntity.OriginalReleaseDate),
Optional.FromNullable(repositoryEntity.OriginalReleaseYear),
Optional.FromNullable(repositoryEntity.ReReleaseDate),
Optional.FromNullable(repositoryEntity.ReReleaseYear),
Optional.FromNullable(repositoryEntity.ReleaseCountry),
Optional.FromNullable(repositoryEntity.ReleaseVersion)
);
if (releaseInfoResult.IsError)
return releaseInfoResult.Errors;
Optional languageInfo = Optional.None();
if (repositoryEntity.LanguageCode is not null)
{
ErrorOr languageInfoResult = LanguageInfo.Create(
repositoryEntity.LanguageCode,
repositoryEntity.LanguageName,
Optional.FromNullable(repositoryEntity.LanguageNativeName)
);
if (languageInfoResult.IsError)
return languageInfoResult.Errors;
languageInfo = languageInfoResult.Value;
}
Optional originalLanguageCode = Optional.None();
if (repositoryEntity.OriginalLanguageCode is not null)
{
ErrorOr originalLanguageInfoResult = LanguageInfo.Create(
repositoryEntity.OriginalLanguageCode,
repositoryEntity.OriginalLanguageName!,
Optional.FromNullable(repositoryEntity.OriginalLanguageNativeName)
);
if (originalLanguageInfoResult.IsError)
return originalLanguageInfoResult.Errors;
originalLanguageCode = originalLanguageInfoResult.Value;
}
ErrorOr writtenContentMetadataResult = WrittenContentMetadata.Create(
repositoryEntity.Title,
Optional.FromNullable(repositoryEntity.OriginalTitle),
Optional.FromNullable(repositoryEntity.Description),
releaseInfoResult.Value,
genresResult.Select(genre => genre.Value).ToList(),
tagsResult.Select(tag => tag.Value).ToList(),
languageInfo,
originalLanguageCode,
Optional.FromNullable(repositoryEntity.Publisher),
Optional.FromNullable(repositoryEntity.PageCount)
);
if (writtenContentMetadataResult.IsError)
return writtenContentMetadataResult.Errors;
Optional bookFormat = Optional.FromNullable(repositoryEntity.Format);
return Book.Create(
BookId.Create(repositoryEntity.Id),
writtenContentMetadataResult.Value,
bookFormat,
Optional.FromNullable(repositoryEntity.Edition),
Optional.FromNullable(repositoryEntity.VolumeNumber),
Optional.None(),
Optional.FromNullable(repositoryEntity.ASIN),
Optional.FromNullable(repositoryEntity.GoodreadsId),
Optional.FromNullable(repositoryEntity.LCCN),
Optional.FromNullable(repositoryEntity.OCLCNumber),
Optional.FromNullable(repositoryEntity.OpenLibraryId),
Optional.FromNullable(repositoryEntity.LibraryThingId),
Optional.FromNullable(repositoryEntity.GoogleBooksId),
Optional.FromNullable(repositoryEntity.BarnesAndNobleId),
Optional.FromNullable(repositoryEntity.AppleBooksId),
repositoryEntity.CreatedOnUtc,
Optional.FromNullable(repositoryEntity.UpdatedOnUtc),
isbnsResult.Select(isbn => isbn.Value).ToList(),
[],
bookRatingsResult.Select(bookRating => bookRating.Value).ToList());
}
///
/// Converts to a collection of .
///
/// The repository entities to be converted.
/// The converted domain entities.
///
/// An colection of containing either successfuly converted , or error messages.
///
public static IEnumerable> ToDomainEntities(this IEnumerable repositoryEntities)
{
return repositoryEntities.Select(repositoryEntity => repositoryEntity.ToDomainEntity());
}
///
/// Converts to .
///
/// The repository entity to be converted.
/// The converted response entity.
public static BookResponse ToResponse(this BookEntity repositoryEntity)
{
ReleaseInfoDto releaseInfoEntity = new(
repositoryEntity.OriginalReleaseDate,
repositoryEntity.OriginalReleaseYear,
repositoryEntity.ReReleaseDate,
repositoryEntity.ReReleaseYear,
repositoryEntity.ReleaseCountry,
repositoryEntity.ReleaseVersion
);
// language and original language make sense only if their subproperties have values
LanguageInfoDto? languageInfoEntity = !string.IsNullOrWhiteSpace(repositoryEntity.LanguageCode) ||
!string.IsNullOrWhiteSpace(repositoryEntity.LanguageName) ||
!string.IsNullOrWhiteSpace(repositoryEntity.LanguageNativeName)
? new LanguageInfoDto(
repositoryEntity.LanguageCode,
repositoryEntity.LanguageName,
repositoryEntity.LanguageNativeName
) : null;
LanguageInfoDto? originalLanguageInfoEntity = !string.IsNullOrWhiteSpace(repositoryEntity.OriginalLanguageCode) ||
!string.IsNullOrWhiteSpace(repositoryEntity.OriginalLanguageName) ||
!string.IsNullOrWhiteSpace(repositoryEntity.OriginalLanguageNativeName)
? new LanguageInfoDto(
repositoryEntity.OriginalLanguageCode,
repositoryEntity.OriginalLanguageName,
repositoryEntity.OriginalLanguageNativeName
) : null;
WrittenContentMetadataDto metadata = new(
repositoryEntity.Title,
repositoryEntity.OriginalTitle,
repositoryEntity.Description,
releaseInfoEntity,
[.. repositoryEntity.Genres.ToResponses()],
[.. repositoryEntity.Tags.ToResponses()],
languageInfoEntity,
originalLanguageInfoEntity,
repositoryEntity.Publisher,
repositoryEntity.PageCount
);
return new BookResponse(
repositoryEntity.Id,
metadata,
repositoryEntity.Format,
repositoryEntity.Edition,
repositoryEntity.VolumeNumber,
null,
repositoryEntity.ASIN,
repositoryEntity.GoodreadsId,
repositoryEntity.LCCN,
repositoryEntity.OCLCNumber,
repositoryEntity.OpenLibraryId,
repositoryEntity.LibraryThingId,
repositoryEntity.GoogleBooksId,
repositoryEntity.BarnesAndNobleId,
repositoryEntity.AppleBooksId,
[.. repositoryEntity.ISBNs.ToResponses()],
null,
[.. repositoryEntity.Ratings.ToResponses()],
repositoryEntity.CreatedOnUtc,
repositoryEntity.UpdatedOnUtc
);
}
///
/// Converts to a collection of .
///
/// The repository entities to be converted.
/// The converted reponses.
public static IEnumerable ToResponses(this IEnumerable repositoryEntities)
{
return repositoryEntities.Select(repositoryEntity => repositoryEntity.ToResponse());
}
}