mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 01:01:10 +00:00
Aligns the book metadata contract and consolidates metadata conversion and merging
This commit is contained in:
1 parent
590e49c9de
commit
e2d8e62908
43 files changed
+1076
-849
No files matched your search
@@ -0,0 +1,22 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Domain.Common.Primitives;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Common.Errors;
|
||||
|
||||
/// <summary>
|
||||
/// Application metadata related error types.
|
||||
/// </summary>
|
||||
public static partial class Errors
|
||||
{
|
||||
/// <summary>
|
||||
/// Error types for application metadata operations.
|
||||
/// </summary>
|
||||
public static class Metadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the error that reports that the metadata of a media item cannot be <see langword="null"/>.
|
||||
/// </summary>
|
||||
public static Error MetadataCannotBeNull => Error.Validation(description: nameof(MetadataCannotBeNull));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using Nemera.Contracts.DTO.Common;
|
||||
using Nemera.Domain.Common.Primitives;
|
||||
using Nemera.Domain.Common.ValueObjects.Metadata;
|
||||
using DomainErrors = Nemera.Domain.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Common.Mapping.Common.Metadata;
|
||||
@@ -20,9 +21,14 @@ public static class LanguageInfoDtoMapping
|
||||
/// </returns>
|
||||
public static Result<LanguageInfo> ToDomainValueObject(this LanguageInfoDto dto)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(dto.LanguageCode))
|
||||
return DomainErrors.Metadata.LanguageCodeCannotBeEmpty;
|
||||
if (string.IsNullOrWhiteSpace(dto.LanguageName))
|
||||
return DomainErrors.Metadata.LanguageNameCannotBeEmpty;
|
||||
|
||||
return LanguageInfo.Create(
|
||||
dto.LanguageCode!,
|
||||
dto.LanguageName!,
|
||||
dto.LanguageCode,
|
||||
dto.LanguageName,
|
||||
Optional<string>.FromNullable(dto.NativeName)
|
||||
);
|
||||
}
|
||||
|
||||
+5
-57
@@ -1,15 +1,15 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.Common;
|
||||
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Nemera.Domain.Common.Primitives;
|
||||
using Nemera.Domain.Common.ValueObjects.Metadata;
|
||||
using Nemera.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Nemera.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
|
||||
using Nemera.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Nemera.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using Nemera.Domain.SharedKernel.Common.Enums.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
@@ -27,62 +27,10 @@ public static class BookMetadataDtoMapping
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public static Result<Success> ApplyMetadata(this Book book, BookMetadataDto metadata)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(metadata.Title))
|
||||
return Domain.Common.Errors.Errors.Metadata.TitleCannotBeEmpty;
|
||||
if (metadata.ReleaseInfo is null)
|
||||
return Domain.Common.Errors.Errors.Metadata.ReleaseInfoCannotBeNull;
|
||||
if (metadata.Metadata is null)
|
||||
return ApplicationErrors.Metadata.MetadataCannotBeNull;
|
||||
|
||||
Result<ReleaseInfo> releaseInfoResult = ReleaseInfo.Create(
|
||||
Optional<DateOnly>.FromNullable(metadata.ReleaseInfo.OriginalReleaseDate),
|
||||
Optional<int>.FromNullable(metadata.ReleaseInfo.OriginalReleaseYear),
|
||||
Optional<DateOnly>.FromNullable(metadata.ReleaseInfo.ReReleaseDate),
|
||||
Optional<int>.FromNullable(metadata.ReleaseInfo.ReReleaseYear),
|
||||
Optional<ReleaseCountry>.FromNullable(metadata.ReleaseInfo.ReleaseCountry),
|
||||
Optional<string>.FromNullable(metadata.ReleaseInfo.ReleaseVersion)
|
||||
);
|
||||
if (releaseInfoResult.IsFailure)
|
||||
return releaseInfoResult.Errors;
|
||||
|
||||
List<Result<Genre>> domainGenresResult = metadata.Genres is not null
|
||||
? [.. metadata.Genres.Select(genre => Genre.Create(genre.Name!))]
|
||||
: [];
|
||||
List<Error> genresErrors = [.. domainGenresResult.Where(genreResult => genreResult.IsFailure).SelectMany(genreResult => genreResult.Errors)];
|
||||
if (genresErrors.Count != 0)
|
||||
return genresErrors;
|
||||
|
||||
List<Result<Tag>> domainTagsResult = metadata.Tags is not null
|
||||
? [.. metadata.Tags.Select(tag => Tag.Create(tag.Name!))]
|
||||
: [];
|
||||
List<Error> tagsErrors = [.. domainTagsResult.Where(tagResult => tagResult.IsFailure).SelectMany(tagResult => tagResult.Errors)];
|
||||
if (tagsErrors.Count != 0)
|
||||
return tagsErrors;
|
||||
|
||||
Optional<LanguageInfo> languageInfo = Optional<LanguageInfo>.None();
|
||||
if (metadata.Language is not null && metadata.Language.LanguageCode is not null && metadata.Language.LanguageName is not null)
|
||||
languageInfo = LanguageInfo.Create(
|
||||
metadata.Language.LanguageCode,
|
||||
metadata.Language.LanguageName,
|
||||
Optional<string>.FromNullable(metadata.Language.NativeName));
|
||||
|
||||
Optional<LanguageInfo> originalLanguageInfo = Optional<LanguageInfo>.None();
|
||||
if (metadata.OriginalLanguage is not null && metadata.OriginalLanguage.LanguageCode is not null && metadata.OriginalLanguage.LanguageName is not null)
|
||||
originalLanguageInfo = LanguageInfo.Create(
|
||||
metadata.OriginalLanguage.LanguageCode,
|
||||
metadata.OriginalLanguage.LanguageName,
|
||||
Optional<string>.FromNullable(metadata.OriginalLanguage.NativeName));
|
||||
|
||||
Result<WrittenContentMetadata> writtenContentMetadataResult = WrittenContentMetadata.Create(
|
||||
metadata.Title,
|
||||
Optional<string>.FromNullable(metadata.OriginalTitle),
|
||||
Optional<string>.FromNullable(metadata.Description),
|
||||
releaseInfoResult.Value,
|
||||
[.. domainGenresResult.Select(genreResult => genreResult.Value)],
|
||||
[.. domainTagsResult.Select(tagResult => tagResult.Value)],
|
||||
languageInfo,
|
||||
originalLanguageInfo,
|
||||
Optional<string>.FromNullable(metadata.Publisher),
|
||||
Optional<int>.FromNullable(metadata.PageCount)
|
||||
);
|
||||
Result<WrittenContentMetadata> writtenContentMetadataResult = metadata.Metadata.ToDomainValueObject();
|
||||
if (writtenContentMetadataResult.IsFailure)
|
||||
return writtenContentMetadataResult.Errors;
|
||||
|
||||
|
||||
+15
-10
@@ -6,6 +6,7 @@ using Nemera.Domain.Common.ValueObjects.Metadata;
|
||||
using Nemera.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DomainErrors = Nemera.Domain.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.Common;
|
||||
@@ -16,7 +17,7 @@ namespace Nemera.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.C
|
||||
public static class WrittenContentMetadataDtoMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts <paramref name="dto"/> to <see cref="WrittenContentMetadataDto"/>.
|
||||
/// Converts <paramref name="dto"/> to <see cref="WrittenContentMetadata"/>. The collections and the optional fields that are missing are left empty, while a missing title or release information is reported as an error.
|
||||
/// </summary>
|
||||
/// <param name="dto">The DTO to be converted.</param>
|
||||
/// <returns>
|
||||
@@ -24,17 +25,21 @@ public static class WrittenContentMetadataDtoMapping
|
||||
/// </returns>
|
||||
public static Result<WrittenContentMetadata> ToDomainValueObject(this WrittenContentMetadataDto dto)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(dto.Title))
|
||||
return DomainErrors.Metadata.TitleCannotBeEmpty;
|
||||
if (dto.ReleaseInfo is null)
|
||||
return DomainErrors.Metadata.ReleaseInfoCannotBeNull;
|
||||
|
||||
Result<ReleaseInfo>? domainReleaseInfoResult = dto.ReleaseInfo!.ToDomainValueObject();
|
||||
if (domainReleaseInfoResult.Value.IsFailure)
|
||||
return domainReleaseInfoResult.Value.Errors;
|
||||
Result<ReleaseInfo> domainReleaseInfoResult = dto.ReleaseInfo.ToDomainValueObject();
|
||||
if (domainReleaseInfoResult.IsFailure)
|
||||
return domainReleaseInfoResult.Errors;
|
||||
|
||||
IEnumerable<Result<Genre>> domainGenresResult = dto.Genres!.ToDomainValueObjects();
|
||||
IEnumerable<Result<Genre>> domainGenresResult = dto.Genres?.ToDomainValueObjects() ?? [];
|
||||
List<Error> errors = [.. domainGenresResult.Where(genreResult => genreResult.IsFailure).SelectMany(genreResult => genreResult.Errors)];
|
||||
if (errors.Count > 0)
|
||||
return errors;
|
||||
|
||||
IEnumerable<Result<Tag>> domainTagsResult = dto.Tags!.ToDomainValueObjects();
|
||||
IEnumerable<Result<Tag>> domainTagsResult = dto.Tags?.ToDomainValueObjects() ?? [];
|
||||
errors = [.. domainTagsResult.Where(tagResult => tagResult.IsFailure).SelectMany(tagResult => tagResult.Errors)];
|
||||
if (errors.Count > 0)
|
||||
return errors;
|
||||
@@ -48,12 +53,12 @@ public static class WrittenContentMetadataDtoMapping
|
||||
return domainOriginalLanguageInfoResult.Value.Errors;
|
||||
|
||||
return WrittenContentMetadata.Create(
|
||||
dto.Title!,
|
||||
dto.Title,
|
||||
Optional<string>.FromNullable(dto.OriginalTitle),
|
||||
Optional<string>.FromNullable(dto.Description),
|
||||
domainReleaseInfoResult!.Value.Value,
|
||||
[.. domainGenresResult.Select(genre => genre.Value)],
|
||||
[.. domainTagsResult.Select(tag => tag.Value)],
|
||||
domainReleaseInfoResult.Value,
|
||||
[.. domainGenresResult.Select(genreResult => genreResult.Value)],
|
||||
[.. domainTagsResult.Select(tagResult => tagResult.Value)],
|
||||
Optional<LanguageInfo>.FromNullable(domainLanguageInfoResult?.Value),
|
||||
Optional<LanguageInfo>.FromNullable(domainOriginalLanguageInfoResult?.Value),
|
||||
Optional<string>.FromNullable(dto.Publisher),
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.Commands.AddAlbum;
|
||||
@@ -41,7 +42,7 @@ public class AddAlbumCommandValidator : AbstractValidator<AddAlbumCommand>
|
||||
// Validates the metadata of the album: title, lengths, release types and status, disc and track counts, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
@@ -303,7 +304,7 @@ public class AddAlbumCommandValidator : AbstractValidator<AddAlbumCommand>
|
||||
// Validates the audio metadata of the track: title, lengths, release information, languages, genres and tags.
|
||||
track.RuleFor(t => t!.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(trackMetadata =>
|
||||
{
|
||||
trackMetadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ using Nemera.Application.Common.Infrastructure.Validation;
|
||||
using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.Commands.UpdateAlbum;
|
||||
@@ -50,7 +51,7 @@ public class UpdateAlbumCommandValidator : AbstractValidator<UpdateAlbumCommand>
|
||||
// Validates the metadata of the album: title, lengths, release types and status, disc and track counts, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
|
||||
+4
-3
@@ -3,6 +3,7 @@ using Nemera.Application.Common.Infrastructure.Validation;
|
||||
using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Commands.AddArtist;
|
||||
@@ -30,7 +31,7 @@ public class AddArtistCommandValidator : AbstractValidator<AddArtistCommand>
|
||||
// Validates the identifying details of the artist.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Name)
|
||||
@@ -213,7 +214,7 @@ public class AddArtistCommandValidator : AbstractValidator<AddArtistCommand>
|
||||
// Validates the metadata of the album: title, lengths, release types and status, and disc and track counts.
|
||||
album.RuleFor(a => a!.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
@@ -476,7 +477,7 @@ public class AddArtistCommandValidator : AbstractValidator<AddArtistCommand>
|
||||
// Validates the audio metadata of the track: title, lengths, release information, languages, genres and tags.
|
||||
track.RuleFor(t => t!.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(trackMetadata =>
|
||||
{
|
||||
trackMetadata.RuleFor(m => m!.Title)
|
||||
|
||||
+4
-3
@@ -3,6 +3,7 @@ using Nemera.Application.Common.Infrastructure.Validation;
|
||||
using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Commands.UpdateArtist;
|
||||
@@ -40,7 +41,7 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
|
||||
// Validates the identifying details of the artist.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Name)
|
||||
@@ -229,7 +230,7 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
|
||||
// Validates the metadata of the album: title, lengths, release types and status, and disc and track counts.
|
||||
album.RuleFor(a => a!.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
@@ -498,7 +499,7 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
|
||||
// Validates the audio metadata of the track: title, lengths, release information, languages, genres and tags.
|
||||
track.RuleFor(t => t!.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(trackMetadata =>
|
||||
{
|
||||
trackMetadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ using Nemera.Application.Common.Infrastructure.Validation;
|
||||
using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Tracks.Commands.AddTrack;
|
||||
@@ -56,7 +57,7 @@ public class AddTrackCommandValidator : AbstractValidator<AddTrackCommand>
|
||||
// Validates the audio metadata of the track: title, lengths, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(trackMetadata =>
|
||||
{
|
||||
trackMetadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ using Nemera.Application.Common.Infrastructure.Validation;
|
||||
using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Tracks.Commands.UpdateTrack;
|
||||
@@ -60,7 +61,7 @@ public class UpdateTrackCommandValidator : AbstractValidator<UpdateTrackCommand>
|
||||
// Validates the audio metadata of the track: title, lengths, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(trackMetadata =>
|
||||
{
|
||||
trackMetadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using Nemera.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.AddBook;
|
||||
@@ -38,7 +39,7 @@ public class AddBookCommandValidator : AbstractValidator<AddBookCommand>
|
||||
// Validates the metadata of the book: title, lengths, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ using Nemera.Application.Common.Utilities;
|
||||
using Nemera.Domain.Common.Errors;
|
||||
using Nemera.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using ApplicationErrors = Nemera.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
@@ -41,7 +42,7 @@ public class UpdateBookCommandValidator : AbstractValidator<UpdateBookCommand>
|
||||
// Validates the metadata of the book: title, lengths, release information, languages, genres and tags.
|
||||
RuleFor(command => command.Metadata)
|
||||
.NotNull()
|
||||
.WithError(Errors.Metadata.MetadataCannotBeNull)
|
||||
.WithError(ApplicationErrors.Metadata.MetadataCannotBeNull)
|
||||
.ChildRules(metadata =>
|
||||
{
|
||||
metadata.RuleFor(m => m!.Title)
|
||||
|
||||
+2
-20
@@ -10,16 +10,7 @@ namespace Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
/// <summary>
|
||||
/// Data transfer object for the metadata of a book returned by a metadata provider.
|
||||
/// </summary>
|
||||
/// <param name="Title">The title of the book.</param>
|
||||
/// <param name="OriginalTitle">The original title of the book, if different from the current title.</param>
|
||||
/// <param name="Description">A brief description or summary of the book.</param>
|
||||
/// <param name="ReleaseInfo">The release information, including release date and other relevant details.</param>
|
||||
/// <param name="Genres">The list of genres associated with the book.</param>
|
||||
/// <param name="Tags">The list of tags that further describe or categorize the book.</param>
|
||||
/// <param name="Language">The language in which the book is written.</param>
|
||||
/// <param name="OriginalLanguage">The original language of the book, if it has been translated.</param>
|
||||
/// <param name="Publisher">The name of the publisher of the book.</param>
|
||||
/// <param name="PageCount">The number of pages in the book.</param>
|
||||
/// <param name="Metadata">Written content metadata of the book.</param>
|
||||
/// <param name="Format">The format of the book (e.g., Hardcover, Paperback), if applicable.</param>
|
||||
/// <param name="Edition">The edition of the book, if applicable.</param>
|
||||
/// <param name="VolumeNumber">The volume or book number in the series, if applicable.</param>
|
||||
@@ -38,16 +29,7 @@ namespace Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
/// <param name="Ratings">The list of ratings for the book.</param>
|
||||
/// <param name="CoverImagePath">The path of the cover image of the book, if applicable.</param>
|
||||
public sealed record BookMetadataDto(
|
||||
string? Title,
|
||||
string? OriginalTitle,
|
||||
string? Description,
|
||||
ReleaseInfoDto? ReleaseInfo,
|
||||
List<GenreDto>? Genres,
|
||||
List<TagDto>? Tags,
|
||||
LanguageInfoDto? Language,
|
||||
LanguageInfoDto? OriginalLanguage,
|
||||
string? Publisher,
|
||||
int? PageCount,
|
||||
WrittenContentMetadataDto? Metadata,
|
||||
BookFormat? Format,
|
||||
string? Edition,
|
||||
float? VolumeNumber,
|
||||
|
||||
@@ -14,11 +14,6 @@ public static partial class Errors
|
||||
/// </summary>
|
||||
public static class Metadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the error that reports that the metadata of a media item cannot be <see langword="null"/>.
|
||||
/// </summary>
|
||||
public static Error MetadataCannotBeNull => Error.Validation(description: nameof(MetadataCannotBeNull));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the error that reports that the title of a media item cannot be empty.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Contracts.DTO.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Infrastructure.Common.Metadata;
|
||||
|
||||
/// <summary>
|
||||
/// Provides the shared field-level merge operations used when aggregating the metadata returned by multiple metadata providers.
|
||||
/// </summary>
|
||||
internal static class MetadataMerge
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the first non-empty value, otherwise the second value unchanged.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the values.</typeparam>
|
||||
/// <param name="first">The value with priority.</param>
|
||||
/// <param name="second">The fallback value.</param>
|
||||
/// <returns>The first value when it is not empty, otherwise the second value unchanged.</returns>
|
||||
public static T? First<T>(T? first, T? second)
|
||||
{
|
||||
return first is not null && !IsEmpty(first) ? first : second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the union of the two collections, de-duplicated by the <paramref name="keySelector"/> key, keeping the items of the first collection on key conflicts.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the collection items.</typeparam>
|
||||
/// <param name="first">The first collection, whose items have priority.</param>
|
||||
/// <param name="second">The second collection.</param>
|
||||
/// <param name="keySelector">The function used to extract the de-duplication key of an item.</param>
|
||||
/// <returns>The union of the two collections, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
public static List<T>? Union<T>(List<T>? first, List<T>? second, Func<T, string?> keySelector)
|
||||
where T : class
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
Dictionary<string, T> seen = new(StringComparer.OrdinalIgnoreCase);
|
||||
List<T> result = [];
|
||||
|
||||
void AddRange(List<T>? items)
|
||||
{
|
||||
foreach (T item in items ?? [])
|
||||
if (seen.TryAdd(keySelector(item) ?? string.Empty, item))
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
AddRange(first);
|
||||
AddRange(second);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merges the release information of the two metadata, keeping the first non-null value of each field.
|
||||
/// </summary>
|
||||
/// <param name="first">The first release information, whose values have priority.</param>
|
||||
/// <param name="second">The second release information, filling the fields that the first one lacks.</param>
|
||||
/// <returns>The merged release information, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
public static ReleaseInfoDto? MergeReleaseInfo(ReleaseInfoDto? first, ReleaseInfoDto? second)
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
DateOnly? originalReleaseDate = First(first?.OriginalReleaseDate, second?.OriginalReleaseDate);
|
||||
int? originalReleaseYear = First(first?.OriginalReleaseYear, second?.OriginalReleaseYear);
|
||||
DateOnly? reReleaseDate = First(first?.ReReleaseDate, second?.ReReleaseDate);
|
||||
int? reReleaseYear = First(first?.ReReleaseYear, second?.ReReleaseYear);
|
||||
|
||||
// A release date and its year describe the same point in time, so the year is derived from the date whenever a date is known. Without this,
|
||||
// a year from one provider could be combined with a date from another (for example a release year from the tags with the original release
|
||||
// date from MusicBrainz), and the resulting inconsistent pair would be rejected when the metadata is applied.
|
||||
if (originalReleaseDate is not null)
|
||||
originalReleaseYear = originalReleaseDate.Value.Year;
|
||||
if (reReleaseDate is not null)
|
||||
reReleaseYear = reReleaseDate.Value.Year;
|
||||
|
||||
return new ReleaseInfoDto(
|
||||
originalReleaseDate,
|
||||
originalReleaseYear,
|
||||
reReleaseDate,
|
||||
reReleaseYear,
|
||||
First(first?.ReleaseCountry, second?.ReleaseCountry),
|
||||
First(first?.ReleaseVersion, second?.ReleaseVersion));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the provided value is an empty string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value.</typeparam>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns><see langword="true"/> when the value is an empty string, otherwise <see langword="false"/>.</returns>
|
||||
private static bool IsEmpty<T>(T? value)
|
||||
{
|
||||
return value is string text && string.IsNullOrWhiteSpace(text);
|
||||
}
|
||||
}
|
||||
+72
-159
@@ -1,8 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Contracts.DTO.Common;
|
||||
using Nemera.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Nemera.Infrastructure.Common.Metadata;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Infrastructure.Core.MediaLibrary.AudioLibrary.MusicLibrary.Metadata;
|
||||
@@ -21,27 +19,27 @@ internal static class MusicMetadataAggregator
|
||||
public static ArtistMetadataDto Merge(ArtistMetadataDto first, ArtistMetadataDto second)
|
||||
{
|
||||
return new ArtistMetadataDto(
|
||||
First(first.Name, second.Name),
|
||||
First(first.SortName, second.SortName),
|
||||
First(first.Disambiguation, second.Disambiguation),
|
||||
First(first.Type, second.Type),
|
||||
First(first.Gender, second.Gender),
|
||||
First(first.Country, second.Country),
|
||||
First(first.Area, second.Area),
|
||||
First(first.BeginArea, second.BeginArea),
|
||||
First(first.EndArea, second.EndArea),
|
||||
First(first.LifeSpanBegin, second.LifeSpanBegin),
|
||||
First(first.LifeSpanEnd, second.LifeSpanEnd),
|
||||
MetadataMerge.First(first.Name, second.Name),
|
||||
MetadataMerge.First(first.SortName, second.SortName),
|
||||
MetadataMerge.First(first.Disambiguation, second.Disambiguation),
|
||||
MetadataMerge.First(first.Type, second.Type),
|
||||
MetadataMerge.First(first.Gender, second.Gender),
|
||||
MetadataMerge.First(first.Country, second.Country),
|
||||
MetadataMerge.First(first.Area, second.Area),
|
||||
MetadataMerge.First(first.BeginArea, second.BeginArea),
|
||||
MetadataMerge.First(first.EndArea, second.EndArea),
|
||||
MetadataMerge.First(first.LifeSpanBegin, second.LifeSpanBegin),
|
||||
MetadataMerge.First(first.LifeSpanEnd, second.LifeSpanEnd),
|
||||
first.IsEnded || second.IsEnded,
|
||||
First(first.Website, second.Website),
|
||||
First(first.MusicBrainzArtistId, second.MusicBrainzArtistId),
|
||||
Union(first.Ipis, second.Ipis, ipi => ipi),
|
||||
Union(first.Isnis, second.Isnis, isni => isni),
|
||||
Union(first.Aliases, second.Aliases, alias => alias.Name),
|
||||
Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"));
|
||||
MetadataMerge.First(first.Website, second.Website),
|
||||
MetadataMerge.First(first.MusicBrainzArtistId, second.MusicBrainzArtistId),
|
||||
MetadataMerge.Union(first.Ipis, second.Ipis, ipi => ipi),
|
||||
MetadataMerge.Union(first.Isnis, second.Isnis, isni => isni),
|
||||
MetadataMerge.Union(first.Aliases, second.Aliases, alias => alias.Name),
|
||||
MetadataMerge.Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
MetadataMerge.Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
MetadataMerge.Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
MetadataMerge.Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -53,32 +51,32 @@ internal static class MusicMetadataAggregator
|
||||
public static AlbumMetadataDto Merge(AlbumMetadataDto first, AlbumMetadataDto second)
|
||||
{
|
||||
return new AlbumMetadataDto(
|
||||
First(first.Title, second.Title),
|
||||
First(first.OriginalTitle, second.OriginalTitle),
|
||||
First(first.Description, second.Description),
|
||||
First(first.Disambiguation, second.Disambiguation),
|
||||
MergeReleaseInfo(first.ReleaseInfo, second.ReleaseInfo),
|
||||
First(first.Language, second.Language),
|
||||
First(first.OriginalLanguage, second.OriginalLanguage),
|
||||
Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
First(first.Script, second.Script),
|
||||
MetadataMerge.First(first.Title, second.Title),
|
||||
MetadataMerge.First(first.OriginalTitle, second.OriginalTitle),
|
||||
MetadataMerge.First(first.Description, second.Description),
|
||||
MetadataMerge.First(first.Disambiguation, second.Disambiguation),
|
||||
MetadataMerge.MergeReleaseInfo(first.ReleaseInfo, second.ReleaseInfo),
|
||||
MetadataMerge.First(first.Language, second.Language),
|
||||
MetadataMerge.First(first.OriginalLanguage, second.OriginalLanguage),
|
||||
MetadataMerge.Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
MetadataMerge.Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
MetadataMerge.First(first.Script, second.Script),
|
||||
first.ReleaseTypes is { Count: > 0 } ? first.ReleaseTypes : second.ReleaseTypes,
|
||||
First(first.ReleaseStatus, second.ReleaseStatus),
|
||||
First(first.MediaFormat, second.MediaFormat),
|
||||
First(first.Packaging, second.Packaging),
|
||||
First(first.TotalDiscs, second.TotalDiscs),
|
||||
First(first.TotalTracks, second.TotalTracks),
|
||||
First(first.Barcode, second.Barcode),
|
||||
Union(first.CatalogNumbers, second.CatalogNumbers, catalogNumber => catalogNumber),
|
||||
First(first.Label, second.Label),
|
||||
First(first.ASIN, second.ASIN),
|
||||
First(first.MusicBrainzReleaseId, second.MusicBrainzReleaseId),
|
||||
First(first.MusicBrainzReleaseGroupId, second.MusicBrainzReleaseGroupId),
|
||||
First(first.MusicBrainzReleaseArtistId, second.MusicBrainzReleaseArtistId),
|
||||
Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
First(first.ReleaseTitle, second.ReleaseTitle));
|
||||
MetadataMerge.First(first.ReleaseStatus, second.ReleaseStatus),
|
||||
MetadataMerge.First(first.MediaFormat, second.MediaFormat),
|
||||
MetadataMerge.First(first.Packaging, second.Packaging),
|
||||
MetadataMerge.First(first.TotalDiscs, second.TotalDiscs),
|
||||
MetadataMerge.First(first.TotalTracks, second.TotalTracks),
|
||||
MetadataMerge.First(first.Barcode, second.Barcode),
|
||||
MetadataMerge.Union(first.CatalogNumbers, second.CatalogNumbers, catalogNumber => catalogNumber),
|
||||
MetadataMerge.First(first.Label, second.Label),
|
||||
MetadataMerge.First(first.ASIN, second.ASIN),
|
||||
MetadataMerge.First(first.MusicBrainzReleaseId, second.MusicBrainzReleaseId),
|
||||
MetadataMerge.First(first.MusicBrainzReleaseGroupId, second.MusicBrainzReleaseGroupId),
|
||||
MetadataMerge.First(first.MusicBrainzReleaseArtistId, second.MusicBrainzReleaseArtistId),
|
||||
MetadataMerge.Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
MetadataMerge.Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
MetadataMerge.First(first.ReleaseTitle, second.ReleaseTitle));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,117 +88,32 @@ internal static class MusicMetadataAggregator
|
||||
public static AudioMetadataDto Merge(AudioMetadataDto first, AudioMetadataDto second)
|
||||
{
|
||||
return new AudioMetadataDto(
|
||||
First(first.Title, second.Title),
|
||||
First(first.OriginalTitle, second.OriginalTitle),
|
||||
First(first.Description, second.Description),
|
||||
First(first.Disambiguation, second.Disambiguation),
|
||||
MergeReleaseInfo(first.ReleaseInfo, second.ReleaseInfo),
|
||||
First(first.Language, second.Language),
|
||||
First(first.OriginalLanguage, second.OriginalLanguage),
|
||||
Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
First(first.Script, second.Script),
|
||||
First(first.Key, second.Key),
|
||||
First(first.Bpm, second.Bpm),
|
||||
MetadataMerge.First(first.Title, second.Title),
|
||||
MetadataMerge.First(first.OriginalTitle, second.OriginalTitle),
|
||||
MetadataMerge.First(first.Description, second.Description),
|
||||
MetadataMerge.First(first.Disambiguation, second.Disambiguation),
|
||||
MetadataMerge.MergeReleaseInfo(first.ReleaseInfo, second.ReleaseInfo),
|
||||
MetadataMerge.First(first.Language, second.Language),
|
||||
MetadataMerge.First(first.OriginalLanguage, second.OriginalLanguage),
|
||||
MetadataMerge.Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
MetadataMerge.Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
MetadataMerge.First(first.Script, second.Script),
|
||||
MetadataMerge.First(first.Key, second.Key),
|
||||
MetadataMerge.First(first.Bpm, second.Bpm),
|
||||
first.IsVideo || second.IsVideo,
|
||||
First(first.Work, second.Work),
|
||||
Union(first.Isrcs, second.Isrcs, isrc => isrc.Value),
|
||||
Union(first.Moods, second.Moods, mood => mood.Name),
|
||||
First(first.DurationInSeconds, second.DurationInSeconds),
|
||||
First(first.SampleRate, second.SampleRate),
|
||||
First(first.Channels, second.Channels),
|
||||
First(first.BitDepth, second.BitDepth),
|
||||
First(first.AudioCodec, second.AudioCodec),
|
||||
First(first.Container, second.Container),
|
||||
First(first.Bitrate, second.Bitrate),
|
||||
First(first.MusicBrainzRecordingId, second.MusicBrainzRecordingId),
|
||||
First(first.MusicBrainzTrackId, second.MusicBrainzTrackId),
|
||||
Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merges the release information of the two metadata, keeping the first non-null value of each field.
|
||||
/// </summary>
|
||||
/// <param name="first">The first release information, whose values have priority.</param>
|
||||
/// <param name="second">The second release information, filling the fields that the first one lacks.</param>
|
||||
/// <returns>The merged release information, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static ReleaseInfoDto? MergeReleaseInfo(ReleaseInfoDto? first, ReleaseInfoDto? second)
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
DateOnly? originalReleaseDate = First(first?.OriginalReleaseDate, second?.OriginalReleaseDate);
|
||||
int? originalReleaseYear = First(first?.OriginalReleaseYear, second?.OriginalReleaseYear);
|
||||
DateOnly? reReleaseDate = First(first?.ReReleaseDate, second?.ReReleaseDate);
|
||||
int? reReleaseYear = First(first?.ReReleaseYear, second?.ReReleaseYear);
|
||||
|
||||
// A release date and its year describe the same point in time, so the year is derived from the date whenever a date is known. Without this,
|
||||
// a year from one provider could be combined with a date from another (for example a release year from the tags with the original release
|
||||
// date from MusicBrainz), and the resulting inconsistent pair would be rejected when the metadata is applied.
|
||||
if (originalReleaseDate is not null)
|
||||
originalReleaseYear = originalReleaseDate.Value.Year;
|
||||
if (reReleaseDate is not null)
|
||||
reReleaseYear = reReleaseDate.Value.Year;
|
||||
|
||||
return new ReleaseInfoDto(
|
||||
originalReleaseDate,
|
||||
originalReleaseYear,
|
||||
reReleaseDate,
|
||||
reReleaseYear,
|
||||
First(first?.ReleaseCountry, second?.ReleaseCountry),
|
||||
First(first?.ReleaseVersion, second?.ReleaseVersion));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first non-null value of the two values.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the values.</typeparam>
|
||||
/// <param name="first">The value with priority.</param>
|
||||
/// <param name="second">The fallback value.</param>
|
||||
/// <returns>The first non-null value, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static T? First<T>(T? first, T? second)
|
||||
{
|
||||
return first is not null && !IsEmpty(first) ? first : second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the provided value is an empty string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value.</typeparam>
|
||||
/// <param name="value">The value to check.</param>
|
||||
/// <returns><see langword="true"/> when the value is an empty string, otherwise <see langword="false"/>.</returns>
|
||||
private static bool IsEmpty<T>(T? value)
|
||||
{
|
||||
return value is string text && string.IsNullOrWhiteSpace(text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the union of the two collections, de-duplicated by the <paramref name="keySelector"/> key, keeping the items of the first collection on key conflicts.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the collection items.</typeparam>
|
||||
/// <param name="first">The first collection, whose items have priority.</param>
|
||||
/// <param name="second">The second collection.</param>
|
||||
/// <param name="keySelector">The function used to extract the de-duplication key of an item.</param>
|
||||
/// <returns>The union of the two collections, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static List<T>? Union<T>(List<T>? first, List<T>? second, Func<T, string?> keySelector)
|
||||
where T : class
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
Dictionary<string, T> seen = new(StringComparer.OrdinalIgnoreCase);
|
||||
List<T> result = [];
|
||||
|
||||
void AddRange(List<T>? items)
|
||||
{
|
||||
foreach (T item in items ?? [])
|
||||
if (seen.TryAdd(keySelector(item) ?? string.Empty, item))
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
AddRange(first);
|
||||
AddRange(second);
|
||||
return result;
|
||||
MetadataMerge.First(first.Work, second.Work),
|
||||
MetadataMerge.Union(first.Isrcs, second.Isrcs, isrc => isrc.Value),
|
||||
MetadataMerge.Union(first.Moods, second.Moods, mood => mood.Name),
|
||||
MetadataMerge.First(first.DurationInSeconds, second.DurationInSeconds),
|
||||
MetadataMerge.First(first.SampleRate, second.SampleRate),
|
||||
MetadataMerge.First(first.Channels, second.Channels),
|
||||
MetadataMerge.First(first.BitDepth, second.BitDepth),
|
||||
MetadataMerge.First(first.AudioCodec, second.AudioCodec),
|
||||
MetadataMerge.First(first.Container, second.Container),
|
||||
MetadataMerge.First(first.Bitrate, second.Bitrate),
|
||||
MetadataMerge.First(first.MusicBrainzRecordingId, second.MusicBrainzRecordingId),
|
||||
MetadataMerge.First(first.MusicBrainzTrackId, second.MusicBrainzTrackId),
|
||||
MetadataMerge.Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
MetadataMerge.Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()));
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -300,7 +300,7 @@ internal sealed class BooksMediaLibraryScanMetadataEnricher : IMediaLibraryScanM
|
||||
try
|
||||
{
|
||||
MetadataDto? metadataResult = await metadataProvider.GetMetadataAsync(bookMetadataLookup, cancellationToken).ConfigureAwait(false);
|
||||
if (metadataResult is not BookMetadataDto bookMetadata || string.IsNullOrWhiteSpace(bookMetadata.Title))
|
||||
if (metadataResult is not BookMetadataDto bookMetadata || string.IsNullOrWhiteSpace(bookMetadata.Metadata?.Title))
|
||||
continue;
|
||||
|
||||
Result<Success> applyMetadataResult = book.ApplyMetadata(bookMetadata);
|
||||
@@ -334,7 +334,7 @@ internal sealed class BooksMediaLibraryScanMetadataEnricher : IMediaLibraryScanM
|
||||
try
|
||||
{
|
||||
MetadataDto? metadataResult = await metadataProvider.GetMetadataAsync(bookMetadataLookup, cancellationToken).ConfigureAwait(false);
|
||||
if (metadataResult is not BookMetadataDto bookMetadata || string.IsNullOrWhiteSpace(bookMetadata.Title))
|
||||
if (metadataResult is not BookMetadataDto bookMetadata || string.IsNullOrWhiteSpace(bookMetadata.Metadata?.Title))
|
||||
continue;
|
||||
|
||||
mergedMetadata = mergedMetadata is null ? bookMetadata : MetadataAggregator.Merge(mergedMetadata, bookMetadata);
|
||||
@@ -365,7 +365,7 @@ internal sealed class BooksMediaLibraryScanMetadataEnricher : IMediaLibraryScanM
|
||||
{
|
||||
string? isbn = bookMetadataLookup.Isbn ?? bookMetadata.Isbns?.FirstOrDefault()?.Value;
|
||||
string? openLibraryId = bookMetadataLookup.OpenLibraryId ?? bookMetadata.OpenLibraryId;
|
||||
string? title = bookMetadataLookup.Title ?? bookMetadata.Title;
|
||||
string? title = bookMetadataLookup.Title ?? bookMetadata.Metadata?.Title;
|
||||
return bookMetadataLookup with
|
||||
{
|
||||
Isbn = isbn,
|
||||
|
||||
+36
-97
@@ -1,8 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Contracts.DTO.Common;
|
||||
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary;
|
||||
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Nemera.Infrastructure.Common.Metadata;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Metadata;
|
||||
@@ -21,107 +20,47 @@ internal static class MetadataAggregator
|
||||
public static BookMetadataDto Merge(BookMetadataDto first, BookMetadataDto second)
|
||||
{
|
||||
return new BookMetadataDto(
|
||||
FirstOrDefaultReference(first.Title, second.Title),
|
||||
FirstOrDefaultReference(first.OriginalTitle, second.OriginalTitle),
|
||||
FirstOrDefaultReference(first.Description, second.Description),
|
||||
MergeReleaseInfo(first.ReleaseInfo, second.ReleaseInfo),
|
||||
Union(first.Genres, second.Genres, genre => genre.Name),
|
||||
Union(first.Tags, second.Tags, tag => tag.Name),
|
||||
FirstOrDefaultReference(first.Language, second.Language),
|
||||
FirstOrDefaultReference(first.OriginalLanguage, second.OriginalLanguage),
|
||||
FirstOrDefaultReference(first.Publisher, second.Publisher),
|
||||
FirstOrDefaultValue(first.PageCount, second.PageCount),
|
||||
FirstOrDefaultValue(first.Format, second.Format),
|
||||
FirstOrDefaultReference(first.Edition, second.Edition),
|
||||
FirstOrDefaultValue(first.VolumeNumber, second.VolumeNumber),
|
||||
FirstOrDefaultReference(first.Series, second.Series),
|
||||
FirstOrDefaultReference(first.ASIN, second.ASIN),
|
||||
FirstOrDefaultReference(first.GoodreadsId, second.GoodreadsId),
|
||||
FirstOrDefaultReference(first.LCCN, second.LCCN),
|
||||
FirstOrDefaultReference(first.OCLCNumber, second.OCLCNumber),
|
||||
FirstOrDefaultReference(first.OpenLibraryId, second.OpenLibraryId),
|
||||
FirstOrDefaultReference(first.LibraryThingId, second.LibraryThingId),
|
||||
FirstOrDefaultReference(first.GoogleBooksId, second.GoogleBooksId),
|
||||
FirstOrDefaultReference(first.BarnesAndNobleId, second.BarnesAndNobleId),
|
||||
FirstOrDefaultReference(first.AppleBooksId, second.AppleBooksId),
|
||||
Union(first.Isbns, second.Isbns, isbn => isbn.Value),
|
||||
Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
FirstOrDefaultReference(first.CoverImagePath, second.CoverImagePath));
|
||||
MergeWrittenContentMetadata(first.Metadata, second.Metadata),
|
||||
MetadataMerge.First(first.Format, second.Format),
|
||||
MetadataMerge.First(first.Edition, second.Edition),
|
||||
MetadataMerge.First(first.VolumeNumber, second.VolumeNumber),
|
||||
MetadataMerge.First(first.Series, second.Series),
|
||||
MetadataMerge.First(first.ASIN, second.ASIN),
|
||||
MetadataMerge.First(first.GoodreadsId, second.GoodreadsId),
|
||||
MetadataMerge.First(first.LCCN, second.LCCN),
|
||||
MetadataMerge.First(first.OCLCNumber, second.OCLCNumber),
|
||||
MetadataMerge.First(first.OpenLibraryId, second.OpenLibraryId),
|
||||
MetadataMerge.First(first.LibraryThingId, second.LibraryThingId),
|
||||
MetadataMerge.First(first.GoogleBooksId, second.GoogleBooksId),
|
||||
MetadataMerge.First(first.BarnesAndNobleId, second.BarnesAndNobleId),
|
||||
MetadataMerge.First(first.AppleBooksId, second.AppleBooksId),
|
||||
MetadataMerge.Union(first.Isbns, second.Isbns, isbn => isbn.Value),
|
||||
MetadataMerge.Union(first.Contributors, second.Contributors, contributor => $"{contributor.Name?.DisplayName}|{contributor.Role}"),
|
||||
MetadataMerge.Union(first.Ratings, second.Ratings, rating => rating.Source?.ToString()),
|
||||
MetadataMerge.First(first.CoverImagePath, second.CoverImagePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merges the release information of the two metadata, keeping the first non-null value of each field.
|
||||
/// Merges the written content metadata of the two metadata, keeping the first non-empty value of each field, and the union of the collection fields.
|
||||
/// </summary>
|
||||
/// <param name="first">The first release information, whose values have priority.</param>
|
||||
/// <param name="second">The second release information, filling the fields that the first one lacks.</param>
|
||||
/// <returns>The merged release information, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static ReleaseInfoDto? MergeReleaseInfo(ReleaseInfoDto? first, ReleaseInfoDto? second)
|
||||
/// <param name="first">The first written content metadata, whose values have priority.</param>
|
||||
/// <param name="second">The second written content metadata, filling the fields that the first one lacks.</param>
|
||||
/// <returns>The merged written content metadata, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static WrittenContentMetadataDto? MergeWrittenContentMetadata(WrittenContentMetadataDto? first, WrittenContentMetadataDto? second)
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
return new ReleaseInfoDto(
|
||||
FirstOrDefaultValue(first?.OriginalReleaseDate, second?.OriginalReleaseDate),
|
||||
FirstOrDefaultValue(first?.OriginalReleaseYear, second?.OriginalReleaseYear),
|
||||
FirstOrDefaultValue(first?.ReReleaseDate, second?.ReReleaseDate),
|
||||
FirstOrDefaultValue(first?.ReReleaseYear, second?.ReReleaseYear),
|
||||
FirstOrDefaultValue(first?.ReleaseCountry, second?.ReleaseCountry),
|
||||
FirstOrDefaultReference(first?.ReleaseVersion, second?.ReleaseVersion));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first non-null value of the two reference values.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the reference values.</typeparam>
|
||||
/// <param name="first">The value with priority.</param>
|
||||
/// <param name="second">The fallback value.</param>
|
||||
/// <returns>The first non-null value, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static T? FirstOrDefaultReference<T>(T? first, T? second)
|
||||
where T : class
|
||||
{
|
||||
return first ?? second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first non-null value of the two nullable value types.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The underlying type of the nullable values.</typeparam>
|
||||
/// <param name="first">The value with priority.</param>
|
||||
/// <param name="second">The fallback value.</param>
|
||||
/// <returns>The first non-null value, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static T? FirstOrDefaultValue<T>(T? first, T? second)
|
||||
where T : struct
|
||||
{
|
||||
return first ?? second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the union of the two collections, de-duplicated by the <paramref name="keySelector"/> key, keeping the items of the first collection on key conflicts.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the collection items.</typeparam>
|
||||
/// <param name="first">The first collection, whose items have priority.</param>
|
||||
/// <param name="second">The second collection.</param>
|
||||
/// <param name="keySelector">The function used to extract the de-duplication key of an item.</param>
|
||||
/// <returns>The union of the two collections, or <see langword="null"/> when both are <see langword="null"/>.</returns>
|
||||
private static List<T>? Union<T>(List<T>? first, List<T>? second, Func<T, string?> keySelector)
|
||||
where T : class
|
||||
{
|
||||
if (first is null && second is null)
|
||||
return null;
|
||||
|
||||
Dictionary<string, T> seen = new(StringComparer.OrdinalIgnoreCase);
|
||||
List<T> result = [];
|
||||
|
||||
void AddRange(List<T>? items)
|
||||
{
|
||||
foreach (T item in items ?? [])
|
||||
if (seen.TryAdd(keySelector(item) ?? string.Empty, item))
|
||||
result.Add(item);
|
||||
}
|
||||
|
||||
AddRange(first);
|
||||
AddRange(second);
|
||||
return result;
|
||||
return new WrittenContentMetadataDto(
|
||||
MetadataMerge.First(first?.Title, second?.Title),
|
||||
MetadataMerge.First(first?.OriginalTitle, second?.OriginalTitle),
|
||||
MetadataMerge.First(first?.Description, second?.Description),
|
||||
MetadataMerge.MergeReleaseInfo(first?.ReleaseInfo, second?.ReleaseInfo),
|
||||
MetadataMerge.Union(first?.Genres, second?.Genres, genre => genre.Name),
|
||||
MetadataMerge.Union(first?.Tags, second?.Tags, tag => tag.Name),
|
||||
MetadataMerge.First(first?.Language, second?.Language),
|
||||
MetadataMerge.First(first?.OriginalLanguage, second?.OriginalLanguage),
|
||||
MetadataMerge.First(first?.Publisher, second?.Publisher),
|
||||
MetadataMerge.First(first?.PageCount, second?.PageCount));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user