mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 09:11:10 +00:00
Refactors the album slice across the stack
This commit is contained in:
1 parent
b51013a9e9
commit
e842c7baaf
20 files changed
+241
-206
No files matched your search
+20
@@ -429,7 +429,27 @@ public class AddAlbumCommandValidator : AbstractValidator<AddAlbumCommand>
|
||||
.When(m => m!.OriginalLanguage is not null);
|
||||
});
|
||||
|
||||
// Validates the MusicBrainz identifiers of the track.
|
||||
track.RuleFor(t => t!.MusicBrainzRecordingId)
|
||||
.Must(musicBrainzRecordingId => musicBrainzRecordingId != Guid.Empty)
|
||||
.When(t => t!.MusicBrainzRecordingId.HasValue)
|
||||
.WithError(Errors.Music.MusicBrainzIdInvalidFormat);
|
||||
|
||||
track.RuleFor(t => t!.MusicBrainzTrackId)
|
||||
.Must(musicBrainzTrackId => musicBrainzTrackId != Guid.Empty)
|
||||
.When(t => t!.MusicBrainzTrackId.HasValue)
|
||||
.WithError(Errors.Music.MusicBrainzIdInvalidFormat);
|
||||
|
||||
track.RuleFor(t => t!.MusicBrainzWorkId)
|
||||
.Must(musicBrainzWorkId => musicBrainzWorkId != Guid.Empty)
|
||||
.When(t => t!.MusicBrainzWorkId.HasValue)
|
||||
.WithError(Errors.Music.MusicBrainzIdInvalidFormat);
|
||||
|
||||
// Validates the ordering and performance characteristics of the track.
|
||||
track.RuleFor(t => t!.TrackNumber)
|
||||
.NotNull()
|
||||
.WithError(Errors.Music.TrackNumberMustBeGreaterThanZero);
|
||||
|
||||
track.RuleFor(t => t!.TrackNumber)
|
||||
.GreaterThan(0)
|
||||
.When(t => t!.TrackNumber.HasValue)
|
||||
|
||||
+8
-6
@@ -79,13 +79,10 @@ public class DeleteAlbumCommandHandler : ICommandHandler<DeleteAlbumCommand, Res
|
||||
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.
|
||||
// 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 route; the mismatch is reported as not found, without disclosing that the artist exists in another library.
|
||||
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;
|
||||
return DomainErrors.Music.ArtistNotFound;
|
||||
|
||||
// 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>(
|
||||
@@ -93,6 +90,10 @@ public class DeleteAlbumCommandHandler : ICommandHandler<DeleteAlbumCommand, Res
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
AlbumEntity? existingAlbum = existingArtist.Albums.FirstOrDefault(album => album.Id == albumId);
|
||||
if (existingAlbum is null)
|
||||
return DomainErrors.Music.AlbumNotFound;
|
||||
|
||||
// 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);
|
||||
@@ -106,6 +107,7 @@ public class DeleteAlbumCommandHandler : ICommandHandler<DeleteAlbumCommand, Res
|
||||
Result<Artist> artistResult = existingArtist.ToDomainEntity();
|
||||
if (artistResult.IsFailure)
|
||||
return artistResult.Errors;
|
||||
// The album is an entity inside the artist aggregate, so it is referenced by object, not by id; the aggregate member is located here and passed through.
|
||||
Album? domainAlbum = artistResult.Value.Albums.FirstOrDefault(album => album.Id.Value == albumId);
|
||||
if (domainAlbum is null)
|
||||
return DomainErrors.Music.AlbumNotFound;
|
||||
|
||||
+13
-9
@@ -92,14 +92,14 @@ public class UpdateAlbumCommandHandler : ICommandHandler<UpdateAlbumCommand, Res
|
||||
if (getArtistResult.IsFailure)
|
||||
return getArtistResult.Errors;
|
||||
|
||||
// Resource scoping: the album must belong to the library and the artist named by the route, so that an album can never be edited
|
||||
// through another library's or artist's route; the mismatch is reported as not found, without disclosing that the album exists elsewhere.
|
||||
if (getArtistResult.Value is null || getArtistResult.Value.LibraryId != libraryId)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
if (getArtistResult.Value is null)
|
||||
return Errors.Music.ArtistNotFound;
|
||||
ArtistEntity existingArtist = getArtistResult.Value;
|
||||
AlbumEntity? existingAlbum = existingArtist.Albums.FirstOrDefault(album => album.Id == albumId);
|
||||
if (existingAlbum is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
|
||||
// Resource scoping: the album must belong to an artist of the library named by the route, so that an album can never be edited 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 update the albums of all libraries; for everyone else, only the albums of the libraries they own.
|
||||
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
@@ -107,6 +107,10 @@ public class UpdateAlbumCommandHandler : ICommandHandler<UpdateAlbumCommand, Res
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
AlbumEntity? existingAlbum = existingArtist.Albums.FirstOrDefault(album => album.Id == albumId);
|
||||
if (existingAlbum is null)
|
||||
return Errors.Music.AlbumNotFound;
|
||||
|
||||
// An album can only belong to a library that exists, so a client can never update 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)
|
||||
@@ -119,8 +123,8 @@ public class UpdateAlbumCommandHandler : ICommandHandler<UpdateAlbumCommand, Res
|
||||
if (getContributorsResult.IsFailure)
|
||||
return getContributorsResult.Errors;
|
||||
|
||||
// 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 reconstituted directly with their own Create method. This difference is intentional.
|
||||
// The write path goes through the aggregate root: an album is a child entity of the artist aggregate, so it is mutated through its owning
|
||||
// artist. Book is itself an aggregate root, so its update flow reconstitutes it directly with its own Create method instead.
|
||||
Result<Artist> artistResult = existingArtist.ToDomainEntity();
|
||||
if (artistResult.IsFailure)
|
||||
return artistResult.Errors;
|
||||
|
||||
Reference in new issue
Block a user