[FA-misc] Mass transit overhaul, needs testing and review
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using FictionArchive.Service.NovelService.Models.Localization;
|
||||
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.NovelService.Services.Consumers;
|
||||
|
||||
public class TranslationCompletedEventConsumer : IConsumer<TranslationCompletedEvent>
|
||||
{
|
||||
private readonly ILogger<TranslationCompletedEventConsumer> _logger;
|
||||
private readonly NovelServiceDbContext _dbContext;
|
||||
|
||||
public TranslationCompletedEventConsumer(
|
||||
ILogger<TranslationCompletedEventConsumer> logger,
|
||||
NovelServiceDbContext dbContext)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<TranslationCompletedEvent> context)
|
||||
{
|
||||
var @event = context.Message;
|
||||
|
||||
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
|
||||
_logger.LogDebug(
|
||||
"TranslationCompletedEvent received for unknown request: {RequestId}",
|
||||
@event.TranslationRequestId);
|
||||
return;
|
||||
}
|
||||
|
||||
localizationRequest.KeyRequestedForTranslation.Texts.Add(new LocalizationText()
|
||||
{
|
||||
Language = localizationRequest.TranslateTo,
|
||||
Text = @event.TranslatedText,
|
||||
TranslationEngine = localizationRequest.Engine
|
||||
});
|
||||
_dbContext.LocalizationRequests.Remove(localizationRequest);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation(
|
||||
"Completed translation for request: {RequestId}",
|
||||
@event.TranslationRequestId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user