using FictionArchive.Common.Enums; using FictionArchive.Service.NovelService.Models.IntegrationEvents; using FictionArchive.Service.Shared.Services.EventBus; namespace FictionArchive.Service.NovelService.Services.EventHandlers; public class FileUploadRequestStatusUpdateEventHandler : IIntegrationEventHandler { private readonly ILogger _logger; private readonly NovelServiceDbContext _context; private readonly NovelUpdateService _novelUpdateService; public FileUploadRequestStatusUpdateEventHandler(ILogger logger, NovelServiceDbContext context, NovelUpdateService novelUpdateService) { _logger = logger; _context = context; _novelUpdateService = novelUpdateService; } public async Task Handle(FileUploadRequestStatusUpdateEvent @event) { var image = await _context.Images.FindAsync(@event.RequestId); if (image == null) { // Not a request we care about. return; } if (@event.Status == RequestStatus.Failed) { _logger.LogError("Image upload failed for image with id {imageId}", image.Id); return; } else if (@event.Status == RequestStatus.Success) { _logger.LogInformation("Image upload succeeded for image with id {imageId}", image.Id); await _novelUpdateService.UpdateImage(image.Id, @event.FileAccessUrl); } } }