Update playlist and some changes
This commit is contained in:
@@ -24,9 +24,4 @@
|
|||||||
<ProjectReference Include="..\Domain\Domain.csproj" />
|
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Contracts\Application\" />
|
|
||||||
<Folder Include="PlayLists\Commands\UpdatePlayList\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Musify.Application.Contracts.Application
|
||||||
|
{
|
||||||
|
public interface IFileData
|
||||||
|
{
|
||||||
|
Stream FileStream { get; }
|
||||||
|
|
||||||
|
string FileName { get; }
|
||||||
|
|
||||||
|
string FileType { get; }
|
||||||
|
|
||||||
|
string ContentType { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using DispatchR.Abstractions.Send;
|
using DispatchR.Abstractions.Send;
|
||||||
|
using Musify.Application.Contracts.Application;
|
||||||
using Musify.Application.PlayLists.Contracts;
|
using Musify.Application.PlayLists.Contracts;
|
||||||
|
|
||||||
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||||
@@ -8,8 +9,6 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
Guid UserId,
|
Guid UserId,
|
||||||
string Name,
|
string Name,
|
||||||
string Description,
|
string Description,
|
||||||
Stream PictureStream,
|
IFileData? Picture)
|
||||||
string PictureFileType,
|
|
||||||
string PictureContentType)
|
|
||||||
: IRequest<CreatePlayListCommand, Task<Result<PlayListResponse>> >;
|
: IRequest<CreatePlayListCommand, Task<Result<PlayListResponse>> >;
|
||||||
}
|
}
|
||||||
@@ -33,18 +33,19 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
Name = request.Name,
|
Name = request.Name,
|
||||||
NormalizedName = request.Name.Trim().ToUpperInvariant(),
|
NormalizedName = request.Name.Trim().ToUpperInvariant(),
|
||||||
Description = request.Description,
|
Description = request.Description,
|
||||||
OriginalPictureKeyName = $"{imageId}{request.PictureFileType}",
|
|
||||||
SmallPictureKeyName = playListConfiguration.Routes.PresetSmallPicture,
|
SmallPictureKeyName = playListConfiguration.Routes.PresetSmallPicture,
|
||||||
MediumPictureKeyName = playListConfiguration.Routes.PresetMediumPicture,
|
MediumPictureKeyName = playListConfiguration.Routes.PresetMediumPicture,
|
||||||
LargePictureKeyName = playListConfiguration.Routes.PresetLargePicture,
|
LargePictureKeyName = playListConfiguration.Routes.PresetLargePicture,
|
||||||
};
|
};
|
||||||
await database.PlayLists.AddAsync(playList, cancellationToken);
|
|
||||||
|
|
||||||
var imageKeyName = $"{playListConfiguration.Routes.OriginalPictures}/{imageId}{request.PictureFileType}";
|
if (request.Picture is not null)
|
||||||
|
{
|
||||||
|
playList.OriginalPictureKeyName = $"{imageId}{request.Picture.FileType}";
|
||||||
|
var imageKeyName = $"{playListConfiguration.Routes.OriginalPictures}/{imageId}{request.Picture.FileType}";
|
||||||
|
|
||||||
var uploadResult = await storageHandler.UploadFileAsync(
|
var uploadResult = await storageHandler.UploadFileAsync(
|
||||||
request.PictureStream,
|
request.Picture.FileStream,
|
||||||
request.PictureContentType,
|
request.Picture.ContentType,
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
imageKeyName,
|
imageKeyName,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
@@ -75,9 +76,16 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
|||||||
playListConfiguration.PicturesSizes.LargePictureHeight,
|
playListConfiguration.PicturesSizes.LargePictureHeight,
|
||||||
playListConfiguration.Routes.LargePictures)
|
playListConfiguration.Routes.LargePictures)
|
||||||
] ), cancellationToken);
|
] ), cancellationToken);
|
||||||
|
}
|
||||||
|
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("Picture for PlayList with Id={PlayListId} ", playList.Id);
|
logger.LogInformation("Picture for PlayList with Id={PlayListId} ", playList.Id);
|
||||||
|
|
||||||
return Result.Created(PlayListResponse.FromEntity(playList));
|
return Result.Created(PlayListResponse.FromEntity(playList));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
return Result.Unauthorized();
|
return Result.Unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
||||||
|
{
|
||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.OriginalPictureKeyName), cancellationToken);
|
playList.OriginalPictureKeyName), cancellationToken);
|
||||||
@@ -46,6 +48,7 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
storageConfiguration.BucketName,
|
storageConfiguration.BucketName,
|
||||||
playList.LargePictureKeyName), cancellationToken);
|
playList.LargePictureKeyName), cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
database.PlayLists.Remove(playList);
|
database.PlayLists.Remove(playList);
|
||||||
await database.SaveChangesAsync(cancellationToken);
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
using Musify.Application.Contracts.Application;
|
||||||
|
using Musify.Application.PlayLists.Contracts;
|
||||||
|
|
||||||
|
namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||||
|
{
|
||||||
|
public record UpdatePlayListCommand(
|
||||||
|
Guid UserId,
|
||||||
|
Guid PlayListId,
|
||||||
|
string? NewName,
|
||||||
|
string? NewDescription,
|
||||||
|
IFileData? NewPicture)
|
||||||
|
: IRequest<UpdatePlayListCommand, Task<Result<PlayListResponse>> >;
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
using Ardalis.Result;
|
||||||
|
using DispatchR.Abstractions.Send;
|
||||||
|
using MassTransit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Musify.Application.Configuration;
|
||||||
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
|
using Musify.Application.Events;
|
||||||
|
using Musify.Application.PlayLists.Contracts;
|
||||||
|
|
||||||
|
namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||||
|
{
|
||||||
|
public class UpdatePlayListCommandHandler(IPublishEndpoint publishEndpoint, IDatabase database, IStorageHandler storageHandler,
|
||||||
|
ILogger<UpdatePlayListCommandHandler> logger, StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||||
|
: 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;
|
||||||
|
|
||||||
|
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}.", playList.Id);
|
||||||
|
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
||||||
|
{
|
||||||
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
playList.OriginalPictureKeyName), cancellationToken);
|
||||||
|
logger.LogInformation("Published event to remove old picture with key name {KeyName} from bucket {BucketName}",
|
||||||
|
playList.OriginalPictureKeyName, storageConfiguration.BucketName);
|
||||||
|
|
||||||
|
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||||
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
playList.SmallPictureKeyName), cancellationToken);
|
||||||
|
|
||||||
|
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||||
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
playList.MediumPictureKeyName), cancellationToken);
|
||||||
|
|
||||||
|
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||||
|
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
playList.LargePictureKeyName), cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
var newImageId = Guid.NewGuid();
|
||||||
|
var imageKeyName = $"{playListConfiguration.Routes.OriginalPictures}/{newImageId}{request.NewPicture.FileType}";
|
||||||
|
|
||||||
|
var uploadResult = await storageHandler.UploadFileAsync(
|
||||||
|
request.NewPicture.FileStream,
|
||||||
|
request.NewPicture.ContentType,
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
imageKeyName,
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
playList.OriginalPictureKeyName = newImageId.ToString();
|
||||||
|
await publishEndpoint.Publish(new ResizePictureEvent(
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
imageKeyName,
|
||||||
|
[
|
||||||
|
new ResizePictureItems(
|
||||||
|
playListConfiguration.PicturesSizes.SmallPictureWidth,
|
||||||
|
playListConfiguration.PicturesSizes.SmallPictureHeight,
|
||||||
|
playListConfiguration.Routes.SmallPictures),
|
||||||
|
new ResizePictureItems(
|
||||||
|
playListConfiguration.PicturesSizes.MediumPictureWidth,
|
||||||
|
playListConfiguration.PicturesSizes.MediumPictureHeight,
|
||||||
|
playListConfiguration.Routes.MediumPictures),
|
||||||
|
new ResizePictureItems(
|
||||||
|
playListConfiguration.PicturesSizes.LargePictureWidth,
|
||||||
|
playListConfiguration.PicturesSizes.LargePictureHeight,
|
||||||
|
playListConfiguration.Routes.LargePictures)
|
||||||
|
]
|
||||||
|
), cancellationToken);
|
||||||
|
|
||||||
|
database.PlayLists.Update(playList);
|
||||||
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
logger.LogInformation("Updated PlayList with id={PlayListId}.", playList.Id);
|
||||||
|
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,9 +25,8 @@ namespace Musify.Domain.Entities
|
|||||||
[MaxLength(254)]
|
[MaxLength(254)]
|
||||||
public required string Description { get; set; } = "No description was provided.";
|
public required string Description { get; set; } = "No description was provided.";
|
||||||
|
|
||||||
[Required]
|
|
||||||
[MaxLength(64)]
|
[MaxLength(64)]
|
||||||
public required string OriginalPictureKeyName { get; set; }
|
public string? OriginalPictureKeyName { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
[MaxLength(64)]
|
[MaxLength(64)]
|
||||||
|
|||||||
Reference in New Issue
Block a user