diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 8116d0eb9..fe6d067d0 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -278,50 +278,63 @@ private void UpdateAgentAllFields(Agent agent) public List GetAgents(AgentFilter filter) { var agents = new List(); - IQueryable query = _dc.Agents.AsQueryable(); + var builder = Builders.Filter; + var filters = new List>() { builder.Empty }; if (!string.IsNullOrEmpty(filter.AgentName)) { - query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower()); + filters.Add(builder.Eq(x => x.Name, filter.AgentName)); } if (filter.Disabled.HasValue) { - query = query.Where(x => x.Disabled == filter.Disabled); + filters.Add(builder.Eq(x => x.Disabled, filter.Disabled.Value)); } if (filter.AllowRouting.HasValue) { - query = query.Where(x => x.AllowRouting == filter.AllowRouting); + filters.Add(builder.Eq(x => x.AllowRouting, filter.AllowRouting.Value)); } if (filter.IsPublic.HasValue) { - query = query.Where(x => x.IsPublic == filter.IsPublic); + filters.Add(builder.Eq(x => x.IsPublic, filter.IsPublic.Value)); } if (filter.IsRouter.HasValue) { var route = _services.GetRequiredService(); - query = filter.IsRouter.Value ? - query.Where(x => route.AgentIds.Contains(x.Id)) : - query.Where(x => !route.AgentIds.Contains(x.Id)); + if (filter.IsRouter.Value) + { + filters.Add(builder.In(x => x.Id, route.AgentIds)); + } + else + { + filters.Add(builder.Nin(x => x.Id, route.AgentIds)); + } } if (filter.IsEvaluator.HasValue) { var evaluate = _services.GetRequiredService(); - query = filter.IsEvaluator.Value ? - query.Where(x => x.Id == evaluate.AgentId) : - query.Where(x => x.Id != evaluate.AgentId); + if (filter.IsEvaluator.Value) + { + filters.Add(builder.Eq(x => x.Id, evaluate.AgentId)); + } + else + { + filters.Add(builder.Ne(x => x.Id, evaluate.AgentId)); + } } if (filter.AgentIds != null) { - query = query.Where(x => filter.AgentIds.Contains(x.Id)); + filters.Add(builder.In(x => x.Id, filter.AgentIds)); } - return query.ToList().Select(x => new Agent + var agentDocs = _dc.Agents.Find(builder.And(filters)).ToList(); + + return agentDocs.Select(x => new Agent { Id = x.Id, Name = x.Name, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index ada3ee5ae..6c8eb2222 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -206,21 +206,21 @@ public Conversation GetConversation(string conversationId) public List GetConversations(ConversationFilter filter) { - var records = new List(); + var conversations = new List(); var builder = Builders.Filter; - var filters = new List>(); + var filters = new List>() { builder.Empty }; if (!string.IsNullOrEmpty(filter.AgentId)) filters.Add(builder.Eq(x => x.AgentId, filter.AgentId)); if (!string.IsNullOrEmpty(filter.Status)) filters.Add(builder.Eq(x => x.Status, filter.Status)); if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel)); if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId)); - var conversations = _dc.Conversations.Find(builder.And(filters)).ToList(); + var conversationDocs = _dc.Conversations.Find(builder.And(filters)).ToList(); - foreach (var conv in conversations) + foreach (var conv in conversationDocs) { var convId = conv.Id.ToString(); - records.Add(new Conversation + conversations.Add(new Conversation { Id = convId, AgentId = conv.AgentId.ToString(), @@ -233,7 +233,7 @@ public List GetConversations(ConversationFilter filter) }); } - return records; + return conversations; } public List GetLastConversations()