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

599 lines
21 KiB
C#
Raw Normal View History

2023-09-03 22:43:50 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Repositories.Models;
2023-09-07 22:04:34 +00:00
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
2023-09-01 22:33:36 +00:00
using BotSharp.Plugin.MongoStorage.Collections;
2023-08-26 04:41:01 +00:00
2023-09-01 22:33:36 +00:00
namespace BotSharp.Plugin.MongoStorage.Repository;
2023-08-28 05:28:14 +00:00
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;
_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-09-07 22:04:34 +00:00
private List<Agent> _agents;
public IQueryable<Agent> Agents
2023-08-28 05:28:14 +00:00
{
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>();
2023-09-07 22:04:34 +00:00
_agents = agentDocs.Select(x => new Agent
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
2023-08-28 05:28:14 +00:00
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
2023-09-02 05:10:46 +00:00
Responses = x.Responses,
2023-09-04 15:15:11 +00:00
IsPublic = x.IsPublic,
2023-08-28 05:28:14 +00:00
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-09-07 22:04:34 +00:00
private List<User> _users;
public IQueryable<User> Users
2023-08-28 05:28:14 +00:00
{
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>();
2023-09-07 22:04:34 +00:00
_users = userDocs.Select(x => new User
2023-08-28 05:28:14 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
2023-08-28 05:28:14 +00:00
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-09-07 22:04:34 +00:00
private List<UserAgent> _userAgents;
public IQueryable<UserAgent> UserAgents
2023-08-28 05:28:14 +00:00
{
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>();
2023-09-07 22:04:34 +00:00
_userAgents = userDocs.Select(x => new UserAgent
2023-08-28 05:28:14 +00:00
{
2023-09-02 05:10:46 +00:00
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
UserId = x.UserId.ToString(),
2023-08-28 05:28:14 +00:00
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-09-07 22:04:34 +00:00
private List<Conversation> _conversations;
public IQueryable<Conversation> Conversations
2023-08-28 05:28:14 +00:00
{
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
2023-09-07 22:04:34 +00:00
_conversations = new List<Conversation>();
2023-08-28 05:28:14 +00:00
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
2023-09-06 04:41:49 +00:00
foreach (var conv in conversationDocs)
2023-08-26 04:41:01 +00:00
{
2023-09-06 04:41:49 +00:00
var convId = conv.Id.ToString();
var dialog = GetConversationDialog(convId);
2023-09-07 22:04:34 +00:00
var states = GetConversationStates(convId);
_conversations.Add(new Conversation
2023-09-06 04:41:49 +00:00
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog,
2023-09-07 22:04:34 +00:00
//State = states, to do
2023-09-06 04:41:49 +00:00
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});
}
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
}
2023-08-26 04:41:01 +00:00
2023-09-07 22:04:34 +00:00
private List<RoutingItem> _routingItems;
public IQueryable<RoutingItem> RoutingItems
2023-09-02 22:41:58 +00:00
{
get
{
if (_routingItems != null)
{
return _routingItems.AsQueryable();
}
var routingItemDocs = _dc.RoutingItems?.AsQueryable()?.ToList() ?? new List<RoutingItemCollection>();
2023-09-07 22:04:34 +00:00
_routingItems = routingItemDocs.Select(x => new RoutingItem
2023-09-02 22:41:58 +00:00
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
Description = x.Description,
RequiredFields = x.RequiredFields,
RedirectTo = x.RedirectTo.ToString(),
Disabled = x.Disabled
}).ToList();
return _routingItems.AsQueryable();
}
}
2023-09-07 22:04:34 +00:00
private List<RoutingProfile> _routingProfiles;
public IQueryable<RoutingProfile> RoutingProfiles
2023-09-02 22:41:58 +00:00
{
get
{
if (_routingProfiles != null)
{
return _routingProfiles.AsQueryable();
}
var routingProfilDocs = _dc.RoutingProfiles?.AsQueryable()?.ToList() ?? new List<RoutingProfileCollection>();
2023-09-07 22:04:34 +00:00
_routingProfiles = routingProfilDocs.Select(x => new RoutingProfile
2023-09-02 22:41:58 +00:00
{
Id = x.Id.ToString(),
Name = x.Name,
AgentIds = x.AgentIds?.Select(x => x.ToString())?.ToList() ?? new List<string>()
}).ToList();
return _routingProfiles.AsQueryable();
}
}
2023-08-28 05:28:14 +00:00
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
2023-09-07 22:04:34 +00:00
if (entity is Conversation conversation)
2023-08-28 05:28:14 +00:00
{
_conversations.Add(conversation);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(Conversation));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is Agent agent)
2023-08-26 04:41:01 +00:00
{
2023-08-28 05:28:14 +00:00
_agents.Add(agent);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(Agent));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is User user)
2023-08-28 05:28:14 +00:00
{
_users.Add(user);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(User));
2023-08-28 05:28:14 +00:00
}
2023-09-07 22:04:34 +00:00
else if (entity is UserAgent userAgent)
2023-08-28 05:28:14 +00:00
{
_userAgents.Add(userAgent);
2023-09-07 22:04:34 +00:00
_changedTableNames.Add(nameof(UserAgent));
2023-08-28 05:28:14 +00:00
}
}
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)
{
2023-09-07 22:04:34 +00:00
if (table == nameof(Conversation))
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-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
UserId = Guid.Parse(x.UserId),
2023-08-28 18:57:51 +00:00
Title = x.Title,
2023-08-28 05:28:14 +00:00
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)
2023-08-28 18:57:51 +00:00
.Set(x => x.Title, conversation.Title)
2023-08-28 05:28:14 +00:00
.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
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(Agent))
2023-08-28 05:28:14 +00:00
{
var agents = _agents.Select(x => new AgentCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
2023-08-28 05:28:14 +00:00
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
2023-09-02 05:10:46 +00:00
Responses = x.Responses,
2023-09-04 15:15:11 +00:00
IsPublic = x.IsPublic,
2023-08-28 05:28:14 +00:00
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)
2023-09-02 05:10:46 +00:00
.Set(x => x.Responses, agent.Responses)
2023-09-04 15:15:11 +00:00
.Set(x => x.IsPublic, agent.IsPublic)
2023-08-28 05:28:14 +00:00
.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
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(User))
2023-08-28 05:28:14 +00:00
{
var users = _users.Select(x => new UserCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
2023-08-28 05:28:14 +00:00
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
}
2023-09-07 22:04:34 +00:00
else if (table == nameof(UserAgent))
2023-08-28 05:28:14 +00:00
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
2023-08-26 04:41:01 +00:00
{
2023-09-02 05:10:46 +00:00
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
UserId = Guid.Parse(x.UserId),
2023-08-28 05:28:14 +00:00
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
}
2023-09-02 05:10:46 +00:00
2023-09-07 22:04:34 +00:00
public User GetUserByEmail(string email)
2023-09-02 05:10:46 +00:00
{
2023-09-07 22:04:34 +00:00
var user = Users.FirstOrDefault(x => x.Email == email);
return user != null ? new User
2023-09-02 05:10:46 +00:00
{
Id = user.Id.ToString(),
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password,
Salt = user.Salt,
ExternalId = user.ExternalId,
CreatedTime = user.CreatedTime,
UpdatedTime = user.UpdatedTime
} : null;
}
2023-09-07 22:04:34 +00:00
public void CreateUser(User user)
2023-09-02 05:10:46 +00:00
{
if (user == null) return;
var userCollection = new UserCollection
{
Id = Guid.NewGuid(),
FirstName = user.FirstName,
LastName = user.LastName,
Salt = user.Salt,
Password = user.Password,
Email = user.Email,
ExternalId = user.ExternalId,
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
_dc.Users.InsertOne(userCollection);
}
2023-09-07 22:04:34 +00:00
public void UpdateAgent(Agent agent)
2023-09-02 05:10:46 +00:00
{
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
var agentCollection = new AgentCollection
{
Id = Guid.Parse(agent.Id),
Name = agent.Name,
Description = agent.Description,
Instruction = agent.Instruction,
Functions = agent.Functions,
Responses = agent.Responses,
2023-09-04 15:15:11 +00:00
IsPublic = agent.IsPublic,
2023-09-02 05:10:46 +00:00
UpdatedTime = DateTime.UtcNow
};
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(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.Responses, agent.Responses)
2023-09-04 15:15:11 +00:00
.Set(x => x.IsPublic, agent.IsPublic)
2023-09-02 05:10:46 +00:00
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
}
2023-09-03 04:11:53 +00:00
public void DeleteRoutingItems()
{
_dc.RoutingItems.DeleteMany(Builders<RoutingItemCollection>.Filter.Empty);
}
public void DeleteRoutingProfiles()
{
_dc.RoutingProfiles.DeleteMany(Builders<RoutingProfileCollection>.Filter.Empty);
}
2023-09-07 22:04:34 +00:00
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
2023-09-03 04:11:53 +00:00
{
var collections = routingItems?.Select(x => new RoutingItemCollection
{
Id = Guid.NewGuid(),
AgentId = Guid.Parse(x.AgentId),
Name = x.Name,
Description = x.Description,
RequiredFields = x.RequiredFields,
RedirectTo = !string.IsNullOrEmpty(x.RedirectTo) ? Guid.Parse(x.RedirectTo) : null,
Disabled = x.Disabled
})?.ToList() ?? new List<RoutingItemCollection>();
_dc.RoutingItems.InsertMany(collections);
2023-09-07 22:04:34 +00:00
return collections.Select(x => new RoutingItem
2023-09-03 04:11:53 +00:00
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
Name = x.Name,
Description = x.Description,
RequiredFields = x.RequiredFields,
RedirectTo = x.RedirectTo?.ToString(),
Disabled = x.Disabled
}).ToList();
}
2023-09-07 22:04:34 +00:00
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
2023-09-03 04:11:53 +00:00
{
var collections = profiles?.Select(x => new RoutingProfileCollection
{
Id = Guid.NewGuid(),
Name = x.Name,
AgentIds = x.AgentIds.Select(x => Guid.Parse(x)).ToList()
})?.ToList() ?? new List<RoutingProfileCollection>();
_dc.RoutingProfiles.InsertMany(collections);
2023-09-07 22:04:34 +00:00
return collections.Select(x => new RoutingProfile
2023-09-03 04:11:53 +00:00
{
Id = x.Id.ToString(),
Name = x.Name,
AgentIds = x.AgentIds.Select(x => x.ToString()).ToList()
}).ToList();
}
2023-09-03 04:54:22 +00:00
public List<string> GetAgentResponses(string agentId)
{
var responses = new List<string>();
2023-09-07 22:04:34 +00:00
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
2023-09-03 04:54:22 +00:00
if (agent == null) return responses;
return agent.Responses;
}
2023-09-03 19:32:12 +00:00
2023-09-07 22:04:34 +00:00
public Agent GetAgent(string agentId)
2023-09-03 19:32:12 +00:00
{
2023-09-07 22:04:34 +00:00
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
2023-09-03 19:32:12 +00:00
return foundAgent;
}
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
public void CreateNewConversation(Conversation conversation)
2023-09-03 22:43:50 +00:00
{
if (conversation == null) return;
2023-09-06 04:41:49 +00:00
var conv = new ConversationCollection
2023-09-03 22:43:50 +00:00
{
2023-09-07 22:04:34 +00:00
Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(),
2023-09-03 22:43:50 +00:00
AgentId = Guid.Parse(conversation.AgentId),
UserId = Guid.Parse(conversation.UserId),
Title = conversation.Title,
2023-09-06 04:41:49 +00:00
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow,
};
var dialog = new ConversationDialogCollection
{
Id = Guid.NewGuid(),
ConversationId = conv.Id,
2023-09-07 22:04:34 +00:00
Dialog = string.Empty
2023-09-03 22:43:50 +00:00
};
2023-09-06 04:41:49 +00:00
_dc.Conversations.InsertOne(conv);
_dc.ConversationDialogs.InsertOne(dialog);
2023-09-03 22:43:50 +00:00
}
public string GetConversationDialog(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
2023-09-06 04:41:49 +00:00
var filter = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault();
if (foundDialog == null) return string.Empty;
2023-09-03 22:43:50 +00:00
2023-09-06 04:41:49 +00:00
return foundDialog.Dialog;
2023-09-03 22:43:50 +00:00
}
public void UpdateConversationDialog(string conversationId, string dialogs)
{
if (string.IsNullOrEmpty(conversationId)) return;
2023-09-07 22:04:34 +00:00
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConv = _dc.Conversations.Find(filterConv).FirstOrDefault();
if (foundConv == null) return;
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
var foundDialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
2023-09-06 04:41:49 +00:00
if (foundDialog == null) return;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
var updateDialog = Builders<ConversationDialogCollection>.Update.Set(x => x.Dialog, dialogs);
var updateConv = Builders<ConversationCollection>.Update.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
_dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog);
_dc.Conversations.UpdateOne(filterConv, updateConv);
2023-09-03 22:43:50 +00:00
}
2023-09-07 22:04:34 +00:00
public List<KeyValueModel> GetConversationStates(string conversationId)
2023-09-03 22:43:50 +00:00
{
var states = new List<KeyValueModel>();
if (string.IsNullOrEmpty(conversationId)) return states;
2023-09-07 22:04:34 +00:00
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConversation = _dc.Conversations.Find(filter).FirstOrDefault();
var savedStates = foundConversation?.States ?? new List<KeyValueModel>();
2023-09-03 22:43:50 +00:00
return savedStates;
}
2023-09-07 22:04:34 +00:00
public void UpdateConversationStates(string conversationId, List<KeyValueModel> states)
2023-09-03 22:43:50 +00:00
{
if (string.IsNullOrEmpty(conversationId)) return;
2023-09-07 22:04:34 +00:00
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
var foundConv = _dc.Conversations.Find(filter).FirstOrDefault();
if (foundConv == null) return;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
var update = Builders<ConversationCollection>.Update
.Set(x => x.States, states)
2023-09-03 22:43:50 +00:00
.Set(x => x.UpdatedTime, DateTime.UtcNow);
2023-09-07 22:04:34 +00:00
_dc.Conversations.UpdateOne(filter, update);
2023-09-03 22:43:50 +00:00
}
2023-09-07 22:04:34 +00:00
public Conversation GetConversation(string conversationId)
2023-09-03 22:43:50 +00:00
{
if (string.IsNullOrEmpty(conversationId)) return null;
2023-09-07 22:04:34 +00:00
var filterConv = Builders<ConversationCollection>.Filter.Eq(x => x.Id, Guid.Parse(conversationId));
2023-09-06 04:41:49 +00:00
var filterDialog = Builders<ConversationDialogCollection>.Filter.Eq(x => x.ConversationId, Guid.Parse(conversationId));
2023-09-07 22:04:34 +00:00
var conv = _dc.Conversations.Find(filterConv).FirstOrDefault();
2023-09-06 04:41:49 +00:00
var dialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
if (conv == null) return null;
2023-09-03 22:43:50 +00:00
2023-09-07 22:04:34 +00:00
return new Conversation
2023-09-03 22:43:50 +00:00
{
2023-09-06 04:41:49 +00:00
Id = conv.Id.ToString(),
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
Dialog = dialog?.Dialog ?? string.Empty,
2023-09-07 22:04:34 +00:00
States = new ConversationState(conv.States ?? new List<KeyValueModel>()),
2023-09-06 04:41:49 +00:00
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
};
2023-09-03 22:43:50 +00:00
}
2023-09-07 22:04:34 +00:00
public List<Conversation> GetConversations(string userId)
2023-09-03 22:43:50 +00:00
{
2023-09-07 22:04:34 +00:00
var records = new List<Conversation>();
2023-09-06 04:41:49 +00:00
if (string.IsNullOrEmpty(userId)) return records;
2023-09-03 22:43:50 +00:00
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, Guid.Parse(userId));
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
2023-09-06 04:41:49 +00:00
foreach (var conv in conversations)
2023-09-03 22:43:50 +00:00
{
2023-09-06 04:41:49 +00:00
var convId = conv.Id.ToString();
2023-09-07 22:04:34 +00:00
records.Add(new Conversation
2023-09-06 04:41:49 +00:00
{
Id = convId,
AgentId = conv.AgentId.ToString(),
UserId = conv.UserId.ToString(),
Title = conv.Title,
CreatedTime = conv.CreatedTime,
UpdatedTime = conv.UpdatedTime
});
}
return records;
2023-09-03 22:43:50 +00:00
}
2023-08-26 04:41:01 +00:00
}