public DataSubList <MessageInfo> SearchMessages( MessageSearchParameters searchParameters, int framePosition, int frameSize) { ValidateFramePosition(framePosition); ValidateFrameSize(frameSize); searchParameters.Validate(); var context = GetOrAddContext(); var source = GetContactSource(searchParameters.Source.Value); var messagesQuery = DbContext.Messages.Include(m => m.Contact) .Include(m => m.Status) .Include(m => m.Template.Event.EventType) .Where(m => m.Contact.SourceId == source.Id && m.Contact.ContextId == context.Id); messagesQuery = ApplyFilters(searchParameters, messagesQuery); var resultMessages = messagesQuery.OrderByDescending(m => m.UpdateTime) .Skip(framePosition) .Take(frameSize) .ToList(DebugInfoCollector, "Search messages"); return(resultMessages.Select(ConvertToMessageInfo).ToSubList(messagesQuery.Count())); }
private IQueryable <Message> ApplyFilters( MessageSearchParameters searchParameters, IQueryable <Message> messagesQuery) { if (searchParameters.EventTypes.IsNotNull()) { var eventTypes = searchParameters.EventTypes.Select(GetCachedEventType); var eventTypeIds = eventTypes.Select(x => x.Id); messagesQuery = messagesQuery.Where(m => eventTypeIds.Contains(m.Template.Event.EventTypeId)); } if (searchParameters.ContactExternalIds.IsNotNull()) { var externalIds = searchParameters.GetContactExternalIds(); messagesQuery = messagesQuery.Where(m => externalIds.Contains(m.Contact.ExternalId)); } return(messagesQuery); }
public async Task Search() { MessageSearchParameters searchParameters = new MessageSearchParameters() { Text = "Change", Caterer = null, Client = null, Order = null }; var builder = Builders <Message> .Filter; var filter = builder.Empty; if (!string.IsNullOrWhiteSpace(searchParameters.Text)) { var criteriaFilter = builder.Regex(x => x.Text, new BsonRegularExpression($".*{searchParameters.Text}.*")); filter &= criteriaFilter; } if (searchParameters.Caterer.HasValue) { var criteriaFilter = builder.Eq(x => x.Caterer, searchParameters.Caterer.Value); filter &= criteriaFilter; } if (searchParameters.Client.HasValue) { var criteriaFilter = builder.Eq(x => x.Client, searchParameters.Client.Value); filter &= criteriaFilter; } if (searchParameters.Order.HasValue) { var criteriaFilter = builder.Eq(x => x.Order, searchParameters.Order.Value); filter &= criteriaFilter; } var result = await MessageRepository.GetAsync(CollectionId, 1, 10, filter); Console.WriteLine($"Output:\nTotal: {result.Item2}\n{JToken.Parse(JsonConvert.SerializeObject(result.Item1)).ToString(Formatting.Indented)}"); }