47 lines
2.0 KiB
C#
47 lines
2.0 KiB
C#
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;
|
|
}
|
|
} |