mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Refactors the album slice across the stack
This commit is contained in:
1 parent
aa097ecc98
commit
d4cc3c1f8d
37 files changed
+600
-305
No files matched your search
+1
-1
@@ -81,7 +81,7 @@ public class AddAlbumCommandHandler : ICommandHandler<AddAlbumCommand, Result<Al
|
||||
Guid artistId = Guid.Parse(command.ArtistId!);
|
||||
|
||||
// An album is a child of the artist aggregate, so the whole aggregate is loaded and the album is added within it.
|
||||
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getArtistResult.IsFailure)
|
||||
return getArtistResult.Errors;
|
||||
if (getArtistResult.Value is null)
|
||||
|
||||
+6
-3
@@ -1,6 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -9,8 +8,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.
|
||||
/// <summary>
|
||||
/// Command for deleting an album by its Id.
|
||||
/// </summary>
|
||||
/// <param name="AlbumId">The unique identifier of the album to delete.</param>
|
||||
/// <param name="LibraryId">The unique identifier of the media library this album belongs to, taken from the route.</param>
|
||||
/// <param name="ArtistId">The unique identifier of the artist the album belongs to, taken from the route.</param>
|
||||
/// <param name="AlbumId">The unique identifier of the album to delete, taken from the route.</param>
|
||||
[DebuggerDisplay("AlbumId: {AlbumId}")]
|
||||
public record DeleteAlbumCommand(
|
||||
Guid AlbumId
|
||||
string? LibraryId,
|
||||
string? ArtistId,
|
||||
string? AlbumId
|
||||
) : ICommand;
|
||||
+53
-11
@@ -1,18 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.MusicLibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.AudioLibraryBoundedContext.MusicLibraryAggregate.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
using DomainErrors = Lumina.Domain.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.Commands.DeleteAlbum;
|
||||
@@ -60,25 +65,62 @@ public class DeleteAlbumCommandHandler : ICommandHandler<DeleteAlbumCommand, Res
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(command.AlbumId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getAlbumResult.IsFailure)
|
||||
return getAlbumResult.Errors;
|
||||
if (getAlbumResult.Value is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
// The validator guarantees that the route identifiers are non-empty Guids before this point.
|
||||
Guid libraryId = Guid.Parse(command.LibraryId!);
|
||||
Guid artistId = Guid.Parse(command.ArtistId!);
|
||||
Guid albumId = Guid.Parse(command.AlbumId!);
|
||||
|
||||
// An album is a child of the artist aggregate, so the whole aggregate is loaded and the album is removed within it.
|
||||
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository
|
||||
.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getArtistResult.IsFailure)
|
||||
return getArtistResult.Errors;
|
||||
if (getArtistResult.Value is null)
|
||||
return DomainErrors.Music.ArtistNotFound;
|
||||
ArtistEntity existingArtist = getArtistResult.Value;
|
||||
|
||||
// Resource scoping: the album must belong to an artist of the library named by the route, so that an album can never be deleted
|
||||
// through another library's or artist's route; the mismatch is reported as not found, without disclosing that the album exists elsewhere.
|
||||
if (existingArtist.LibraryId != libraryId)
|
||||
return DomainErrors.Music.AlbumNotFound;
|
||||
AlbumEntity? existingAlbum = existingArtist.Albums.FirstOrDefault(album => album.Id == albumId);
|
||||
if (existingAlbum is null)
|
||||
return DomainErrors.Music.AlbumNotFound;
|
||||
|
||||
// Admins can delete the albums of all libraries; for everyone else, only the albums of the libraries they own.
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
userId, new LibraryOwnershipPolicyContext(getAlbumResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
userId, new LibraryOwnershipPolicyContext(existingArtist.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
Result<Deleted> deleteAlbumResult = await _unitOfWork.AlbumRepository.DeleteByIdAsync(command.AlbumId, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteAlbumResult.IsFailure)
|
||||
return deleteAlbumResult.Errors;
|
||||
// An album can only belong to a library that exists, so a client can never delete an album of a library of the host that is not there.
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository
|
||||
.GetByIdAsync(libraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure)
|
||||
return getLibraryResult.Errors;
|
||||
if (getLibraryResult.Value is null)
|
||||
return DomainErrors.Library.LibraryNotFound;
|
||||
|
||||
// The write path goes through the aggregate root: an album is a child entity of the artist aggregate, unlike Book and Artist,
|
||||
// which are aggregate roots and are deleted directly through their own repositories. This difference is intentional.
|
||||
Result<Artist> artistResult = existingArtist.ToDomainEntity();
|
||||
if (artistResult.IsFailure)
|
||||
return artistResult.Errors;
|
||||
Album? domainAlbum = artistResult.Value.Albums.FirstOrDefault(album => album.Id.Value == albumId);
|
||||
if (domainAlbum is null)
|
||||
return DomainErrors.Music.AlbumNotFound;
|
||||
Result<Deleted> removeAlbumResult = artistResult.Value.RemoveAlbum(domainAlbum);
|
||||
if (removeAlbumResult.IsFailure)
|
||||
return removeAlbumResult.Errors;
|
||||
|
||||
Result<Updated> updateResult = await _unitOfWork.ArtistRepository
|
||||
.UpdateAsync(artistResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
|
||||
if (updateResult.IsFailure)
|
||||
return updateResult.Errors;
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
return deleteAlbumResult;
|
||||
return Result.Deleted;
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -17,10 +17,34 @@ public class DeleteAlbumCommandValidator : AbstractValidator<DeleteAlbumCommand>
|
||||
/// </summary>
|
||||
public DeleteAlbumCommandValidator()
|
||||
{
|
||||
// Validates the identifier of the media library that owns the album, taken from the route.
|
||||
RuleFor(command => command.LibraryId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.LibraryId)
|
||||
.Must(libraryId => Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty)
|
||||
.When(command => command.LibraryId is not null && command.LibraryId.Length > 0)
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Validates the identifier of the artist the album belongs to, taken from the route.
|
||||
RuleFor(command => command.ArtistId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.ArtistId)
|
||||
.Must(artistId => Guid.TryParse(artistId, out Guid parsedArtistId) && parsedArtistId != Guid.Empty)
|
||||
.When(command => command.ArtistId is not null && command.ArtistId.Length > 0)
|
||||
.WithError(Errors.Music.ArtistIdCannotBeEmpty);
|
||||
|
||||
// Validates the identifier of the album to delete, taken from the route.
|
||||
RuleFor(command => command.AlbumId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty)
|
||||
.Must(albumId => albumId != Guid.Empty)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
|
||||
RuleFor(command => command.AlbumId)
|
||||
.Must(albumId => Guid.TryParse(albumId, out Guid parsedAlbumId) && parsedAlbumId != Guid.Empty)
|
||||
.When(command => command.AlbumId is not null && command.AlbumId.Length > 0)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -88,7 +88,7 @@ public class UpdateAlbumCommandHandler : ICommandHandler<UpdateAlbumCommand, Res
|
||||
Guid albumId = Guid.Parse(command.AlbumId!);
|
||||
|
||||
// An album is a child of the artist aggregate, so the whole aggregate is loaded and the album is edited within it.
|
||||
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
Result<ArtistEntity?> getArtistResult = await _unitOfWork.ArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getArtistResult.IsFailure)
|
||||
return getArtistResult.Errors;
|
||||
|
||||
|
||||
+6
-2
@@ -9,8 +9,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.
|
||||
/// <summary>
|
||||
/// Query for getting an album by its Id.
|
||||
/// </summary>
|
||||
/// <param name="AlbumId">The unique identifier of the album to get.</param>
|
||||
/// <param name="LibraryId">The unique identifier of the media library the album belongs to, taken from the route.</param>
|
||||
/// <param name="ArtistId">The unique identifier of the artist the album belongs to, taken from the route.</param>
|
||||
/// <param name="AlbumId">The unique identifier of the album to get, taken from the route.</param>
|
||||
[DebuggerDisplay("AlbumId: {AlbumId}")]
|
||||
public record GetAlbumQuery(
|
||||
Guid AlbumId
|
||||
string? LibraryId,
|
||||
string? ArtistId,
|
||||
string? AlbumId
|
||||
) : IQuery;
|
||||
+14
-3
@@ -65,18 +65,29 @@ public class GetAlbumQueryHandler : IQueryHandler<GetAlbumQuery, Result<AlbumRes
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(query.AlbumId, 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!);
|
||||
Guid albumId = Guid.Parse(query.AlbumId!);
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(albumId, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getAlbumResult.IsFailure)
|
||||
return getAlbumResult.Errors;
|
||||
if (getAlbumResult.Value is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
AlbumEntity existingAlbum = getAlbumResult.Value;
|
||||
|
||||
// Resource scoping: the album must belong to the library and the artist named by the route, so that an album can never be read through
|
||||
// another library's or artist's route; the mismatch is reported as not found, without disclosing that the album exists elsewhere.
|
||||
if (existingAlbum.LibraryId != libraryId || existingAlbum.ArtistId != artistId)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
|
||||
// Admins can see the albums of all libraries; for everyone else, only the albums of the libraries they own.
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
userId, new LibraryOwnershipPolicyContext(getAlbumResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
userId, new LibraryOwnershipPolicyContext(existingAlbum.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
return getAlbumResult.Value.ToResponse();
|
||||
return existingAlbum.ToResponse();
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -17,10 +17,34 @@ public class GetAlbumQueryValidator : AbstractValidator<GetAlbumQuery>
|
||||
/// </summary>
|
||||
public GetAlbumQueryValidator()
|
||||
{
|
||||
// Validates the identifier of the media library the album 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 the album belongs to, taken from the route.
|
||||
RuleFor(query => query.ArtistId)
|
||||
.NotEmpty()
|
||||
.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);
|
||||
|
||||
// Validates the identifier of the album to get, taken from the route.
|
||||
RuleFor(query => query.AlbumId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty)
|
||||
.Must(albumId => albumId != Guid.Empty)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
|
||||
RuleFor(query => query.AlbumId)
|
||||
.Must(albumId => Guid.TryParse(albumId, out Guid parsedAlbumId) && parsedAlbumId != Guid.Empty)
|
||||
.When(query => query.AlbumId is not null && query.AlbumId.Length > 0)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -9,8 +9,12 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.
|
||||
/// <summary>
|
||||
/// Query for getting all the tracks of an album.
|
||||
/// </summary>
|
||||
/// <param name="AlbumId">The unique identifier of the album whose tracks are retrieved.</param>
|
||||
/// <param name="LibraryId">The unique identifier of the media library the album belongs to, taken from the route.</param>
|
||||
/// <param name="ArtistId">The unique identifier of the artist the album belongs to, taken from the route.</param>
|
||||
/// <param name="AlbumId">The unique identifier of the album whose tracks are retrieved, taken from the route.</param>
|
||||
[DebuggerDisplay("AlbumId: {AlbumId}")]
|
||||
public record GetAlbumTracksQuery(
|
||||
Guid AlbumId
|
||||
string? LibraryId,
|
||||
string? ArtistId,
|
||||
string? AlbumId
|
||||
) : IQuery;
|
||||
+14
-3
@@ -65,19 +65,30 @@ public class GetAlbumTracksQueryHandler : IQueryHandler<GetAlbumTracksQuery, Res
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(query.AlbumId, 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!);
|
||||
Guid albumId = Guid.Parse(query.AlbumId!);
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getAlbumResult.IsFailure)
|
||||
return getAlbumResult.Errors;
|
||||
if (getAlbumResult.Value is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
AlbumEntity existingAlbum = getAlbumResult.Value;
|
||||
|
||||
// Resource scoping: the album must belong to the library and the artist named by the route, so that the tracks of an album can never be read
|
||||
// through another library's or artist's route; the mismatch is reported as not found, without disclosing that the album exists elsewhere.
|
||||
if (existingAlbum.LibraryId != libraryId || existingAlbum.ArtistId != artistId)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
|
||||
// Admins can see the tracks of the albums of all libraries; for everyone else, only of the libraries they own.
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
userId, new LibraryOwnershipPolicyContext(getAlbumResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
userId, new LibraryOwnershipPolicyContext(existingAlbum.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
Result<IReadOnlyList<TrackEntity>> getTracksResult = await _unitOfWork.TrackRepository.GetByAlbumIdAsync(query.AlbumId, cancellationToken).ConfigureAwait(false);
|
||||
Result<IReadOnlyList<TrackEntity>> getTracksResult = await _unitOfWork.TrackRepository.GetByAlbumIdAsync(albumId, cancellationToken).ConfigureAwait(false);
|
||||
return getTracksResult.Match(value => Result.From(value.ToResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -17,10 +17,34 @@ public class GetAlbumTracksQueryValidator : AbstractValidator<GetAlbumTracksQuer
|
||||
/// </summary>
|
||||
public GetAlbumTracksQueryValidator()
|
||||
{
|
||||
// Validates the identifier of the media library the album 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 the album belongs to, taken from the route.
|
||||
RuleFor(query => query.ArtistId)
|
||||
.NotEmpty()
|
||||
.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);
|
||||
|
||||
// Validates the identifier of the album whose tracks are retrieved, taken from the route.
|
||||
RuleFor(query => query.AlbumId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty)
|
||||
.Must(albumId => albumId != Guid.Empty)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
|
||||
RuleFor(query => query.AlbumId)
|
||||
.Must(albumId => Guid.TryParse(albumId, out Guid parsedAlbumId) && parsedAlbumId != Guid.Empty)
|
||||
.When(query => query.AlbumId is not null && query.AlbumId.Length > 0)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+9
-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,14 @@ namespace Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Albums.
|
||||
/// <summary>
|
||||
/// Query for getting the lightweight read models of all the tracks of an album.
|
||||
/// </summary>
|
||||
/// <param name="AlbumId">The unique identifier of the album whose tracks are retrieved.</param>
|
||||
/// <param name="CurrentPage">The page of results to retrieve.</param>
|
||||
/// <param name="PerPage">The maximum number of tracks to retrieve per page.</param>
|
||||
/// <param name="LibraryId">The unique identifier of the media library the album belongs to, taken from the route.</param>
|
||||
/// <param name="ArtistId">The unique identifier of the artist the album belongs to, taken from the route.</param>
|
||||
/// <param name="AlbumId">The unique identifier of the album whose tracks are retrieved, taken from the route.</param>
|
||||
/// <param name="PaginationData">The object containing the requested pagination data.</param>
|
||||
[DebuggerDisplay("AlbumId: {AlbumId}")]
|
||||
public record GetAlbumTracksLiteQuery(
|
||||
Guid AlbumId,
|
||||
int? CurrentPage,
|
||||
int? PerPage
|
||||
string? LibraryId,
|
||||
string? ArtistId,
|
||||
string? AlbumId,
|
||||
PaginationDataDto? PaginationData
|
||||
) : IQuery;
|
||||
+14
-9
@@ -67,25 +67,30 @@ public class GetAlbumTracksLiteQueryHandler : IQueryHandler<GetAlbumTracksLiteQu
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(query.AlbumId, 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!);
|
||||
Guid albumId = Guid.Parse(query.AlbumId!);
|
||||
|
||||
Result<AlbumEntity?> getAlbumResult = await _unitOfWork.AlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: false, shouldTrackEntities: false, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getAlbumResult.IsFailure)
|
||||
return getAlbumResult.Errors;
|
||||
if (getAlbumResult.Value is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
AlbumEntity existingAlbum = getAlbumResult.Value;
|
||||
|
||||
// Resource scoping: the album must belong to the library and the artist named by the route, so that the tracks of an album can never be read
|
||||
// through another library's or artist's route; the mismatch is reported as not found, without disclosing that the album exists elsewhere.
|
||||
if (existingAlbum.LibraryId != libraryId || existingAlbum.ArtistId != artistId)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
|
||||
// Admins can see the tracks of the albums of all libraries; for everyone else, only of the libraries they own.
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
userId, new LibraryOwnershipPolicyContext(getAlbumResult.Value.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
userId, new LibraryOwnershipPolicyContext(existingAlbum.LibraryId), cancellationToken).ConfigureAwait(false);
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
PaginationDataDto paginationData = new()
|
||||
{
|
||||
CurrentPage = query.CurrentPage ?? 1,
|
||||
PerPage = query.PerPage ?? 200
|
||||
};
|
||||
|
||||
Result<PaginatedResultDto<TrackLiteRow>> getTracksResult = await _unitOfWork.TrackRepository.GetTracksLiteByAlbumIdAsync(query.AlbumId, paginationData, cancellationToken).ConfigureAwait(false);
|
||||
Result<PaginatedResultDto<TrackLiteRow>> getTracksResult = await _unitOfWork.TrackRepository.GetTracksLiteByAlbumIdAsync(albumId, query.PaginationData, cancellationToken).ConfigureAwait(false);
|
||||
return getTracksResult.Match(value => Result.From(value.ToResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -17,10 +17,34 @@ public class GetAlbumTracksLiteQueryValidator : AbstractValidator<GetAlbumTracks
|
||||
/// </summary>
|
||||
public GetAlbumTracksLiteQueryValidator()
|
||||
{
|
||||
// Validates the identifier of the media library the album 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 the album belongs to, taken from the route.
|
||||
RuleFor(query => query.ArtistId)
|
||||
.NotEmpty()
|
||||
.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);
|
||||
|
||||
// Validates the identifier of the album whose tracks are retrieved, taken from the route.
|
||||
RuleFor(query => query.AlbumId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty)
|
||||
.Must(albumId => albumId != Guid.Empty)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
|
||||
RuleFor(query => query.AlbumId)
|
||||
.Must(albumId => Guid.TryParse(albumId, out Guid parsedAlbumId) && parsedAlbumId != Guid.Empty)
|
||||
.When(query => query.AlbumId is not null && query.AlbumId.Length > 0)
|
||||
.WithError(Errors.Music.AlbumIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user