[FA-misc] Mass transit overhaul, needs testing and review
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||
using MassTransit;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||
|
||||
public class FileUploadCompletedEventConsumer : IConsumer<FileUploadCompletedEvent>
|
||||
{
|
||||
private readonly ILogger<FileUploadCompletedEventConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public FileUploadCompletedEventConsumer(
|
||||
ILogger<FileUploadCompletedEventConsumer> logger,
|
||||
NovelServiceDbContext dbContext,
|
||||
NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<FileUploadCompletedEvent> context)
|
||||
{
|
||||
var @event = context.Message;
|
||||
|
||||
var image = await _dbContext.Images.FindAsync(@event.RequestId);
|
||||
if (image == null)
|
||||
{
|
||||
// Not a request we care about.
|
||||
_logger.LogDebug(
|
||||
"FileUploadCompletedEvent received for unknown image: {RequestId}",
|
||||
@event.RequestId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (@event.Status == RequestStatus.Failed)
|
||||
{
|
||||
_logger.LogError(
|
||||
"Image upload failed for image with id {ImageId}: {ErrorMessage}",
|
||||
image.Id, @event.ErrorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
if (@event.Status == RequestStatus.Success)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Image upload succeeded for image with id {ImageId}",
|
||||
image.Id);
|
||||
await _novelUpdateService.UpdateImage(image.Id, @event.FileAccessUrl!);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user