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 { private readonly ILogger _logger; private readonly NovelServiceDbContext _dbContext; private readonly NovelUpdateService _novelUpdateService; public FileUploadRequestStatusUpdateConsumer( ILogger logger, NovelServiceDbContext dbContext, NovelUpdateService novelUpdateService) { _logger = logger; _dbContext = dbContext; _novelUpdateService = novelUpdateService; } public async Task Consume(ConsumeContext 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); } } }