Example #1
0
        private string CreatePagging(FilterParameters filterParameters)
        {
            var pageData = PageDataExtensions.GetPageData(filterParameters.PageSize, filterParameters.CurrentPage);

            if (pageData.skip >= 0)
            {
                return($"OFFSET {pageData.skip} ROWS FETCH NEXT {pageData.take} ROWS ONLY");
            }

            return(string.Empty);
        }
        public async Task <PagedResult <TFilterableEntity> > FilterAsync(DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory, FilterParameters filterParameters)
        {
            var mandatoryPredicate = mandatoryPredicateFactory.GetMandatoryPredicates();

            var predicate = LinqKit.PredicateBuilder.New <TEntity>(true);

            foreach (var filter in filterParameters.Statements)
            {
                ISpecification <TEntity> specification = Create(filter);

                if (specification == null)
                {
                    continue;
                }

                if (filter.LogicalOperator == FilterLogicalOperators.Or)
                {
                    predicate.Or(specification.IsSatisfiedBy());
                }
                else
                {
                    predicate.And(specification.IsSatisfiedBy());
                }
            }

            var count = await context.Set <TEntity>().AsExpandable().Where(mandatoryPredicate).Where(predicate).LongCountAsync();

            var entityQuery = context.Set <TEntity>().AsQueryable();

            if (_parameters.IncludeProperties != null)
            {
                foreach (var includedProperty in _parameters.IncludeProperties)
                {
                    entityQuery = entityQuery.Include(includedProperty);
                }
            }

            var query = entityQuery.AsExpandable().Where(mandatoryPredicate).Where(predicate);

            if (!string.IsNullOrWhiteSpace(filterParameters.OrderBy))
            {
                var sortOrder = filterParameters.OrderKind == OrderKind.Asc? ListSortDirection.Ascending: ListSortDirection.Descending;

                var orderByColumn = filterParameters.OrderBy.ToLower();

                if (_parameters.SortFieldsMaping.ContainsKey(orderByColumn))
                {
                    orderByColumn = _parameters.SortFieldsMaping[orderByColumn];
                }

                query = query.OrderBy(orderByColumn, sortOrder);
            }

            var pageData = PageDataExtensions.GetPageData(filterParameters.PageSize, filterParameters.CurrentPage);

            if (pageData.skip >= 0)
            {
                query = query.Skip(pageData.skip).Take(pageData.take);
            }

            TFilterableEntity[] entities;

            if (_parameters.DirectProject)
            {
                entities = await _mapper.ProjectTo <TFilterableEntity>(query).ToArrayAsync();
            }
            else
            {
                var queryEntities = await query.ToArrayAsync();

                entities = _mapper.Map <TFilterableEntity[]>(queryEntities);
            }

            return(new PagedResult <TFilterableEntity>(count, entities));
        }