Move
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Application.PlayLists.Commands;
|
||||
using Musify.Application.PlayLists.Responses;
|
||||
using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class CreatePlayListCommandHandler(IEventBus eventBus, IDatabase database, IStorageHandler storageHandler, IPictureHandler pictureHandler,
|
||||
ILogger<CreatePlayListCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
: IRequestHandler<CreatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userExists = await database.Users.AnyAsync(u => u.Id == request.UserId, cancellationToken);
|
||||
|
||||
if (!userExists)
|
||||
{
|
||||
logger.LogWarning("User with Id={UserId} does not exist.", request.UserId);
|
||||
return Result.NotFound($"User with Id {request.UserId} does not exist.");
|
||||
}
|
||||
|
||||
var playList = new PlayList
|
||||
{
|
||||
UserId = request.UserId,
|
||||
Name = request.Name,
|
||||
NormalizedName = request.Name.Trim().ToUpperInvariant(),
|
||||
Description = request.Description,
|
||||
SmallPictureKeyName = playListConfiguration.Routes.PresetSmallPicture,
|
||||
MediumPictureKeyName = playListConfiguration.Routes.PresetMediumPicture,
|
||||
LargePictureKeyName = playListConfiguration.Routes.PresetLargePicture,
|
||||
};
|
||||
|
||||
if (request.Picture is not null)
|
||||
{
|
||||
var imageId = Guid.NewGuid();
|
||||
var pictureName = $"{imageId}{request.Picture.FileType}";
|
||||
var originalImageStorageKey = Path.Join(playListConfiguration.Routes.OriginalPictures, pictureName);
|
||||
|
||||
var uploadResult = await storageHandler.UploadFileAsync(
|
||||
request.Picture.FileStream,
|
||||
request.Picture.ContentType,
|
||||
storageConfiguration.BucketName,
|
||||
originalImageStorageKey,
|
||||
cancellationToken);
|
||||
|
||||
if (!uploadResult.IsSuccess)
|
||||
{
|
||||
logger.LogError("Failed to upload picture for PlayList with Id={PlayListId} to storage. Storage handler returned error: {ErrorMessage}",
|
||||
playList.Id, string.Join("; ", uploadResult.Errors));
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
logger.LogInformation("Playlist image uploaded successfully for PlayList with Id={PlayListId}.", playList.Id);
|
||||
|
||||
var smallPictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.SmallPictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
var mediumPictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.MediumPictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
var largePictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.LargePictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new UpdatePlayListPictureEvent(
|
||||
playList.Id,
|
||||
storageConfiguration.BucketName,
|
||||
originalImageStorageKey,
|
||||
smallPictureKeyName,
|
||||
playListConfiguration.PicturesSizes.SmallPictureWidth,
|
||||
playListConfiguration.PicturesSizes.SmallPictureHeight,
|
||||
mediumPictureKeyName,
|
||||
playListConfiguration.PicturesSizes.MediumPictureWidth,
|
||||
playListConfiguration.PicturesSizes.MediumPictureHeight,
|
||||
largePictureKeyName,
|
||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||
playListConfiguration.PicturesSizes.LargePictureHeight), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish resize picture event for PlayList with id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish resize picture event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("No picture provided for PlayList with Id={PlayListId}. Using Preset pictures.", playList.Id);
|
||||
}
|
||||
|
||||
await database.PlayLists.AddAsync(playList, cancellationToken);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("PlayList with id={PlayListId} created successfully for user with id={UserId}.", playList.Id, request.UserId);
|
||||
return Result.Created(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Application.PlayLists.Commands;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class DeletePlayListCommandHandler(IDatabase database, IEventBus eventBus,
|
||||
ApplicationStorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||
: IRequestHandler<DeletePlayListCommand, Task<Result>>
|
||||
{
|
||||
public async Task<Result> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var playList = await database.PlayLists.FindAsync(request.PlayListId, cancellationToken);
|
||||
|
||||
if (playList is null)
|
||||
{
|
||||
logger.LogDebug("PlayList with id={PlayListId} not found", request.PlayListId);
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
if (playList.UserId != request.UserId)
|
||||
{
|
||||
logger.LogInformation("PlayList with id={PlayListId} does not belong to user with id={UserId}", request.PlayListId, request.UserId);
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
|
||||
var keysToRemove = new List<string>();
|
||||
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
{
|
||||
keysToRemove.Add(playList.SmallPictureKeyName);
|
||||
}
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
{
|
||||
keysToRemove.Add(playList.MediumPictureKeyName);
|
||||
}
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
{
|
||||
keysToRemove.Add(playList.LargePictureKeyName);
|
||||
}
|
||||
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
var publishResult = await TryPublishRemoveFileEventAsync(playList.Id, key, cancellationToken);
|
||||
|
||||
if (!publishResult.IsSuccess)
|
||||
{
|
||||
return publishResult;
|
||||
}
|
||||
}
|
||||
|
||||
database.PlayLists.Remove(playList);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
return Result.NoContent();
|
||||
}
|
||||
|
||||
async Task<Result> TryPublishRemoveFileEventAsync(Guid playListId, string keyName, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
keyName), cancellationToken);
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for PlayList id={PlayListId} and key={KeyName}.", playListId, keyName);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playListId}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.PlayLists.Responses;
|
||||
using Musify.Application.PlayLists.Queries;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class GetPlayListByIdQueryHandler(IDatabase database)
|
||||
: IRequestHandler<GetPlayListByIdQuery, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(GetPlayListByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var playList = await database.PlayLists
|
||||
.AsNoTracking()
|
||||
.SingleOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
||||
|
||||
return playList is not null
|
||||
? Result.Success(PlayListResponse.FromEntity(playList))
|
||||
: Result.NotFound("Playlist not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Musify.Application.Contracts.Application;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.PlayLists.Responses;
|
||||
using Musify.Application.PlayLists.Queries;
|
||||
using X.PagedList.EF;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class GetPlayListsByUserIdQueryHandler(IDatabase database)
|
||||
: IRequestHandler<GetPlayListsByUserIdQuery, Task<Result<PaginatedResponse<PlayListResponse> > >>
|
||||
{
|
||||
public async Task<Result<PaginatedResponse<PlayListResponse> >> Handle(GetPlayListsByUserIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var pageNumber = request.PageNumber < 1 ? 1 : request.PageNumber;
|
||||
var pageSize = request.PageSize < 1 ? 10 : request.PageSize;
|
||||
var normalizedName = request.Name?.Trim().ToUpperInvariant() ?? string.Empty;
|
||||
|
||||
var playListsQuery = database.PlayLists
|
||||
.AsNoTracking()
|
||||
.Where(p => p.UserId == request.UserId);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(normalizedName))
|
||||
playListsQuery = playListsQuery.Where(p => p.NormalizedName.Contains(normalizedName));
|
||||
|
||||
var totalCount = await playListsQuery.CountAsync(cancellationToken);
|
||||
|
||||
var pagedPlayLists = await playListsQuery
|
||||
.OrderBy(p => p.CreatedDate)
|
||||
.Select(p => PlayListResponse.FromEntity(p))
|
||||
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
||||
|
||||
var response = new PaginatedResponse<PlayListResponse>(
|
||||
pagedPlayLists.ToArray(),
|
||||
pagedPlayLists.PageNumber,
|
||||
pagedPlayLists.PageSize,
|
||||
pagedPlayLists.PageCount,
|
||||
pagedPlayLists.TotalItemCount,
|
||||
pagedPlayLists.HasNextPage,
|
||||
pagedPlayLists.HasPreviousPage);
|
||||
|
||||
return Result.Success(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Musify.Application.Contracts.Application;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.PlayLists.Responses;
|
||||
using Musify.Application.PlayLists.Queries;
|
||||
using X.PagedList.EF;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class GetPlayListsQueryHandler(IDatabase database)
|
||||
: IRequestHandler<GetPlayListsQuery, Task<Result<PaginatedResponse<PlayListResponse> >> >
|
||||
{
|
||||
public async Task<Result<PaginatedResponse<PlayListResponse> >> Handle(GetPlayListsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var pageNumber = request.PageNumber < 1 ? 1 : request.PageNumber;
|
||||
var pageSize = request.PageSize < 1 ? 10 : request.PageSize;
|
||||
|
||||
var totalCount = await database.PlayLists.CountAsync(cancellationToken);
|
||||
|
||||
var pagedPlayLists = await database.PlayLists
|
||||
.AsNoTracking()
|
||||
.OrderBy(p => p.Id)
|
||||
.Select(p => PlayListResponse.FromEntity(p))
|
||||
.ToPagedListAsync(pageNumber, pageSize, totalCount, cancellationToken);
|
||||
|
||||
var response = new PaginatedResponse<PlayListResponse>(
|
||||
pagedPlayLists.ToArray(),
|
||||
pagedPlayLists.PageNumber,
|
||||
pagedPlayLists.PageSize,
|
||||
pagedPlayLists.PageCount,
|
||||
pagedPlayLists.TotalItemCount,
|
||||
pagedPlayLists.HasNextPage,
|
||||
pagedPlayLists.HasPreviousPage);
|
||||
|
||||
return Result.Success(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Application.PlayLists.Commands;
|
||||
using Musify.Application.PlayLists.Responses;
|
||||
|
||||
namespace Musify.Application.PlayLists.Handlers
|
||||
{
|
||||
public class UpdatePlayListCommandHandler(IEventBus eventBus, IDatabase database, IStorageHandler storageHandler,
|
||||
ILogger<UpdatePlayListCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration,
|
||||
PlayListConfiguration playListConfiguration, IPictureHandler pictureHandler)
|
||||
: IRequestHandler<UpdatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(UpdatePlayListCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var playList = await database.PlayLists.SingleOrDefaultAsync(pl => pl.Id == request.PlayListId, cancellationToken);
|
||||
|
||||
if (playList is null)
|
||||
{
|
||||
logger.LogInformation("PlayList with id {PlayListId} not found", request.PlayListId);
|
||||
return Result.NotFound();
|
||||
}
|
||||
|
||||
if (playList.UserId != request.UserId)
|
||||
{
|
||||
logger.LogWarning("User with id {UserId} is not the owner of the PlayList with id {PlayListId}", request.UserId, request.PlayListId);
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.NewName))
|
||||
{
|
||||
playList.Name = request.NewName;
|
||||
playList.NormalizedName = request.NewName.Trim().ToUpperInvariant();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.NewDescription))
|
||||
playList.Description = request.NewDescription;
|
||||
|
||||
if (request.NewPicture is null)
|
||||
{
|
||||
database.PlayLists.Update(playList);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId} without picture changes.", playList.Id);
|
||||
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.SmallPictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for small picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
}
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.MediumPictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for medium picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
}
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.LargePictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for large picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
}
|
||||
|
||||
var newImageId = Guid.NewGuid();
|
||||
var pictureName = $"{newImageId}{request.NewPicture.FileType}";
|
||||
var newImageStorageKey = Path.Join(playListConfiguration.Routes.OriginalPictures, pictureName);
|
||||
|
||||
var uploadResult = await storageHandler.UploadFileAsync(
|
||||
request.NewPicture.FileStream,
|
||||
request.NewPicture.ContentType,
|
||||
storageConfiguration.BucketName,
|
||||
newImageStorageKey,
|
||||
cancellationToken);
|
||||
|
||||
if (!uploadResult.IsSuccess)
|
||||
{
|
||||
logger.LogError("Failed to upload picture for PlayList with Id={PlayListId} to storage. Storage handler returned error: {ErrorMessage}",
|
||||
playList.Id, string.Join("; ", uploadResult.Errors));
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
|
||||
var smallPictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.SmallPictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
var mediumPictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.MediumPictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
var largePictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.LargePictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new UpdatePlayListPictureEvent(
|
||||
playList.Id,
|
||||
storageConfiguration.BucketName,
|
||||
newImageStorageKey,
|
||||
smallPictureKeyName,
|
||||
playListConfiguration.PicturesSizes.SmallPictureWidth,
|
||||
playListConfiguration.PicturesSizes.SmallPictureHeight,
|
||||
mediumPictureKeyName,
|
||||
playListConfiguration.PicturesSizes.MediumPictureWidth,
|
||||
playListConfiguration.PicturesSizes.MediumPictureHeight,
|
||||
largePictureKeyName,
|
||||
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||
playListConfiguration.PicturesSizes.LargePictureHeight), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish resize picture event for PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish resize picture event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
|
||||
database.PlayLists.Update(playList);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId} and picture key {PictureKeyName}.", playList.Id, newImageStorageKey);
|
||||
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user