[FA-misc] Mass transit overhaul, needs testing and review

This commit is contained in:
gamer147
2026-01-21 23:16:31 -05:00
parent 055ef33666
commit f88f340d0a
97 changed files with 1150 additions and 858 deletions

View File

@@ -0,0 +1,40 @@
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.TranslationService.Models.Enums;
using MassTransit;
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
public class TranslateTextCommandConsumer : IConsumer<TranslateTextCommand>
{
private readonly ILogger<TranslateTextCommandConsumer> _logger;
private readonly TranslationEngineService _translationEngineService;
public TranslateTextCommandConsumer(
ILogger<TranslateTextCommandConsumer> logger,
TranslationEngineService translationEngineService)
{
_logger = logger;
_translationEngineService = translationEngineService;
}
public async Task Consume(ConsumeContext<TranslateTextCommand> context)
{
var command = context.Message;
var result = await _translationEngineService.Translate(
command.From,
command.To,
command.Body,
command.TranslationEngineKey);
if (result.Status == TranslationRequestStatus.Success)
{
await context.Publish(new TranslationCompletedEvent
{
TranslatedText = result.TranslatedText!,
TranslationRequestId = command.TranslationRequestId,
});
}
}
}

View File

@@ -1,31 +0,0 @@
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.TranslationService.Models.Enums;
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
public class TranslationRequestCreatedEventHandler : IIntegrationEventHandler<TranslationRequestCreatedEvent>
{
private readonly ILogger<TranslationRequestCreatedEventHandler> _logger;
private readonly TranslationEngineService _translationEngineService;
private readonly IEventBus _eventBus;
public TranslationRequestCreatedEventHandler(ILogger<TranslationRequestCreatedEventHandler> logger, TranslationEngineService translationEngineService)
{
_logger = logger;
_translationEngineService = translationEngineService;
}
public async Task Handle(TranslationRequestCreatedEvent @event)
{
var result = await _translationEngineService.Translate(@event.From, @event.To, @event.Body, @event.TranslationEngineKey);
if (result.Status == TranslationRequestStatus.Success)
{
await _eventBus.Publish(new TranslationRequestCompletedEvent()
{
TranslatedText = result.TranslatedText,
TranslationRequestId = @event.TranslationRequestId,
});
}
}
}

View File

@@ -1,28 +1,21 @@
using System.Text;
using FictionArchive.Common.Enums;
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
using FictionArchive.Service.TranslationService.Models;
using FictionArchive.Service.TranslationService.Models.Database;
using FictionArchive.Service.TranslationService.Models.Enums;
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
using FictionArchive.Service.TranslationService.Services.Database;
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
using RabbitMQ.Client;
namespace FictionArchive.Service.TranslationService.Services;
public class TranslationEngineService
{
private readonly IEnumerable<ITranslationEngine> _translationEngines;
private readonly IEventBus _eventBus;
private readonly TranslationServiceDbContext _dbContext;
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext, IEventBus eventBus)
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext)
{
_translationEngines = translationEngines;
_dbContext = dbContext;
_eventBus = eventBus;
}
public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey)