ChatRAG/Data/MongoTextDataService.cs
2025-06-15 21:34:47 -03:00

179 lines
5.9 KiB
C#

using ChatApi.Data;
using ChatRAG.Models;
using ChatRAG.Services.Contracts;
using Microsoft.SemanticKernel.Embeddings;
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
namespace ChatRAG.Data
{
public class MongoTextDataService : ITextDataService
{
private readonly TextData _textData; // Sua classe existente!
private readonly ITextEmbeddingGenerationService _embeddingService;
public MongoTextDataService(
TextData textData,
ITextEmbeddingGenerationService embeddingService)
{
_textData = textData;
_embeddingService = embeddingService;
}
public string ProviderName => "MongoDB";
// ========================================
// MÉTODOS ORIGINAIS - Delega para TextData
// ========================================
public async Task SalvarNoMongoDB(string titulo, string texto, string projectId)
{
await _textData.SalvarNoMongoDB(titulo, texto, projectId);
}
public async Task SalvarNoMongoDB(string? id, string titulo, string texto, string projectId)
{
await _textData.SalvarNoMongoDB(id, titulo, texto, projectId);
}
public async Task SalvarTextoComEmbeddingNoMongoDB(string textoCompleto, string projectId)
{
await _textData.SalvarTextoComEmbeddingNoMongoDB(textoCompleto, projectId);
}
public async Task<IEnumerable<TextoComEmbedding>> GetAll()
{
return await _textData.GetAll();
}
public async Task<IEnumerable<TextoComEmbedding>> GetByPorjectId(string projectId)
{
return await _textData.GetByPorjectId(projectId);
}
public async Task<TextoComEmbedding> GetById(string id)
{
return await _textData.GetById(id);
}
// ========================================
// NOVOS MÉTODOS UNIFICADOS
// ========================================
public async Task<string> SaveDocumentAsync(DocumentInput document)
{
var id = document.Id ?? Guid.NewGuid().ToString();
await _textData.SalvarNoMongoDB(id, document.Title, document.Content, document.ProjectId);
return id;
}
public async Task UpdateDocumentAsync(string id, DocumentInput document)
{
await _textData.SalvarNoMongoDB(id, document.Title, document.Content, document.ProjectId);
}
public async Task DeleteDocumentAsync(string id)
{
// Implementar quando necessário ou usar TextDataRepository diretamente
throw new NotImplementedException("Delete será implementado quando migrar para interface");
}
public async Task<bool> DocumentExistsAsync(string id)
{
try
{
var doc = await _textData.GetById(id);
return doc != null;
}
catch
{
return false;
}
}
public async Task<DocumentOutput?> GetDocumentAsync(string id)
{
try
{
var doc = await _textData.GetById(id);
if (doc == null) return null;
return new DocumentOutput
{
Id = doc.Id,
Title = doc.Titulo,
Content = doc.Conteudo,
ProjectId = doc.ProjetoId,
Embedding = doc.Embedding,
CreatedAt = DateTime.UtcNow, // MongoDB não tem essa info no modelo atual
UpdatedAt = DateTime.UtcNow
};
}
catch
{
return null;
}
}
public async Task<List<DocumentOutput>> GetDocumentsByProjectAsync(string projectId)
{
var docs = await _textData.GetByPorjectId(projectId);
return docs.Select(doc => new DocumentOutput
{
Id = doc.Id,
Title = doc.Titulo,
Content = doc.Conteudo,
ProjectId = doc.ProjetoId,
Embedding = doc.Embedding,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}).ToList();
}
public async Task<int> GetDocumentCountAsync(string? projectId = null)
{
if (string.IsNullOrEmpty(projectId))
{
var all = await _textData.GetAll();
return all.Count();
}
else
{
var byProject = await _textData.GetByPorjectId(projectId);
return byProject.Count();
}
}
public async Task<List<string>> SaveDocumentsBatchAsync(List<DocumentInput> documents)
{
var ids = new List<string>();
foreach (var doc in documents)
{
var id = await SaveDocumentAsync(doc);
ids.Add(id);
}
return ids;
}
public async Task DeleteDocumentsBatchAsync(List<string> ids)
{
foreach (var id in ids)
{
await DeleteDocumentAsync(id);
}
}
public async Task<Dictionary<string, object>> GetProviderStatsAsync()
{
var totalDocs = await GetDocumentCountAsync();
return new Dictionary<string, object>
{
["provider"] = "MongoDB",
["total_documents"] = totalDocs,
["health"] = "ok",
["last_check"] = DateTime.UtcNow
};
}
}
}
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.