Change
This commit is contained in:
@@ -12,8 +12,13 @@ using Musify.Domain.Entities;
|
|||||||
|
|
||||||
namespace Musify.Application.PlayLists.Handlers
|
namespace Musify.Application.PlayLists.Handlers
|
||||||
{
|
{
|
||||||
public class CreatePlayListCommandHandler(IEventBus eventBus, IDatabase database, IStorageService storageHandler,
|
public class CreatePlayListCommandHandler(
|
||||||
ILogger<CreatePlayListCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
IEventBus eventBus,
|
||||||
|
IDatabase database,
|
||||||
|
IStorageService storageService,
|
||||||
|
ILogger<CreatePlayListCommandHandler> logger,
|
||||||
|
ApplicationStorageConfiguration storageConfiguration,
|
||||||
|
PlayListConfiguration playListConfiguration)
|
||||||
: ICommandHandler<CreatePlayListCommand, Result<PlayListResponse>>
|
: ICommandHandler<CreatePlayListCommand, Result<PlayListResponse>>
|
||||||
{
|
{
|
||||||
public async ValueTask<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
public async ValueTask<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
||||||
@@ -25,7 +30,8 @@ namespace Musify.Application.PlayLists.Handlers
|
|||||||
return Result.NotFound($"User {request.UserId} not found");
|
return Result.NotFound($"User {request.UserId} not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
var playListEntity = CreatePlayListCommand.ToEntity(request,
|
var playList = CreatePlayListCommand.ToEntity(
|
||||||
|
request,
|
||||||
playListConfiguration.Routes.PresetOriginalPicture,
|
playListConfiguration.Routes.PresetOriginalPicture,
|
||||||
playListConfiguration.Routes.PresetSmallPicture,
|
playListConfiguration.Routes.PresetSmallPicture,
|
||||||
playListConfiguration.Routes.PresetMediumPicture,
|
playListConfiguration.Routes.PresetMediumPicture,
|
||||||
@@ -33,70 +39,57 @@ namespace Musify.Application.PlayLists.Handlers
|
|||||||
|
|
||||||
if (request.Picture is not null)
|
if (request.Picture is not null)
|
||||||
{
|
{
|
||||||
var uploadResult = await SetPicture(playListEntity, request.Picture, cancellationToken);
|
var uploadResult = await UploadPictureAsync(playList, request.Picture, cancellationToken);
|
||||||
if (!uploadResult.IsSuccess)
|
if (!uploadResult.IsSuccess)
|
||||||
return uploadResult;
|
return uploadResult;
|
||||||
|
|
||||||
var publishResult = await PublishUpdateEvent(playListEntity, cancellationToken);
|
|
||||||
if (!publishResult.IsSuccess)
|
|
||||||
{
|
|
||||||
await RollbackPicture(playListEntity, cancellationToken);
|
|
||||||
return publishResult;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await database.PlayLists.AddAsync(playListEntity, cancellationToken);
|
await database.PlayLists.AddAsync(playList, cancellationToken);
|
||||||
await database.SaveChangesAsync(cancellationToken);
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
await RollbackPicture(playListEntity, cancellationToken);
|
logger.LogError(exception, "Failed to save playlist for user {UserId}", request.UserId);
|
||||||
logger.LogError(exception, "Failed to create playlist for user {UserId}", request.UserId);
|
await RollbackAllPicturesAsync(playList, cancellationToken);
|
||||||
return Result.Error($"Failed to create playlist for user {request.UserId}");
|
return Result.Error($"Failed to create playlist for user {request.UserId}");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.LogInformation("Created playlist {PlayListId} for user {UserId}", playListEntity.Id, request.UserId);
|
if (request.Picture is not null)
|
||||||
return Result.Created(PlayListResponse.FromEntity(playListEntity));
|
{
|
||||||
|
var publishResult = await PublishPictureUpdateEventAsync(playList, cancellationToken);
|
||||||
|
if (!publishResult.IsSuccess)
|
||||||
|
return publishResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task RollbackPicture(PlayList playList, CancellationToken cancellationToken)
|
logger.LogInformation("Created playlist {PlayListId} for user {UserId}", playList.Id, request.UserId);
|
||||||
{
|
return Result.Created(PlayListResponse.FromEntity(playList));
|
||||||
try
|
|
||||||
{
|
|
||||||
await storageHandler.RemoveFileAsync(storageConfiguration.Bucket,
|
|
||||||
Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, playList.OriginalPictureName), cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "Failed to rollback picture upload for playlist {PlayListId}", playList.Id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task<Result> SetPicture(PlayList playList, IFileData picture, CancellationToken cancellationToken)
|
async Task<Result> UploadPictureAsync(PlayList playList, IFileData picture, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var pictureName = Guid.NewGuid() + picture.FileType;
|
var pictureName = Guid.NewGuid() + picture.FileType;
|
||||||
var pictureKey = Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, pictureName);
|
var pictureKey = Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, pictureName);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await storageHandler.UploadFileAsync(picture.FileStream, picture.ContentType, storageConfiguration.Bucket, pictureKey, cancellationToken);
|
await storageService.UploadFileAsync(picture.FileStream, picture.ContentType, storageConfiguration.Bucket, pictureKey, cancellationToken);
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return Result.Error($"An error occurred while uploading picture for playlist {playList.Id} to storage.");
|
|
||||||
}
|
|
||||||
|
|
||||||
playList.OriginalPictureName = pictureName;
|
playList.OriginalPictureName = pictureName;
|
||||||
return Result.Success();
|
return Result.Success();
|
||||||
}
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "Failed to upload picture for playlist {PlayListId}", playList.Id);
|
||||||
|
return Result.Error($"Failed to upload picture for playlist {playList.Id}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async Task<Result> PublishUpdateEvent(PlayList playList, CancellationToken cancellationToken)
|
async Task<Result> PublishPictureUpdateEventAsync(PlayList playList, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await eventBus.PublishAsync(new UpdatePlayListPictureEvent(
|
var updateEvent = new UpdatePlayListPictureEvent(
|
||||||
playList.Id,
|
playList.Id,
|
||||||
storageConfiguration.Bucket,
|
storageConfiguration.Bucket,
|
||||||
Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, playList.OriginalPictureName),
|
Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, playList.OriginalPictureName),
|
||||||
@@ -111,14 +104,32 @@ namespace Musify.Application.PlayLists.Handlers
|
|||||||
new ImageSize(
|
new ImageSize(
|
||||||
playListConfiguration.Routes.LargePicturesPath,
|
playListConfiguration.Routes.LargePicturesPath,
|
||||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||||
playListConfiguration.PicturesSizes.LargePictureHeight)),
|
playListConfiguration.PicturesSizes.LargePictureHeight));
|
||||||
cancellationToken);
|
|
||||||
|
await eventBus.PublishAsync(updateEvent, cancellationToken);
|
||||||
return Result.Success();
|
return Result.Success();
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
logger.LogError(exception, "Failed to publish playlist picture update event for playlist {PlayListId}", playList.Id);
|
logger.LogError(exception, "Failed to publish picture update event for playlist {PlayListId}", playList.Id);
|
||||||
return Result.Error($"Failed to publish playlist {playList.Id} picture update event");
|
return Result.Error($"Failed to publish picture update event for playlist {playList.Id}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task RollbackAllPicturesAsync(PlayList playList, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(playList.OriginalPictureName))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var originalPicturePath = Path.Combine(playListConfiguration.Routes.OriginalPicturesPath, playList.OriginalPictureName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await storageService.RemoveFileAsync(storageConfiguration.Bucket, originalPicturePath, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "Failed to rollback picture upload for playlist {PlayListId}", playList.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user