public override void Filter(QueryFilterContext context)
        {
            var userName = context.Filters.Get("username");
            var category = context.Filters.Get("category");
            var from     = GetDateFromFilter(context.Filters, "From", "from").StartOfDay();
            var to       = GetDateFromFilter(context.Filters, "To", "to").EndOfDay();
            var query    = context.Query;

            if (!String.IsNullOrWhiteSpace(userName))
            {
                query = query.Where(x => x.UserName == userName);
            }
            if (!String.IsNullOrWhiteSpace(category))
            {
                query = query.Where(x => x.Category == category);
            }
            if (from != null)
            {
                query = query.Where(x => x.CreatedUtc >= from);
            }
            if (to != null)
            {
                query = query.Where(x => x.CreatedUtc <= to);
            }

            context.Query = query;
        }
        public async Task <AuditTrailEventSearchResults> GetAuditTrailEventsAsync(
            int page,
            int pageSize,
            Filters filters           = null,
            AuditTrailOrderBy orderBy = AuditTrailOrderBy.DateDescending)
        {
            var session = _session.Store.CreateSession(System.Data.IsolationLevel.ReadUncommitted);
            var query   = session.Query <AuditTrailEvent>();

            if (filters != null)
            {
                var filterContext = new QueryFilterContext(query, filters);

                _auditTrailEventHandlers.Invoke((handler, context) => handler.Filter(context), filterContext, Logger);

                // Give each provider a chance to modify the query.
                var providersContext = DescribeProviders();
                foreach (var queryFilter in providersContext.QueryFilters)
                {
                    queryFilter(filterContext);
                }

                query = filterContext.Query;
            }

            switch (orderBy)
            {
            case AuditTrailOrderBy.CategoryAscending:
                query.With <AuditTrailEventIndex>().OrderBy(eventIndex => eventIndex.Category).ThenByDescending(eventIndex => eventIndex.Id);
                break;

            case AuditTrailOrderBy.EventAscending:
                query.With <AuditTrailEventIndex>().OrderBy(eventIndex => eventIndex.EventName).ThenByDescending(eventIndex => eventIndex.Id);
                break;

            case AuditTrailOrderBy.DateDescending:
                query.With <AuditTrailEventIndex>().OrderByDescending(eventIndex => eventIndex.CreatedUtc).ThenByDescending(eventIndex => eventIndex.Id);
                break;
            }

            var auditTrailEvents = await query.ListAsync();

            var auditTrailEventsTotalCount = auditTrailEvents.Count();

            var startIndex = (page - 1) * pageSize;

            auditTrailEvents = auditTrailEvents.Skip(startIndex);

            if (pageSize > 0)
            {
                auditTrailEvents = auditTrailEvents.Take(pageSize);
            }

            return(new AuditTrailEventSearchResults
            {
                AuditTrailEvents = auditTrailEvents,
                TotalCount = auditTrailEventsTotalCount
            });
        }
        public IPageOfItems <AuditTrailEventRecord> GetRecords(
            int page,
            int pageSize,
            Filters filters           = null,
            AuditTrailOrderBy orderBy = AuditTrailOrderBy.DateDescending)
        {
            var query = _auditTrailRepository.Table;

            if (filters != null)
            {
                var filterContext = new QueryFilterContext(query, filters);

                // Invoke event handlers.
                _auditTrailEventHandlers.Filter(filterContext);

                // Give each provider a chance to modify the query.
                var providersContext = DescribeProviders();
                foreach (var queryFilter in providersContext.QueryFilters)
                {
                    queryFilter(filterContext);
                }

                query = filterContext.Query;
            }

            switch (orderBy)
            {
            case AuditTrailOrderBy.EventAscending:
                query = query.OrderBy(x => x.EventName).ThenByDescending(x => x.Id);
                break;

            case AuditTrailOrderBy.CategoryAscending:
                query = query.OrderBy(x => x.Category).ThenByDescending(x => x.Id);
                break;

            default:
                query = query.OrderByDescending(x => x.CreatedUtc).ThenByDescending(x => x.Id);
                break;
            }

            var totalItemCount = query.Count();
            var startIndex     = (page - 1) * pageSize;

            query = query.Skip(startIndex);

            if (pageSize > 0)
            {
                query = query.Take(pageSize);
            }

            return(new PageOfItems <AuditTrailEventRecord>(query)
            {
                PageNumber = page,
                PageSize = pageSize,
                TotalItemCount = totalItemCount
            });
        }
        private void QueryFilter(QueryFilterContext context)
        {
            if (!context.Filters.ContainsKey("content"))
            {
                return;
            }

            var contentId = context.Filters["content"].ToInt32();

            context.Query = context.Query.Where(x => x.EventFilterKey == "content" && x.EventFilterData == contentId.ToString());
        }
        private void QueryFilter(QueryFilterContext context)
        {
            var contentType = context.Filters.Get("contenttype");

            if (String.IsNullOrWhiteSpace(contentType))
            {
                return;
            }

            context.Query = context.Query.Where(x => x.EventFilterKey == "contenttype" && x.EventFilterData == contentType);
        }
        /// <summary>Adds or get the filter context associated with the context.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The filter context associated with the context.</returns>
        public static QueryFilterContext AddOrGetFilterContext(DbContext context)
        {
            QueryFilterContext filterContext;

            if (!CacheWeakFilterContext.TryGetValue(context, out filterContext))
            {
                filterContext = new QueryFilterContext(context);
                CacheWeakFilterContext.Add(context, filterContext);
            }

            return filterContext;
        }
        /// <summary>Adds or gets the generic filter context associated with the context.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The generic filter context associated with the context.</returns>
        public static QueryFilterContext AddOrGetGenericFilterContext(DbContext context)
        {
            var key = context.GetType().FullName;
            QueryFilterContext filterContext;

            if (!CacheGenericFilterContext.TryGetValue(key, out filterContext))
            {
                lock (GenericFilterContextLock)
                {
                    if (!CacheGenericFilterContext.TryGetValue(key, out filterContext))
                    {
                        filterContext = new QueryFilterContext(context, true);
                        CacheGenericFilterContext.Add(key, filterContext);
                    }
                }
            }

            return filterContext;
        }
示例#8
0
        public override void Filter(QueryFilterContext context)
        {
            var userName = context.Filters.Get("username");
            var category = context.Filters.Get("category");
            var from     = GetDateFromFilter(context.Filters, "From", "from");
            var to       = GetDateFromFilter(context.Filters, "To", "to");

            if (!string.IsNullOrWhiteSpace(userName))
            {
                context.Query.With <AuditTrailEventIndex>(eventIndex => eventIndex.UserName == userName);
            }
            if (!string.IsNullOrWhiteSpace(category))
            {
                context.Query.With <AuditTrailEventIndex>(eventIndex => eventIndex.Category == category);
            }
            if (from != null)
            {
                context.Query.With <AuditTrailEventIndex>(eventIndex => eventIndex.CreatedUtc >= from);
            }
            if (to != null)
            {
                context.Query.With <AuditTrailEventIndex>(eventIndex => eventIndex.CreatedUtc <= to.Value.AddDays(1));
            }
        }
 public virtual void Filter(QueryFilterContext context)
 {
 }