[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,47 @@
|
||||
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)
|
||||
{
|
||||
_translationEngines = translationEngines;
|
||||
_dbContext = dbContext;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey)
|
||||
{
|
||||
var engine = _translationEngines.FirstOrDefault(engine => engine.Descriptor.Key == translationEngineKey);
|
||||
var translation = await engine.GetTranslation(text, from, to);
|
||||
|
||||
_dbContext.TranslationRequests.Add(new TranslationRequest()
|
||||
{
|
||||
OriginalText = text,
|
||||
BilledCharacterCount = translation.BilledCharacterCount, // FILL ME
|
||||
From = from,
|
||||
To = to,
|
||||
Status = translation != null ? TranslationRequestStatus.Success : TranslationRequestStatus.Failed,
|
||||
TranslatedText = translation.TranslatedText,
|
||||
TranslationEngineKey = translationEngineKey
|
||||
});
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return translation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user