Albums previously had no cover concept at all: no picture upload
flow, no GET /albums/{id}/cover, and AlbumApplicationResponse's
image-key fields were plumbed but never populated. Description,
Pictures and TrackCount already lived on the shared Album base entity
for both local and external albums, so unifying the cover story only
required building the missing local-album picture pipeline.
Backend, cloning PlayList's existing async picture-processing
architecture exactly (MassTransit routing slip + saga), but making
the picture mandatory instead of optional (matching Track's pattern):
- AlbumPictures moves from all-nullable to required fields.
- New AlbumConfiguration (routes/sizes), deliberately without
PlayList's preset-picture fallback since a picture is now required
up front.
- CreateAlbum now takes a mandatory PictureIntentId, validates and
consumes the upload intent, and kicks off the same
copy-to-final -> resize -> upload -> update-entity routing slip
PlayLists use, via a new AlbumProcessingState saga that marks the
album Failed on a picture-processing failure.
- UpdateAlbum takes an optional NewPictureIntentId to replace the
cover later.
- New GET /albums/{id}/cover and POST /albums/upload-picture
endpoints, mirroring the playlist ones.
- New EF migration for the AlbumProcessingState saga table.
Frontend:
- AlbumForm gains a cover dropzone, required on create and optional
on edit (via ImageDropzone's new `required` prop), uploaded through
the same presigned-upload helper playlists use.
- New /api/albums/{id}/cover proxy route.
- PlaylistArt gains a generic coverUrl prop so AlbumHeader and the
album list/detail cards render the real uploaded cover instead of
only the track mosaic fallback.
Delete-time picture cleanup for albums is intentionally left as a
follow-up, matching the existing gap in playlist deletion.
153 lines
6.3 KiB
C#
153 lines
6.3 KiB
C#
using ErrorOr;
|
|
using Mediator;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using Musify.Application.Configuration;
|
|
using Musify.Application.Shared;
|
|
using Musify.Application.Events;
|
|
using Musify.Application.Albums.Responses;
|
|
using Musify.Application.Services;
|
|
using Musify.Domain.Entities;
|
|
using Musify.Domain.ValueObjects;
|
|
using Musify.Application.Contracts;
|
|
|
|
namespace Musify.Application.Albums
|
|
{
|
|
public record UpdateAlbumCommand(
|
|
long UserId,
|
|
Guid AlbumId,
|
|
string Title,
|
|
string? Description,
|
|
int? ReleaseYear,
|
|
Guid? NewPictureIntentId)
|
|
: ICommand<ErrorOr<AlbumApplicationResponse>>;
|
|
|
|
public class UpdateAlbumCommandHandler(
|
|
IEventBus eventBus,
|
|
IDatabase database,
|
|
UploadIntentValidator uploadIntentValidator,
|
|
ILogger<UpdateAlbumCommandHandler> logger,
|
|
ApplicationStorageConfiguration storageConfiguration,
|
|
AlbumConfiguration albumConfiguration,
|
|
UploadIntentConfiguration uploadIntentConfiguration)
|
|
: ICommandHandler<UpdateAlbumCommand, ErrorOr<AlbumApplicationResponse>>
|
|
{
|
|
public async ValueTask<ErrorOr<AlbumApplicationResponse>> Handle(UpdateAlbumCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var album = await database.UserAlbums
|
|
.SingleOrDefaultAsync(a => a.Id == request.AlbumId, cancellationToken);
|
|
if (album is null)
|
|
{
|
|
logger.LogInformation("Album {AlbumId} not found", request.AlbumId);
|
|
return Error.NotFound();
|
|
}
|
|
|
|
if (album.OwnerUserId != request.UserId)
|
|
{
|
|
logger.LogWarning("Album {AlbumId} does not belong to user {UserId}", request.AlbumId, request.UserId);
|
|
return Error.Unauthorized();
|
|
}
|
|
|
|
var title = request.Title.Trim();
|
|
|
|
album.Title = title;
|
|
album.NormalizedTitle = title.ToUpperInvariant();
|
|
album.Description = request.Description;
|
|
album.ReleaseYear = request.ReleaseYear;
|
|
|
|
UploadIntent? pictureIntent = null;
|
|
string? finalPictureKey = null;
|
|
|
|
if (request.NewPictureIntentId.HasValue)
|
|
{
|
|
var validation = await uploadIntentValidator.ValidateAndLoadAsync(
|
|
uploadIntentConfiguration,
|
|
request.NewPictureIntentId.Value, request.UserId, cancellationToken);
|
|
if (validation.IsError)
|
|
return validation.Errors;
|
|
|
|
pictureIntent = validation.Value;
|
|
album.Pictures = new AlbumPictures
|
|
{
|
|
OriginalName = pictureIntent.ObjectName,
|
|
SmallName = pictureIntent.ObjectName,
|
|
MediumName = pictureIntent.ObjectName,
|
|
LargeName = pictureIntent.ObjectName
|
|
};
|
|
finalPictureKey = albumConfiguration.Routes.BuildOriginalPicturePath(request.UserId, pictureIntent.ObjectName);
|
|
}
|
|
|
|
database.UserAlbums.Update(album);
|
|
|
|
if (pictureIntent != null && finalPictureKey != null)
|
|
{
|
|
var publishResult = await PublishUpdateAlbumPictureSourceEventAsync(
|
|
album.Id, pictureIntent, finalPictureKey, cancellationToken);
|
|
if (publishResult.IsError)
|
|
return publishResult.Errors;
|
|
}
|
|
|
|
try
|
|
{
|
|
await database.SaveChangesAsync(cancellationToken);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError(exception, "Failed to update album {AlbumId}", request.AlbumId);
|
|
return Error.Failure(description: $"Failed to update album {request.AlbumId}");
|
|
}
|
|
|
|
var albumTracks = database.AlbumHasTracks
|
|
.AsNoTracking()
|
|
.Where(albumTrack => albumTrack.AlbumId == album.Id);
|
|
|
|
var trackCount = await albumTracks.CountAsync(cancellationToken);
|
|
var coverTrackIds = await albumTracks
|
|
.OrderBy(albumTrack => albumTrack.TrackNumber)
|
|
.Take(AlbumApplicationResponse.CoverTrackCount)
|
|
.Select(albumTrack => albumTrack.TrackId)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
logger.LogInformation("Updated album {AlbumId}", album.Id);
|
|
return AlbumApplicationResponse.FromEntity(album, trackCount, coverTrackIds);
|
|
}
|
|
|
|
private async Task<ErrorOr<Success>> PublishUpdateAlbumPictureSourceEventAsync(
|
|
Guid albumId,
|
|
UploadIntent pictureIntent,
|
|
string finalPictureKey,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
await eventBus.PublishAsync(
|
|
new UpdateAlbumPictureSourceEvent(
|
|
albumId,
|
|
pictureIntent.Id,
|
|
storageConfiguration.Bucket,
|
|
pictureIntent.Key,
|
|
finalPictureKey,
|
|
new ImageSize(
|
|
albumConfiguration.Routes.SmallPicturesPath,
|
|
albumConfiguration.PicturesSizes.SmallPictureWidth,
|
|
albumConfiguration.PicturesSizes.SmallPictureHeight),
|
|
new ImageSize(
|
|
albumConfiguration.Routes.MediumPicturesPath,
|
|
albumConfiguration.PicturesSizes.MediumPictureWidth,
|
|
albumConfiguration.PicturesSizes.MediumPictureHeight),
|
|
new ImageSize(
|
|
albumConfiguration.Routes.LargePicturesPath,
|
|
albumConfiguration.PicturesSizes.LargePictureWidth,
|
|
albumConfiguration.PicturesSizes.LargePictureHeight)),
|
|
cancellationToken);
|
|
return new Success();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError(exception, "Failed to publish update album picture source event for album {AlbumId}", albumId);
|
|
return Error.Failure(description: $"Failed to publish update album picture source event for album {albumId}");
|
|
}
|
|
}
|
|
}
|
|
}
|