60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using ChatApi;
|
|
using ChatRAG.Models;
|
|
using ChatRAG.Settings.ChatRAG.Configuration;
|
|
using Microsoft.Extensions.Options;
|
|
using MongoDB.Driver;
|
|
|
|
namespace ChatRAG.Data
|
|
{
|
|
public class ProjectDataRepository
|
|
{
|
|
private readonly IMongoCollection<Project> _textsCollection;
|
|
|
|
public ProjectDataRepository(
|
|
IOptions<VectorDatabaseSettings> databaseSettings)
|
|
{
|
|
var mongoClient = new MongoClient(
|
|
databaseSettings.Value.MongoDB.ConnectionString);
|
|
|
|
var mongoDatabase = mongoClient.GetDatabase(
|
|
databaseSettings.Value.MongoDB.DatabaseName);
|
|
|
|
_textsCollection = mongoDatabase.GetCollection<Project>(
|
|
databaseSettings.Value.MongoDB.ProjectCollectionName);
|
|
}
|
|
|
|
public async Task<List<Project>> GetAsync() =>
|
|
await _textsCollection.Find(_ => true).ToListAsync();
|
|
|
|
public async Task<Project?> GetAsync(string id) =>
|
|
await _textsCollection.Find(x => x.Id == id).FirstOrDefaultAsync();
|
|
|
|
public async Task CreateAsync(Project newProject) =>
|
|
await _textsCollection.InsertOneAsync(newProject);
|
|
|
|
public async Task UpdateAsync(string id, Project updatedProject) =>
|
|
await _textsCollection.ReplaceOneAsync(x => x.Id == id, updatedProject);
|
|
|
|
public async Task SaveAsync(Project project)
|
|
{
|
|
Project projectExist = null;
|
|
if (project.Id != null)
|
|
{
|
|
projectExist = await _textsCollection.Find(x => x.Id == project.Id).FirstOrDefaultAsync();
|
|
}
|
|
|
|
if (projectExist == null)
|
|
{
|
|
await _textsCollection.InsertOneAsync(project);
|
|
}
|
|
else
|
|
{
|
|
await _textsCollection.ReplaceOneAsync(x => x.Id == project.Id, project);
|
|
}
|
|
}
|
|
|
|
public async Task RemoveAsync(string id) =>
|
|
await _textsCollection.DeleteOneAsync(x => x.Id == id);
|
|
}
|
|
}
|