private IMongoQueryable <EventModel> AddFilters( IEventFilteringCriteria eventFilteringCriteria, IMongoQueryable <EventModel> query) { return(query .AddFilter(!string.IsNullOrEmpty(eventFilteringCriteria.Source), q => q.Where(x => x.Source == eventFilteringCriteria.Source)) .AddFilter(!string.IsNullOrEmpty(eventFilteringCriteria.EventName), q => q.Where(x => x.EventName == eventFilteringCriteria.EventName)) .AddFilter(eventFilteringCriteria.EventType != null, q => q.Where(x => x.EventType == eventFilteringCriteria.EventType)) .AddFilter(eventFilteringCriteria.From != null, q => q.Where(x => x.Timestamp >= eventFilteringCriteria.From)) .AddFilter(eventFilteringCriteria.To != null, q => q.Where(x => x.Timestamp <= eventFilteringCriteria.To))); }
public async Task <PaginationResult <EventModel> > FindEventsByCriteriaAsync( IEventFilteringCriteria eventFilteringCriteria, CancellationToken cancellationToken = default) { var collection = _eventStoreDatabase.GetCollection <EventModel>(_configProvider.EventStoreContainer); var query = collection.AsQueryable(); query = AddFilters(eventFilteringCriteria, query); int count = await query.CountAsync(cancellationToken); var result = await query .OrderByDescending(x => x.Id) .Skip((eventFilteringCriteria.PageNumber - 1) * eventFilteringCriteria.PageSize) .Take(eventFilteringCriteria.PageSize) .ToListAsync(cancellationToken); return(new PaginationResult <EventModel> { PageNumber = eventFilteringCriteria.PageNumber, PageSize = eventFilteringCriteria.PageSize, ResultTotalCount = count, Result = result }); }