[FA-4] Adds an event bus infrastructure, a RabbitMQ implementation and rewires existing mutations on NovelService to utilize it.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class ChapterPullRequestedEventHandler : IIntegrationEventHandler<ChapterPullRequestedEvent>
|
||||
{
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public ChapterPullRequestedEventHandler(NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Handle(ChapterPullRequestedEvent @event)
|
||||
{
|
||||
await _novelUpdateService.PullChapterContents(@event.NovelId, @event.ChapterNumber);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class NovelUpdateRequestedEventHandler : IIntegrationEventHandler<NovelUpdateRequestedEvent>
|
||||
{
|
||||
private readonly ILogger<NovelUpdateRequestedEventHandler> _logger;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly NovelUpdateService _novelUpdateService;
|
||||
|
||||
public NovelUpdateRequestedEventHandler(ILogger<NovelUpdateRequestedEventHandler> logger, IEventBus eventBus, NovelUpdateService novelUpdateService)
|
||||
{
|
||||
_logger = logger;
|
||||
_eventBus = eventBus;
|
||||
_novelUpdateService = novelUpdateService;
|
||||
}
|
||||
|
||||
public async Task Handle(NovelUpdateRequestedEvent @event)
|
||||
{
|
||||
await _novelUpdateService.ImportNovel(@event.NovelUrl);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using FictionArchive.Service.NovelService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.EventHandlers;
|
||||
|
||||
public class TranslationRequestCompletedEventHandler : IIntegrationEventHandler<TranslationRequestCompletedEvent>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCompletedEventHandler> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationRequestCompletedEventHandler(ILogger<TranslationRequestCompletedEventHandler> logger, NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Handle(TranslationRequestCompletedEvent @event)
|
||||
{
|
||||
var localizationRequest = await _dbContext.LocalizationRequests.Include(r => r.KeyRequestedForTranslation)
|
||||
.ThenInclude(lk => lk.Texts)
|
||||
.FirstOrDefaultAsync(lk => lk.Id == @event.TranslationRequestId);
|
||||
if (localizationRequest == null)
|
||||
{
|
||||
// Not one of our requests, discard it
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText()
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = @event.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user