Ejemplo n.º 1
0
        /// <summary>
        /// Sets the default bounds for admin paging queries which is a
        /// default page size of 20 and and a maximum page size of 100.
        /// </summary>
        /// <param name="query">Query to set bounds for.</param>
        public static void SetDefaultBounds(IPageableQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            query.SetBounds(40, 100);
        }
Ejemplo n.º 2
0
        private IQueryable <IncomeEntity> ApplyPaging(IQueryable <IncomeEntity> sql, IPageableQuery query)
        {
            if (query.PageIndex != null)
            {
                sql = sql.TakePage(query.PageIndex.Value, PageSize);
            }

            return(sql);
        }
Ejemplo n.º 3
0
        protected virtual IOrderedQueryable <TModel> PerformGetBy(IPageableQuery <TModel> query, TDataContext dataContext)
        {
            var sequence = ApplyFilters(query, dataContext.Set <TModel>());

            sequence = ApplyIncludedProperties(query, sequence);

            sequence = ApplySortCriterias(query, sequence);

            return(sequence as IOrderedQueryable <TModel>);
        }
Ejemplo n.º 4
0
        protected IOrderedQueryable <TModel> ApplySortCriterias(IPageableQuery <TModel> searchCritery,
                                                                IQueryable <TModel> sequence)
        {
            var result = sequence.OrderBy(e => e.Id);

            return(searchCritery.SortCriterias.Aggregate(result,
                                                         (current, sortCriteria) =>
                                                         sortCriteria.SortDirection == SortDirection.Asc
                                                ? current.OrderByAsc(sortCriteria.Name)
                                                : current.OrderByDesc(sortCriteria.Name)));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns an empty result that represents the specified query, i.e.
        /// with the correct page number and page size, but no results.
        /// </summary>
        /// <param name="query">Query to base the result on.</param>
        /// <returns>New, empty instance of PagedQueryResult</returns>
        public static PagedQueryResult <TResult> Empty(IPageableQuery query)
        {
            var result = new PagedQueryResult <TResult>();

            result.Items      = new TResult[0];
            result.PageCount  = 0;
            result.PageNumber = query.PageNumber;
            result.PageSize   = query.PageSize;
            result.TotalItems = 0;

            return(result);
        }
Ejemplo n.º 6
0
        public IPageableList <TModel> GetPageableBy(IPageableQuery <TModel> query = null)
        {
            return(ProcessMethod(() =>
            {
                if (query == null)
                {
                    query = new PageableQuery <TModel>(new PageInfo());
                }

                var items = PerformGetBy(query, DataContext).ToPageableList(query.PageInfo.PageNumber, query.PageInfo.PageSize);

                return items;
            }));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Pages a query based on the parameters of the query.
        /// </summary>
        public static IQueryable <T> Page <T>(this IQueryable <T> source, IPageableQuery query)
        {
            if (query == null || query.PageSize <= 0)
            {
                return(source);
            }

            var pageNumber  = query.PageNumber < 1 ? 0 : query.PageNumber - 1;
            var itemsToSkip = pageNumber * query.PageSize;

            return(source
                   .Skip(itemsToSkip)
                   .Take(query.PageSize));
        }
Ejemplo n.º 8
0
 /// <remarks>
 /// Public so it can be referenced by ToPagedResultAsync in Cofoundry.Domain.Data
 /// </remarks>
 public static void MapPagingData <T>(IPageableQuery query, PagedQueryResult <T> result)
 {
     if (query != null && query.PageSize > 0)
     {
         result.PageSize   = query.PageSize;
         result.PageCount  = (int)Math.Ceiling(result.TotalItems / (double)result.PageSize);
         result.PageNumber = query.PageNumber < 1 ? 1 : query.PageNumber;
     }
     else
     {
         result.PageSize   = result.TotalItems;
         result.PageCount  = 1;
         result.PageNumber = 1;
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Sets the paging bounds of an IPageableQuery, setting a default page
        /// size if one isn't provided.
        /// </summary>
        /// <param name="query">Query to set the bounds of.</param>
        /// <param name="defaultSize">The default page size to apply if one is not set.</param>
        /// <param name="allowUnbounded">
        /// By default negative page sizes indicate that no paging should apply, but
        /// this setting can be used to prevent that behaviour. The default value is
        /// false, which means the default page size will be applied if the page size
        /// less than 0. If set to true the default page size will only be applied if
        /// the page size is 0 i.e. negative page sizes will be permitted.
        /// </param>
        public static void SetBounds(
            this IPageableQuery query,
            int defaultSize,
            bool allowUnbounded = false
            )
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if ((allowUnbounded && query.PageSize == 0) ||
                (!allowUnbounded && query.PageSize <= 0))
            {
                query.PageSize = defaultSize;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sets the paging bounds of an IPageableQuery, setting a default page
        /// size if one isn't provided. This overload treats negative page sizes
        /// as 'not set' rather than unbounded. It is assumed that if you're setting
        /// a max size that you don't want to allow unbounded queries.
        /// </summary>
        /// <param name="query">Query to set the bounds of.</param>
        /// <param name="defaultSize">The default page size to apply if one is not set.</param>
        /// <param name="maxSize">
        /// The maximum page size limit to apply. If the PageSize setting is exceeds
        /// this value then the PageSize is set to the maxSize value.
        /// </param>
        public static void SetBounds(
            this IPageableQuery query,
            int defaultSize,
            int maxSize
            )
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (query.PageSize <= 0)
            {
                query.PageSize = defaultSize;
            }
            if (query.PageSize > maxSize)
            {
                query.PageSize = maxSize;
            }
        }
 public static IQueryable <T> Page <T>(this IPageableQuery <T> sourceQuery, int pageNumber)
 {
     return(sourceQuery.Skip((pageNumber - 1) * sourceQuery.PageSize)
            .Take(sourceQuery.PageSize));
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Converts a query to an instance of PagedQueryResult, executing the query twice,
        /// once to get the total count and again to get the results.
        /// </summary>
        public static PagedQueryResult <T> ToPagedResult <T>(this IQueryable <T> source, IPageableQuery query)
        {
            Condition.Requires(source).IsNotNull();

            var result = new PagedQueryResult <T>();

            result.TotalItems = source.Count();
            result.Items      = source.Page(query).ToArray();
            MapPagingData <T>(query, result);

            return(result);
        }
        /// <summary>
        /// Converts a query to an instance of PagedQueryResult, executing the query twice,
        /// once to get the total count and again to get the results.
        /// </summary>
        public static async Task <PagedQueryResult <T> > ToPagedResultAsync <T>(this IQueryable <T> source, IPageableQuery query)
        {
            Condition.Requires(source).IsNotNull();

            var result = new PagedQueryResult <T>();

            result.TotalItems = await source.CountAsync();

            result.Items = await source.Page(query).ToArrayAsync();

            PagingExtensions.MapPagingData <T>(query, result);

            return(result);
        }
Ejemplo n.º 14
0
 private static IPagedList <T> PageToListEnumerable <T>(IEnumerable <T> enumerable, IPageableQuery pageable)
     where T : class
 {
     if (pageable.PageSize <= 0)
     {
         throw new ArgumentOutOfRangeException("pageSize", pageable.PageSize,
                                               "Page size should be greater than zero.");
     }
     if (pageable.PageNumber <= 0)
     {
         throw new ArgumentOutOfRangeException("pageNumber", pageable.PageNumber,
                                               "Page number should be greater than zero.");
     }
     return(new PagedList <T>(enumerable, pageable.PageNumber, pageable.PageSize));
 }
 public OrderedPageableQuery(IPageableQuery <T> sourcePageableQuery, IOrderedQueryable <T> newSourceQuery)
 {
     _sourcePageableQuery = sourcePageableQuery;
     _sourceQuery         = newSourceQuery;
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Converts a query to an instance of PagedQueryResult, executing the query twice,
        /// once to get the total count and again to get the results.
        /// </summary>
        public static async Task <PagedQueryResult <T> > ToPagedResultAsync <T>(this IQueryable <T> source, IPageableQuery query)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var result = new PagedQueryResult <T>();

            result.TotalItems = await source.CountAsync();

            result.Items = await source.Page(query).ToArrayAsync();

            PagingExtensions.MapPagingData <T>(query, result);

            return(result);
        }
 public static IOrderedPageableQuery <T> OrderBy <T, U>(this IPageableQuery <T> sourcePageableQuery, Expression <Func <T, U> > orderBy)
 {
     return(new OrderedPageableQuery <T>(sourcePageableQuery, Queryable.OrderBy(sourcePageableQuery, orderBy)));
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Converts a query to an instance of PagedQueryResult, executing the query twice,
        /// once to get the total count and again to get the results.
        /// </summary>
        public static PagedQueryResult <T> ToPagedResult <T>(this IQueryable <T> source, IPageableQuery query)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var result = new PagedQueryResult <T>();

            result.TotalItems = source.Count();
            result.Items      = source.Page(query).ToArray();
            MapPagingData <T>(query, result);

            return(result);
        }