Remove files consumer
This commit is contained in:
@@ -25,20 +25,21 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
return Result.NotFound($"User with Id {request.UserId} does not exist.");
|
||||
}
|
||||
|
||||
var imageId = Guid.NewGuid();
|
||||
|
||||
var playList = new PlayList
|
||||
{
|
||||
UserId = request.UserId,
|
||||
Name = request.Name,
|
||||
NormalizedName = request.Name.Trim().ToUpperInvariant(),
|
||||
Description = request.Description,
|
||||
SmallPictureKeyName = playListConfiguration.Routes.SmallPictures,
|
||||
MediumPictureKeyName = playListConfiguration.Routes.MediumPictures,
|
||||
LargePictureKeyName = playListConfiguration.Routes.LargePictures,
|
||||
OriginalPictureKeyName = $"{imageId}/{request.PictureFileType}",
|
||||
SmallPictureKeyName = playListConfiguration.Routes.PresetSmallPicture,
|
||||
MediumPictureKeyName = playListConfiguration.Routes.PresetMediumPicture,
|
||||
LargePictureKeyName = playListConfiguration.Routes.PresetLargePicture,
|
||||
};
|
||||
await database.PlayLists.AddAsync(playList, cancellationToken);
|
||||
|
||||
var imageId = Guid.NewGuid();
|
||||
|
||||
var imageKeyName = $"{playListConfiguration.Routes.OriginalPictures}/{imageId}.{request.PictureFileType}";
|
||||
|
||||
var uploadResult = await storageHandler.UploadFileAsync(
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
|
||||
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||
{
|
||||
public class DeletePlayListCommandHandler(IDatabase database, IStorageHandler storageHandler,
|
||||
StorageConfiguration storageConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||
public class DeletePlayListCommandHandler(IDatabase database, IPublishEndpoint publishEndpoint,
|
||||
StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||
: IRequestHandler<DeletePlayListCommand, Task<Result>>
|
||||
{
|
||||
public async Task<Result> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||
@@ -26,11 +28,28 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
|
||||
await publishEndpoint.Publish(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.OriginalPictureKeyName), cancellationToken);
|
||||
|
||||
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);
|
||||
|
||||
database.PlayLists.Remove(playList);
|
||||
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Result.NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,10 @@ namespace Musify.Domain.Entities
|
||||
[MaxLength(254)]
|
||||
public required string Description { get; set; } = "No description was provided.";
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string OriginalPictureKeyName { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string SmallPictureKeyName { get; set; }
|
||||
|
||||
@@ -1,14 +1,50 @@
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Domain.Entities;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Consumers
|
||||
{
|
||||
public class RemoveFileConsumer
|
||||
public class RemoveFileConsumer(IDatabase database, IStorageHandler storageHandler, ILogger<RemoveFileConsumer> logger)
|
||||
: IConsumer<RemoveFileEvent>
|
||||
{
|
||||
public Task Consume(ConsumeContext<RemoveFileEvent> context)
|
||||
public const string QueueName = "file-remove-queue";
|
||||
|
||||
public async Task Consume(ConsumeContext<RemoveFileEvent> consumeContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var payload = JsonSerializer.Serialize(consumeContext.Message);
|
||||
|
||||
var job = await database.Jobs
|
||||
.SingleOrDefaultAsync(j => j.JobState == JobState.Failed && j.Payload == payload);
|
||||
|
||||
if (job is null)
|
||||
{
|
||||
job = new Job
|
||||
{
|
||||
JobType = JobType.RemoveFile,
|
||||
Payload = payload
|
||||
};
|
||||
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
job.RetryCount++;
|
||||
job.JobState = JobState.Pending;
|
||||
database.Jobs.Update(job);
|
||||
}
|
||||
|
||||
var removeFile = await storageHandler.RemoveFileAsync(
|
||||
consumeContext.Message.BucketName,
|
||||
consumeContext.Message.KeyName,
|
||||
consumeContext.CancellationToken);
|
||||
|
||||
if (!removeFile.IsSuccess)
|
||||
job.JobState = JobState.Failed;
|
||||
|
||||
await database.SaveChangesAsync(consumeContext.CancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ namespace Musify.Infrastructure.Messaging
|
||||
serviceDescriptors.AddMassTransit(options =>
|
||||
{
|
||||
options.AddConsumer<ResizePictureConsumer>();
|
||||
options.AddConsumer<RemoveFileConsumer>();
|
||||
|
||||
options.UsingRabbitMq((busRegistrationContext, busFactoryConfigurator) =>
|
||||
{
|
||||
@@ -62,6 +63,10 @@ namespace Musify.Infrastructure.Messaging
|
||||
{
|
||||
endpointConfigurator.ConfigureConsumer<ResizePictureConsumer>(busRegistrationContext);
|
||||
} );
|
||||
busFactoryConfigurator.ReceiveEndpoint(RemoveFileConsumer.QueueName, endpointConfigurator =>
|
||||
{
|
||||
endpointConfigurator.ConfigureConsumer<RemoveFileConsumer>(busRegistrationContext);
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user