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 _translationEngines; private readonly IEventBus _eventBus; private readonly TranslationServiceDbContext _dbContext; public TranslationEngineService(IEnumerable translationEngines, TranslationServiceDbContext dbContext, IEventBus eventBus) { _translationEngines = translationEngines; _dbContext = dbContext; _eventBus = eventBus; } public async Task 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; } }