mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 07:11:10 +00:00
Refactors the artist slice across the stack
This commit is contained in:
1 parent
c112c64c25
commit
aa097ecc98
55 files changed
+3219
-278
No files matched your search
+10
@@ -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)
|
||||
|
||||
+1
-1
@@ -100,6 +100,6 @@ public class DeleteArtistCommandHandler : ICommandHandler<DeleteArtistCommand, R
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
return deleteArtistResult;
|
||||
return Result.Deleted;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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)
|
||||
|
||||
+22
@@ -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()
|
||||
|
||||
+4
-2
@@ -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;
|
||||
+14
-4
@@ -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();
|
||||
}
|
||||
}
|
||||
+16
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -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;
|
||||
+13
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
+16
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
+7
-7
@@ -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;
|
||||
+13
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
+16
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
+5
-7
@@ -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;
|
||||
+6
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
+5
-7
@@ -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;
|
||||
+6
-8
@@ -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);
|
||||
|
||||
+6
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user