Refactors the artist slice across the stack

This commit is contained in:
BinaryAlley committed 2026-09-19 12:57:13 -07:00
1 parent c112c64c25
commit aa097ecc98
55 files changed
+3219 -278

No files matched your search

@@ -40,8 +40,9 @@ public static class ArtistMapping
Albums = [.. domainEntity.Albums.Select(album => album.ToRepositoryEntity(domainEntity.Id.Value, domainEntity.LibraryId.Value))],
CreatedOnUtc = domainEntity.CreatedOnUtc,
CreatedBy = Guid.Empty,
UpdatedOnUtc = domainEntity.UpdatedOnUtc.HasValue ? domainEntity.UpdatedOnUtc : null,
UpdatedBy = domainEntity.UpdatedOnUtc.HasValue ? Guid.NewGuid() : null
// The audit columns are owned by the auditing interceptor, which stamps only the rows that actually changed, so they are never mapped here.
UpdatedOnUtc = null,
UpdatedBy = null
};
}
}
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistAlbumsLite;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
#endregion
@@ -14,12 +15,22 @@ public static class GetArtistAlbumsLiteRequestMapping
/// Converts <paramref name="request"/> to <see cref="GetArtistAlbumsLiteQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="libraryId">The Id of the library the artist belongs to, taken from the route.</param>
/// <param name="artistId">The Id of the artist whose albums are retrieved, taken from the route.</param>
/// <returns>The converted query.</returns>
public static GetArtistAlbumsLiteQuery ToQuery(this GetArtistAlbumsLiteRequest request)
public static GetArtistAlbumsLiteQuery ToQuery(this GetArtistAlbumsLiteRequest request, string? libraryId, string? artistId)
{
PaginationDataDto? paginationData = null;
if (request.CurrentPage is not null || request.PerPage is not null)
paginationData = new PaginationDataDto
{
CurrentPage = request.CurrentPage ?? 1,
PerPage = request.PerPage ?? 200
};
return new GetArtistAlbumsLiteQuery(
request.ArtistId,
request.CurrentPage,
request.PerPage);
libraryId,
artistId,
paginationData);
}
}
@@ -1,23 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistAlbums;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
/// <summary>
/// Extension methods for converting <see cref="GetArtistAlbumsRequest"/>.
/// </summary>
public static class GetArtistAlbumsRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetArtistAlbumsQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetArtistAlbumsQuery ToQuery(this GetArtistAlbumsRequest request)
{
return new GetArtistAlbumsQuery(
request.ArtistId);
}
}
@@ -1,23 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtist;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
/// <summary>
/// Extension methods for converting <see cref="GetArtistRequest"/>.
/// </summary>
public static class GetArtistRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetArtistQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetArtistQuery ToQuery(this GetArtistRequest request)
{
return new GetArtistQuery(
request.ArtistId);
}
}
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistsLite;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
#endregion
@@ -14,13 +15,21 @@ public static class GetArtistsLiteRequestMapping
/// Converts <paramref name="request"/> to <see cref="GetArtistsLiteQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="libraryId">The Id of the library whose artists are retrieved, taken from the route.</param>
/// <returns>The converted query.</returns>
public static GetArtistsLiteQuery ToQuery(this GetArtistsLiteRequest request)
public static GetArtistsLiteQuery ToQuery(this GetArtistsLiteRequest request, string? libraryId)
{
PaginationDataDto? paginationData = null;
if (request.CurrentPage is not null || request.PerPage is not null)
paginationData = new PaginationDataDto
{
CurrentPage = request.CurrentPage ?? 1,
PerPage = request.PerPage ?? 200
};
return new GetArtistsLiteQuery(
request.LibraryId,
request.CurrentPage,
request.PerPage,
libraryId,
paginationData,
request.SearchTerm);
}
}
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtists;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
#endregion
@@ -14,13 +15,21 @@ public static class GetArtistsRequestMapping
/// Converts <paramref name="request"/> to <see cref="GetArtistsQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="libraryId">The Id of the library whose artists are retrieved, taken from the route.</param>
/// <returns>The converted query.</returns>
public static GetArtistsQuery ToQuery(this GetArtistsRequest request)
public static GetArtistsQuery ToQuery(this GetArtistsRequest request, string? libraryId)
{
PaginationDataDto? paginationData = null;
if (request.CurrentPage is not null || request.PerPage is not null)
paginationData = new PaginationDataDto
{
CurrentPage = request.CurrentPage ?? 1,
PerPage = request.PerPage ?? 200
};
return new GetArtistsQuery(
request.LibraryId,
request.CurrentPage,
request.PerPage,
libraryId,
paginationData,
request.SearchTerm);
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.Commands.AddAlbum;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
/// <summary>
/// Extension methods for converting <see cref="UpdateArtistAlbumRequest"/>.
/// </summary>
public static class UpdateArtistAlbumRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="AddAlbumCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="libraryId">The Id of the library the album belongs to, taken from the route.</param>
/// <param name="artistId">The Id of the artist the album belongs to, taken from the route.</param>
/// <returns>The converted command.</returns>
public static AddAlbumCommand ToCommand(this UpdateArtistAlbumRequest request, string? libraryId, string? artistId)
{
return new AddAlbumCommand(
libraryId,
artistId,
request.Metadata,
request.MediaFormat,
request.Barcode,
request.CatalogNumber,
request.MusicBrainzReleaseId,
request.MusicBrainzReleaseGroupId,
request.MusicBrainzReleaseArtistId,
request.Contributors,
request.Ratings,
request.Tracks?.ToCommands(libraryId, artistId, null).ToList(),
request.AlbumId);
}
/// <summary>
/// Converts <paramref name="requests"/> to a collection of <see cref="AddAlbumCommand"/>.
/// </summary>
/// <param name="requests">The requests to be converted.</param>
/// <param name="libraryId">The Id of the library the albums belong to, taken from the route.</param>
/// <param name="artistId">The Id of the artist the albums belong to, taken from the route.</param>
/// <returns>The converted commands.</returns>
public static IEnumerable<AddAlbumCommand> ToCommands(this IEnumerable<UpdateArtistAlbumRequest> requests, string? libraryId, string? artistId)
{
return requests.Select(request => request.ToCommand(libraryId, artistId));
}
}
@@ -32,13 +32,13 @@ public static class UpdateArtistCommandMapping
{
// Map the media contributors that make up the artist to their domain counterparts.
IEnumerable<Result<MusicMediaContributor>> domainContributorsResult = command.Contributors!.ToMusicDomainEntities();
List<Error> errors = [.. domainContributorsResult.Where(contributorResult => contributorResult.IsFailure).SelectMany(contributorResult => contributorResult.Errors) ?? []];
List<Error> errors = [.. domainContributorsResult.Where(contributorResult => contributorResult.IsFailure).SelectMany(contributorResult => contributorResult.Errors)];
if (errors.Count > 0)
return errors;
// Build each album of the artist, together with its metadata, contributors, ratings and tracks.
IEnumerable<Result<Album>> domainAlbumsResult = command.Albums!.ToDomainEntities();
errors = [.. domainAlbumsResult.Where(albumResult => albumResult.IsFailure).SelectMany(albumResult => albumResult.Errors) ?? []];
errors = [.. domainAlbumsResult.Where(albumResult => albumResult.IsFailure).SelectMany(albumResult => albumResult.Errors)];
if (errors.Count > 0)
return errors;
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Albums;
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Commands.UpdateArtist;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
using System.Linq;
@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Tracks.Commands.AddTrack;
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
/// <summary>
/// Extension methods for converting <see cref="UpdateArtistTrackRequest"/>.
/// </summary>
public static class UpdateArtistTrackRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="AddTrackCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="libraryId">The Id of the library the track belongs to, taken from the route.</param>
/// <param name="artistId">The Id of the artist the track belongs to, taken from the route.</param>
/// <param name="albumId">The Id of the album the track belongs to, taken from the route.</param>
/// <returns>The converted command.</returns>
public static AddTrackCommand ToCommand(this UpdateArtistTrackRequest request, string? libraryId, string? artistId, string? albumId)
{
return new AddTrackCommand(
libraryId,
artistId,
albumId,
request.Path,
request.Metadata,
request.TrackNumber,
request.DiscNumber,
request.Script,
request.Key,
request.Bpm,
request.Work,
request.MusicBrainzRecordingId,
request.MusicBrainzTrackId,
request.MusicBrainzWorkId,
request.Moods,
request.Isrcs,
request.Contributors,
request.Ratings,
request.TrackId);
}
/// <summary>
/// Converts <paramref name="requests"/> to a collection of <see cref="AddTrackCommand"/>.
/// </summary>
/// <param name="requests">The requests to be converted.</param>
/// <param name="libraryId">The Id of the library the tracks belong to, taken from the route.</param>
/// <param name="artistId">The Id of the artist the tracks belong to, taken from the route.</param>
/// <param name="albumId">The Id of the album the tracks belong to, taken from the route.</param>
/// <returns>The converted commands.</returns>
public static IEnumerable<AddTrackCommand> ToCommands(this IEnumerable<UpdateArtistTrackRequest> requests, string? libraryId, string? artistId, string? albumId)
{
return requests.Select(request => request.ToCommand(libraryId, artistId, albumId));
}
}
@@ -242,6 +242,16 @@ public class AddArtistCommandValidator : AbstractValidator<AddArtistCommand>
.When(a => a!.CatalogNumber is not null)
.WithError(Errors.Music.CatalogNumberMustBeMaximum50CharactersLong);
album.RuleFor(a => a!.Barcode)
.NotEmpty()
.When(a => a!.Barcode is not null)
.WithError(Errors.Music.BarcodeValueCannotBeEmpty);
album.RuleFor(a => a!.Barcode)
.Matches(@"^\d{12,13}$")
.When(a => a!.Barcode is not null && a!.Barcode.Length > 0)
.WithError(Errors.Music.InvalidFormatForBarcode);
// Validates the MusicBrainz identifiers of the album.
album.RuleFor(a => a!.MusicBrainzReleaseId)
.Must(musicBrainzReleaseId => musicBrainzReleaseId != Guid.Empty)
@@ -100,6 +100,6 @@ public class DeleteArtistCommandHandler : ICommandHandler<DeleteArtistCommand, R
if (saveChangesResult.IsFailure)
return saveChangesResult.Errors;
return deleteArtistResult;
return Result.Deleted;
}
}
@@ -79,8 +79,8 @@ public class UpdateArtistCommandHandler : ICommandHandler<UpdateArtistCommand, R
Guid libraryId = Guid.Parse(command.LibraryId!);
Guid artistId = Guid.Parse(command.ArtistId!);
// Get the existing artist with all its related data.
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, cancellationToken: cancellationToken).ConfigureAwait(false);
// Get the existing artist, only for its identity and creation metadata; the repository reloads and tracks the whole aggregate when it applies the update.
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (getArtistResult.IsFailure)
return getArtistResult.Errors;
if (getArtistResult.Value is null)
@@ -82,6 +82,12 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
RuleForEach(command => command.Albums)
.ChildRules(album =>
{
// Validates the identifier of the album, when the album already exists.
album.RuleFor(a => a!.AlbumId)
.Must(albumId => albumId != Guid.Empty)
.When(a => a!.AlbumId.HasValue)
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
// Validates the metadata of the album: title, lengths, release type and status, and disc and track counts.
album.RuleFor(a => a!.Metadata)
.NotNull()
@@ -252,6 +258,16 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
.When(a => a!.CatalogNumber is not null)
.WithError(Errors.Music.CatalogNumberMustBeMaximum50CharactersLong);
album.RuleFor(a => a!.Barcode)
.NotEmpty()
.When(a => a!.Barcode is not null)
.WithError(Errors.Music.BarcodeValueCannotBeEmpty);
album.RuleFor(a => a!.Barcode)
.Matches(@"^\d{12,13}$")
.When(a => a!.Barcode is not null && a!.Barcode.Length > 0)
.WithError(Errors.Music.InvalidFormatForBarcode);
// Validates the MusicBrainz identifiers of the album.
album.RuleFor(a => a!.MusicBrainzReleaseId)
.Must(musicBrainzReleaseId => musicBrainzReleaseId != Guid.Empty)
@@ -319,6 +335,12 @@ public class UpdateArtistCommandValidator : AbstractValidator<UpdateArtistComman
album.RuleForEach(a => a!.Tracks)
.ChildRules(track =>
{
// Validates the identifier of the track, when the track already exists.
track.RuleFor(t => t!.TrackId)
.Must(trackId => trackId != Guid.Empty)
.When(t => t!.TrackId.HasValue)
.WithError(Errors.Music.TrackIdCannotBeEmpty);
// Validates the file system path of the track.
track.RuleFor(t => t!.Path)
.NotEmpty()
@@ -9,8 +9,10 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <summary>
/// Query for getting an artist by its Id.
/// </summary>
/// <param name="ArtistId">The unique identifier of the artist to get.</param>
/// <param name="LibraryId">The unique identifier of the media library the artist belongs to, taken from the route.</param>
/// <param name="ArtistId">The unique identifier of the artist to get, taken from the route.</param>
[DebuggerDisplay("ArtistId: {ArtistId}")]
public record GetArtistQuery(
Guid ArtistId
string? LibraryId,
string? ArtistId
) : IQuery;
@@ -65,18 +65,28 @@ public class GetArtistQueryHandler : IQueryHandler<GetArtistQuery, Result<Artist
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// Admins can see all artists; for everyone else, only the artists of the libraries they own.
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(query.ArtistId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
// The validator guarantees that the route identifiers are non-empty Guids before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid artistId = Guid.Parse(query.ArtistId!);
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (getArtistResult.IsFailure)
return getArtistResult.Errors;
if (getArtistResult.Value is null)
return Errors.Music.ArtistNotFound;
ArtistEntity existingArtist = getArtistResult.Value;
// Resource scoping: the artist must belong to the library named by the route, so that an artist can never be read through another
// library's route; the mismatch is reported as not found, without disclosing that the artist exists in another library.
if (existingArtist.LibraryId != libraryId)
return Errors.Music.ArtistNotFound;
// Admins can see all artists; for everyone else, only the artists of the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(getArtistResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(existingArtist.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
return getArtistResult.Value.ToResponse();
return existingArtist.ToResponse();
}
}
@@ -17,10 +17,24 @@ public class GetArtistQueryValidator : AbstractValidator<GetArtistQuery>
/// </summary>
public GetArtistQueryValidator()
{
// Validates the identifier of the media library the artist belongs to, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.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);
// Validates the identifier of the artist to get, taken from the route.
RuleFor(query => query.ArtistId)
.NotEmpty()
.WithError(Errors.Music.ArtistIdCannotBeEmpty)
.Must(id => id != Guid.Empty)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
RuleFor(query => query.ArtistId)
.Must(artistId => Guid.TryParse(artistId, out Guid parsedArtistId) && parsedArtistId != Guid.Empty)
.When(query => query.ArtistId is not null && query.ArtistId.Length > 0)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
}
}
@@ -9,8 +9,10 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <summary>
/// Query for getting all the albums of an artist.
/// </summary>
/// <param name="ArtistId">The unique identifier of the artist whose albums are retrieved.</param>
/// <param name="LibraryId">The unique identifier of the media library the artist belongs to, taken from the route.</param>
/// <param name="ArtistId">The unique identifier of the artist whose albums are retrieved, taken from the route.</param>
[DebuggerDisplay("ArtistId: {ArtistId}")]
public record GetArtistAlbumsQuery(
Guid ArtistId
string? LibraryId,
string? ArtistId
) : IQuery;
@@ -65,19 +65,29 @@ public class GetArtistAlbumsQueryHandler : IQueryHandler<GetArtistAlbumsQuery, R
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(query.ArtistId, cancellationToken: cancellationToken).ConfigureAwait(false);
// The validator guarantees that the route identifiers are non-empty Guids before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid artistId = Guid.Parse(query.ArtistId!);
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (getArtistResult.IsFailure)
return getArtistResult.Errors;
if (getArtistResult.Value is null)
return Errors.Music.ArtistNotFound;
ArtistEntity existingArtist = getArtistResult.Value;
// Resource scoping: the artist must belong to the library named by the route, so that the albums of an artist can never be read through
// another library's route; the mismatch is reported as not found, without disclosing that the artist exists in another library.
if (existingArtist.LibraryId != libraryId)
return Errors.Music.ArtistNotFound;
// Admins can see the albums of the artists of all libraries; for everyone else, only of the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(getArtistResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(existingArtist.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
Result<IReadOnlyList<AlbumEntity>> getAlbumsResult = await _unitOfWork.AlbumRepository.GetByArtistIdAsync(query.ArtistId, cancellationToken).ConfigureAwait(false);
Result<IReadOnlyList<AlbumEntity>> getAlbumsResult = await _unitOfWork.AlbumRepository.GetByArtistIdAsync(artistId, cancellationToken).ConfigureAwait(false);
return getAlbumsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -17,10 +17,24 @@ public class GetArtistAlbumsQueryValidator : AbstractValidator<GetArtistAlbumsQu
/// </summary>
public GetArtistAlbumsQueryValidator()
{
// Validates the identifier of the media library the artist belongs to, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.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);
// Validates the identifier of the artist whose albums are retrieved, taken from the route.
RuleFor(query => query.ArtistId)
.NotEmpty()
.WithError(Errors.Music.ArtistIdCannotBeEmpty)
.Must(artistId => artistId != Guid.Empty)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
RuleFor(query => query.ArtistId)
.Must(artistId => Guid.TryParse(artistId, out Guid parsedArtistId) && parsedArtistId != Guid.Empty)
.When(query => query.ArtistId is not null && query.ArtistId.Length > 0)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
}
}
@@ -1,6 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using System;
using Lumina.Application.Common.DTO.Pagination;
using System.Diagnostics;
#endregion
@@ -9,12 +9,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <summary>
/// Query for getting the lightweight read models of all the albums of an artist.
/// </summary>
/// <param name="ArtistId">The unique identifier of the artist whose albums are retrieved.</param>
/// <param name="CurrentPage">The page of results to retrieve.</param>
/// <param name="PerPage">The maximum number of albums to retrieve per page.</param>
/// <param name="LibraryId">The unique identifier of the media library the artist belongs to, taken from the route.</param>
/// <param name="ArtistId">The unique identifier of the artist whose albums are retrieved, taken from the route.</param>
/// <param name="PaginationData">The object containing the requested pagination data.</param>
[DebuggerDisplay("ArtistId: {ArtistId}")]
public record GetArtistAlbumsLiteQuery(
Guid ArtistId,
int? CurrentPage,
int? PerPage
string? LibraryId,
string? ArtistId,
PaginationDataDto? PaginationData
) : IQuery;
@@ -67,25 +67,29 @@ public class GetArtistAlbumsLiteQueryHandler : IQueryHandler<GetArtistAlbumsLite
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(query.ArtistId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
// The validator guarantees that the route identifiers are non-empty Guids before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid artistId = Guid.Parse(query.ArtistId!);
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
if (getArtistResult.IsFailure)
return getArtistResult.Errors;
if (getArtistResult.Value is null)
return Errors.Music.ArtistNotFound;
ArtistEntity existingArtist = getArtistResult.Value;
// Resource scoping: the artist must belong to the library named by the route, so that the albums of an artist can never be read through
// another library's route; the mismatch is reported as not found, without disclosing that the artist exists in another library.
if (existingArtist.LibraryId != libraryId)
return Errors.Music.ArtistNotFound;
// Admins can see the albums of the artists of all libraries; for everyone else, only of the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(getArtistResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(existingArtist.LibraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
PaginationDataDto paginationData = new()
{
CurrentPage = query.CurrentPage ?? 1,
PerPage = query.PerPage ?? 200
};
Result<PaginatedResultDto<AlbumLiteRow>> getAlbumsResult = await _unitOfWork.AlbumRepository.GetAlbumsLiteByArtistIdAsync(query.ArtistId, paginationData, cancellationToken).ConfigureAwait(false);
Result<PaginatedResultDto<AlbumLiteRow>> getAlbumsResult = await _unitOfWork.AlbumRepository.GetAlbumsLiteByArtistIdAsync(artistId, query.PaginationData, cancellationToken).ConfigureAwait(false);
return getAlbumsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -17,10 +17,24 @@ public class GetArtistAlbumsLiteQueryValidator : AbstractValidator<GetArtistAlbu
/// </summary>
public GetArtistAlbumsLiteQueryValidator()
{
// Validates the identifier of the media library the artist belongs to, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.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);
// Validates the identifier of the artist whose albums are retrieved, taken from the route.
RuleFor(query => query.ArtistId)
.NotEmpty()
.WithError(Errors.Music.ArtistIdCannotBeEmpty)
.Must(artistId => artistId != Guid.Empty)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
RuleFor(query => query.ArtistId)
.Must(artistId => Guid.TryParse(artistId, out Guid parsedArtistId) && parsedArtistId != Guid.Empty)
.When(query => query.ArtistId is not null && query.ArtistId.Length > 0)
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
}
}
@@ -1,6 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using System;
using Lumina.Application.Common.DTO.Pagination;
using System.Diagnostics;
#endregion
@@ -9,14 +9,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <summary>
/// Query for getting all the artists of a media library.
/// </summary>
/// <param name="LibraryId">The Id of the media library whose artists are retrieved.</param>
/// <param name="CurrentPage">The page of results to retrieve.</param>
/// <param name="PerPage">The maximum number of artists to retrieve per page.</param>
/// <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>
[DebuggerDisplay("LibraryId: {LibraryId}")]
public record GetArtistsQuery(
Guid LibraryId,
int? CurrentPage,
int? PerPage,
string? LibraryId,
PaginationDataDto? PaginationData,
string? SearchTerm
) : IQuery;
@@ -67,26 +67,23 @@ public class GetArtistsQueryHandler : IQueryHandler<GetArtistsQuery, Result<Pagi
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// The validator guarantees that the library id of the route is a non-empty Guid before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
// Admins can see the artists of all libraries; for everyone else, only the artists of the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(query.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(libraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
PaginationDataDto paginationData = new()
{
CurrentPage = query.CurrentPage ?? 1,
PerPage = query.PerPage ?? 200
};
LibraryFilterDto libraryFilter = new()
{
LibraryId = query.LibraryId,
LibraryId = libraryId,
SearchTerm = query.SearchTerm
};
Result<PaginatedResultDto<ArtistEntity>> getArtistsResult = await _unitOfWork.ArtistRepository
.GetAllAsync(paginationData, filterModel: libraryFilter, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
.GetAllAsync(query.PaginationData, filterModel: libraryFilter, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
return getArtistsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -17,10 +17,14 @@ public class GetArtistsQueryValidator : AbstractValidator<GetArtistsQuery>
/// </summary>
public GetArtistsQueryValidator()
{
// Validates the identifier of the media library whose artists are retrieved, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
.Must(libraryId => libraryId != Guid.Empty)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.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);
}
}
@@ -1,6 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using System;
using Lumina.Application.Common.DTO.Pagination;
using System.Diagnostics;
#endregion
@@ -9,14 +9,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists
/// <summary>
/// Query for getting the lightweight read models of all the artists of a media library.
/// </summary>
/// <param name="LibraryId">The Id of the media library whose artists are retrieved.</param>
/// <param name="CurrentPage">The page of results to retrieve.</param>
/// <param name="PerPage">The maximum number of artists to retrieve per page.</param>
/// <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>
[DebuggerDisplay("LibraryId: {LibraryId}")]
public record GetArtistsLiteQuery(
Guid LibraryId,
int? CurrentPage,
int? PerPage,
string? LibraryId,
PaginationDataDto? PaginationData,
string? SearchTerm
) : IQuery;
@@ -66,25 +66,23 @@ public class GetArtistsLiteQueryHandler : IQueryHandler<GetArtistsLiteQuery, Res
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// The validator guarantees that the library id of the route is a non-empty Guid before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
// Admins can see the artists of all libraries; for everyone else, only the artists of the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(query.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(libraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return ApplicationErrors.Authorization.NotAuthorized;
PaginationDataDto paginationData = new()
{
CurrentPage = query.CurrentPage ?? 1,
PerPage = query.PerPage ?? 200
};
LibraryFilterDto filter = new()
{
LibraryId = query.LibraryId,
LibraryId = libraryId,
SearchTerm = query.SearchTerm
};
Result<PaginatedResultDto<ArtistLiteRow>> getArtistsResult = await _unitOfWork.ArtistRepository.GetAllLiteAsync(
paginationData,
query.PaginationData,
filterModel: filter,
cancellationToken: cancellationToken).ConfigureAwait(false);
return getArtistsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
@@ -17,10 +17,14 @@ public class GetArtistsLiteQueryValidator : AbstractValidator<GetArtistsLiteQuer
/// </summary>
public GetArtistsLiteQueryValidator()
{
// Validates the identifier of the media library whose artists are retrieved, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
.Must(libraryId => libraryId != Guid.Empty)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.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);
}
}