BotSharp/src/Plugins/BotSharp.Plugin.MongoRepository/Repository/MongoRepository.cs

265 lines
9.4 KiB
C#
Raw Normal View History

2023-08-28 05:28:14 +00:00
using BotSharp.Plugin.Mongo.Collections;
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
namespace BotSharp.Plugin.Mongo.Repository;
public class MongoRepository : IBotSharpRepository
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
private readonly MongoDbContext _dc;
private readonly IServiceProvider _services;
private UpdateOptions _options;
public MongoRepository(MongoDbContext dc, IServiceProvider services)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
_dc = dc;
_services = services;
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
_options = new UpdateOptions
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
IsUpsert = true,
};
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
private List<AgentRecord> _agents;
public IQueryable<AgentRecord> Agent
{
get
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
if (_agents != null)
2023-08-26 04:41:01 +00:00
{
return _agents.AsQueryable();
}
2023-08-28 05:28:14 +00:00
var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List<AgentCollection>();
_agents = agentDocs.Select(x => new AgentRecord
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id?.ToString(),
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
Routes = x.Routes,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _agents.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
private List<UserRecord> _users;
public IQueryable<UserRecord> User
{
get
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
if (_users != null)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _users.AsQueryable();
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List<UserCollection>();
_users = userDocs.Select(x => new UserRecord
{
Id = x.Id?.ToString(),
FirstName = x.FirstName,
LastName = x.LastName,
Email = x.Email,
Password = x.Password,
Salt = x.Salt,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _users.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
private List<UserAgentRecord> _userAgents;
public IQueryable<UserAgentRecord> UserAgent
{
get
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
if (_userAgents != null && _userAgents.Count > 0)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _userAgents.AsQueryable();
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List<UserAgentCollection>();
_userAgents = userDocs.Select(x => new UserAgentRecord
{
Id = x.Id?.ToString(),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
return _userAgents.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
private List<ConversationRecord> _conversations;
public IQueryable<ConversationRecord> Conversation
{
get
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
if (_conversations != null)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
return _conversations.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
_conversations = conversationDocs.Select(x => new ConversationRecord
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id?.ToString(),
AgentId = x.AgentId,
UserId = x.UserId,
Title = x.Title,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _conversations.AsQueryable();
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
if (entity is ConversationRecord conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(ConversationRecord));
}
else if (entity is AgentRecord agent)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
_agents.Add(agent);
_changedTableNames.Add(nameof(AgentRecord));
}
else if (entity is UserRecord user)
{
_users.Add(user);
_changedTableNames.Add(nameof(UserRecord));
}
else if (entity is UserAgentRecord userAgent)
{
_userAgents.Add(userAgent);
_changedTableNames.Add(nameof(UserAgentRecord));
}
}
public int Transaction<TTableInterface>(Action action)
{
_changedTableNames.Clear();
action();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var table in _changedTableNames)
{
if (table == nameof(ConversationRecord))
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
var conversations = _conversations.Select(x => new ConversationCollection
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var conversation in conversations)
{
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.Update
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
else if (table == nameof(AgentRecord))
{
var agents = _agents.Select(x => new AgentCollection
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
Routes = x.Routes,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var agent in agents)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, agent.Id);
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Routes, agent.Routes)
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
else if (table == nameof(UserRecord))
{
var users = _users.Select(x => new UserCollection
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
FirstName = x.FirstName,
LastName = x.LastName,
Salt = x.Salt,
Password = x.Password,
Email = x.Email,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var user in users)
{
var filter = Builders<UserCollection>.Filter.Eq(x => x.Id, user.Id);
var update = Builders<UserCollection>.Update
.Set(x => x.FirstName, user.FirstName)
.Set(x => x.LastName, user.LastName)
.Set(x => x.Email, user.Email)
.Set(x => x.Salt, user.Salt)
.Set(x => x.Password, user.Password)
.Set(x => x.ExternalId, user.ExternalId)
.Set(x => x.CreatedTime, user.CreatedTime)
.Set(x => x.UpdatedTime, user.UpdatedTime);
_dc.Users.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
2023-08-28 05:28:14 +00:00
}
else if (table == nameof(UserAgentRecord))
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
2023-08-26 04:41:01 +00:00
2023-08-28 05:28:14 +00:00
foreach (var userAgent in userAgents)
{
var filter = Builders<UserAgentCollection>.Filter.Eq(x => x.Id, userAgent.Id);
var update = Builders<UserAgentCollection>.Update
.Set(x => x.AgentId, userAgent.AgentId)
.Set(x => x.UserId, userAgent.UserId)
.Set(x => x.CreatedTime, userAgent.CreatedTime)
.Set(x => x.UpdatedTime, userAgent.UpdatedTime);
_dc.UserAgents.UpdateOne(filter, update, _options);
2023-08-26 04:41:01 +00:00
}
}
}
2023-08-28 05:28:14 +00:00
return _changedTableNames.Count;
2023-08-26 04:41:01 +00:00
}
}