Extends the music library listings with covers, sorting and alpha filtering.

This commit is contained in:
BinaryAlley committed 2026-10-13 13:26:14 +03:00
1 parent 0019821e87
commit 9d82f283e4
69 files changed
+3613 -268

No files matched your search

@@ -1,5 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Common.DTO.MediaLibrary.AudioLibrary.MusicLibrary.Albums;
@@ -23,4 +25,19 @@ public sealed record AlbumLiteRow
/// Gets the number of tracks of the release.
/// </summary>
public int TotalTracks { get; init; }
/// <summary>
/// Gets the path of the image representing the album, if available.
/// </summary>
public string? CoverPath { get; init; }
/// <summary>
/// Gets the release year of the album (the re-release year, if available, or the original release year), if known.
/// </summary>
public int? ReleaseYear { get; init; }
/// <summary>
/// Gets the set of types of the release.
/// </summary>
public List<MusicReleaseType>? ReleaseTypes { get; init; }
}
@@ -18,4 +18,9 @@ public sealed record ArtistLiteRow
/// Gets the name of the artist.
/// </summary>
public required string Name { get; init; }
/// <summary>
/// Gets the path of the image representing the artist, if available.
/// </summary>
public string? CoverPath { get; init; }
}
@@ -28,4 +28,9 @@ public sealed record TrackLiteRow
/// Gets the number of the disc the track belongs to, if applicable.
/// </summary>
public int? DiscNumber { get; init; }
/// <summary>
/// Gets the duration of the track in seconds.
/// </summary>
public int DurationInSeconds { get; init; }
}
@@ -157,8 +157,10 @@ public static class AlbumEntityMapping
/// Converts <paramref name="repositoryEntity"/> to <see cref="AlbumResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <param name="coverPath">The optional path of the image representing the album.</param>
/// <param name="artwork">The optional stored artwork of the album, in display order.</param>
/// <returns>The converted response entity.</returns>
public static AlbumResponse ToResponse(this AlbumEntity repositoryEntity)
public static AlbumResponse ToResponse(this AlbumEntity repositoryEntity, string? coverPath = null, IReadOnlyList<MediaArtworkDto>? artwork = null)
{
ReleaseInfoDto releaseInfo = new(
repositoryEntity.OriginalReleaseDate,
@@ -220,7 +222,9 @@ public static class AlbumEntityMapping
repositoryEntity.UpdatedOnUtc,
[.. repositoryEntity.Contributors.Select(contributor => new MediaContributorReferenceDto(contributor.MediaContributorId, contributor.Role))],
[.. repositoryEntity.Ratings.ToResponses()],
[.. repositoryEntity.Tracks.Select(track => track.ToResponse())]);
[.. repositoryEntity.Tracks.Select(track => track.ToResponse())],
coverPath,
artwork ?? []);
}
/// <summary>
@@ -24,7 +24,10 @@ public static class AlbumLiteRowMapping
return new AlbumLiteResponse(
readModel.Id,
readModel.Title,
readModel.TotalTracks);
readModel.TotalTracks,
readModel.CoverPath,
readModel.ReleaseYear,
readModel.ReleaseTypes);
}
/// <summary>
@@ -147,8 +147,9 @@ public static class ArtistEntityMapping
/// Converts <paramref name="repositoryEntity"/> to <see cref="ArtistResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <param name="coverPath">The optional path of the image representing the artist.</param>
/// <returns>The converted response entity.</returns>
public static ArtistResponse ToResponse(this ArtistEntity repositoryEntity)
public static ArtistResponse ToResponse(this ArtistEntity repositoryEntity, string? coverPath = null)
{
MusicArtistMetadataDto metadata = new(
repositoryEntity.Name,
@@ -178,7 +179,8 @@ public static class ArtistEntityMapping
repositoryEntity.UpdatedOnUtc,
[.. repositoryEntity.Contributors.Select(contributor => new MediaContributorReferenceDto(contributor.MediaContributorId, contributor.Role))],
[.. repositoryEntity.Ratings.ToResponses()],
[.. repositoryEntity.Albums.Select(album => album.ToResponse())]);
[.. repositoryEntity.Albums.Select(album => album.ToResponse())],
coverPath);
}
/// <summary>
@@ -23,7 +23,8 @@ public static class ArtistLiteRowMapping
{
return new ArtistLiteResponse(
readModel.Id,
readModel.Name);
readModel.Name,
readModel.CoverPath);
}
/// <summary>
@@ -30,6 +30,10 @@ public static class GetArtistsLiteRequestMapping
return new GetArtistsLiteQuery(
libraryId,
paginationData,
request.SearchTerm);
request.SearchTerm,
request.FilterAlphaKey,
request.ShouldIgnoreThePrefixForAlphaPicker,
request.SortBy,
request.SortOrder);
}
}
@@ -25,7 +25,8 @@ public static class TrackLiteRowMapping
readModel.Id,
readModel.Title,
readModel.TrackNumber,
readModel.DiscNumber);
readModel.DiscNumber,
readModel.DurationInSeconds);
}
/// <summary>
@@ -8,11 +8,15 @@ using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwn
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Albums;
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.Responses.MediaLibrary.AudioLibrary.MusicLibrary.Albums;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
@@ -88,6 +92,21 @@ public class GetAlbumQueryHandler : IQueryHandler<GetAlbumQuery, Result<AlbumRes
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
return existingAlbum.ToResponse();
// The artwork of an album is stored in the MusicArtwork table, not on the album row, so it is loaded to expose the cover
// and every other stored piece of artwork.
Result<IReadOnlyList<MusicArtworkEntity>> getArtworkResult = await _unitOfWork.MusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, cancellationToken).ConfigureAwait(false);
if (getArtworkResult.IsFailure)
return getArtworkResult.Errors;
string? coverPath = getArtworkResult.Value
.Where(artwork => artwork.ArtworkType == ArtworkType.Cover)
.Select(artwork => artwork.FileName)
.FirstOrDefault();
// Only the artwork whose file has already been stored can be presented, so the ones that are still pending resolution are left out.
List<MediaArtworkDto> artwork = [.. getArtworkResult.Value
.Where(storedArtwork => !string.IsNullOrWhiteSpace(storedArtwork.FileName))
.Select(storedArtwork => new MediaArtworkDto(storedArtwork.ArtworkType, storedArtwork.Ordinal, storedArtwork.FileName!))];
return existingAlbum.ToResponse(coverPath, artwork);
}
}
@@ -11,8 +11,11 @@ using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.A
using Lumina.Contracts.Responses.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
@@ -87,6 +90,15 @@ public class GetArtistQueryHandler : IQueryHandler<GetArtistQuery, Result<Artist
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
return existingArtist.ToResponse();
// The artwork of the artist is stored separately from the artist, so it is loaded to expose the cover of the artist in the response.
Result<IReadOnlyList<MusicArtworkEntity>> getArtworkResult = await _unitOfWork.MusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, cancellationToken).ConfigureAwait(false);
if (getArtworkResult.IsFailure)
return getArtworkResult.Errors;
string? coverPath = getArtworkResult.Value
.Where(artwork => artwork.ArtworkType == ArtworkType.Cover)
.Select(artwork => artwork.FileName)
.FirstOrDefault();
return existingArtist.ToResponse(coverPath);
}
}
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Domain.SharedKernel.Common.Enums.Common;
using System.Diagnostics;
#endregion
@@ -12,9 +13,17 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <param name="LibraryId">The unique identifier of the media library whose artists are retrieved, taken from the route.</param>
/// <param name="PaginationData">The object containing the requested pagination data.</param>
/// <param name="SearchTerm">The search term used to filter the artists by name.</param>
/// <param name="FilterAlphaKey">The alpha key used to filter the artists by the first character of their name.</param>
/// <param name="ShouldIgnoreThePrefixForAlphaPicker">Whether the leading "The " prefix of a name should be ignored when computing the alpha key, or not.</param>
/// <param name="SortBy">The name of the field by which to sort the results.</param>
/// <param name="SortOrder">The direction in which to sort the results.</param>
[DebuggerDisplay("LibraryId: {LibraryId}")]
public record GetArtistsLiteQuery(
string? LibraryId,
PaginationDataDto? PaginationData,
string? SearchTerm
string? SearchTerm,
string? FilterAlphaKey,
bool ShouldIgnoreThePrefixForAlphaPicker,
string? SortBy,
SortOrder? SortOrder
) : IQuery;
@@ -78,12 +78,16 @@ public class GetArtistsLiteQueryHandler : IQueryHandler<GetArtistsLiteQuery, Res
LibraryFilterDto filter = new()
{
LibraryId = libraryId,
SearchTerm = query.SearchTerm
SearchTerm = query.SearchTerm,
FilterAlphaKey = query.FilterAlphaKey,
ShouldIgnoreThePrefixForAlphaPicker = query.ShouldIgnoreThePrefixForAlphaPicker
};
Result<PaginatedResultDto<ArtistLiteRow>> getArtistsResult = await _unitOfWork.ArtistRepository.GetAllLiteAsync(
query.PaginationData,
filterModel: filter,
query.SortBy,
query.SortOrder,
filter,
cancellationToken: cancellationToken).ConfigureAwait(false);
return getArtistsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Utilities;
using Lumina.Domain.Common.Errors;
@@ -26,5 +27,14 @@ public class GetArtistsLiteQueryValidator : AbstractValidator<GetArtistsLiteQuer
.Must(libraryId => Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty)
.When(query => query.LibraryId is not null && query.LibraryId.Length > 0)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
// The alpha key must be exactly one of the three picker bucket kinds, so that the filter specification can
// derive the key of a name unambiguously: no filter, a number bucket, a symbol bucket, or a single ASCII letter.
RuleFor(query => query.FilterAlphaKey)
.Must(alphaKey => alphaKey is null
|| alphaKey == LibraryItemAlphaKeys.NUMBER
|| alphaKey == LibraryItemAlphaKeys.SYMBOL
|| (alphaKey.Length == 1 && char.IsAsciiLetter(alphaKey[0])))
.WithError(Errors.Library.InvalidFilterAlphaKey);
}
}