48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using FictionArchive.Common.Enums;
|
|
using FictionArchive.Service.NovelService.Services;
|
|
using FictionArchive.Service.Shared.Contracts.Events;
|
|
using MassTransit;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace FictionArchive.Service.NovelService.Consumers;
|
|
|
|
public class FileUploadRequestStatusUpdateConsumer : IConsumer<IFileUploadRequestStatusUpdate>
|
|
{
|
|
private readonly ILogger<FileUploadRequestStatusUpdateConsumer> _logger;
|
|
private readonly NovelServiceDbContext _dbContext;
|
|
private readonly NovelUpdateService _novelUpdateService;
|
|
|
|
public FileUploadRequestStatusUpdateConsumer(
|
|
ILogger<FileUploadRequestStatusUpdateConsumer> logger,
|
|
NovelServiceDbContext dbContext,
|
|
NovelUpdateService novelUpdateService)
|
|
{
|
|
_logger = logger;
|
|
_dbContext = dbContext;
|
|
_novelUpdateService = novelUpdateService;
|
|
}
|
|
|
|
public async Task Consume(ConsumeContext<IFileUploadRequestStatusUpdate> context)
|
|
{
|
|
var message = context.Message;
|
|
|
|
var image = await _dbContext.Images.FindAsync(message.RequestId);
|
|
if (image == null)
|
|
{
|
|
// Not a request we care about.
|
|
return;
|
|
}
|
|
|
|
if (message.Status == RequestStatus.Failed)
|
|
{
|
|
_logger.LogError("Image upload failed for image with id {imageId}", image.Id);
|
|
return;
|
|
}
|
|
else if (message.Status == RequestStatus.Success)
|
|
{
|
|
_logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id);
|
|
await _novelUpdateService.UpdateImage(image.Id, message.FileAccessUrl);
|
|
}
|
|
}
|
|
}
|