|
| 1 | +namespace DotNetToolkit.Repository.Queries |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Linq; |
| 5 | + using System.Linq.Expressions; |
| 6 | + |
| 7 | + /// <summary> |
| 8 | + /// Represents a paging option. |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="T">The entity type of the repository.</typeparam> |
| 11 | + /// <typeparam name="TKey">The type of the property that is being sorted.</typeparam> |
| 12 | + public class PagingOptions<T, TKey> : SortingOptions<T, TKey> |
| 13 | + { |
| 14 | + #region Fields |
| 15 | + |
| 16 | + private const int DefaultPageSize = 100; |
| 17 | + private int _pageIndex; |
| 18 | + private int _pageSize; |
| 19 | + |
| 20 | + #endregion |
| 21 | + |
| 22 | + #region Properties |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the number of rows of the data page to retrieve. |
| 26 | + /// </summary> |
| 27 | + public int PageSize |
| 28 | + { |
| 29 | + get { return _pageSize; } |
| 30 | + set |
| 31 | + { |
| 32 | + if (value <= 0) |
| 33 | + throw new ArgumentException($"{nameof(PageSize)} cannot be lower than zero."); |
| 34 | + |
| 35 | + _pageSize = value; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets or sets the zero-based index of the data page to retrieve. |
| 41 | + /// </summary> |
| 42 | + public int PageIndex |
| 43 | + { |
| 44 | + get { return _pageIndex; } |
| 45 | + set |
| 46 | + { |
| 47 | + if (value < 1) |
| 48 | + throw new ArgumentOutOfRangeException(nameof(PageIndex), "Cannot be lower than 1."); |
| 49 | + |
| 50 | + _pageIndex = value; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Gets the page count. |
| 56 | + /// </summary> |
| 57 | + public int PageCount { get; private set; } |
| 58 | + |
| 59 | + #endregion |
| 60 | + |
| 61 | + #region Constructors |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the <see cref="PagingOptions{TEntity, TKey}"/> class. |
| 65 | + /// </summary> |
| 66 | + /// <param name="pageIndex">The zero-based index of the data page to retrieve.</param> |
| 67 | + /// <param name="sorting">The sorting expression.</param> |
| 68 | + /// <param name="isDescending">if set to <c>true</c> use descending order; otherwise, use ascending.</param> |
| 69 | + public PagingOptions(int pageIndex, Expression<Func<T, TKey>> sorting, bool isDescending = false) : base(sorting, isDescending) |
| 70 | + { |
| 71 | + PageIndex = pageIndex; |
| 72 | + PageSize = DefaultPageSize; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Initializes a new instance of the <see cref="PagingOptions{TEntity, TKey}"/> class. |
| 77 | + /// </summary> |
| 78 | + /// <param name="pageIndex">The zero-based index of the data page to retrieve.</param> |
| 79 | + /// <param name="pageSize">The number of rows of the data page to retrieve.</param> |
| 80 | + /// <param name="sorting">The sorting expression.</param> |
| 81 | + /// <param name="isDescending">if set to <c>true</c> use descending order; otherwise, use ascending.</param> |
| 82 | + public PagingOptions(int pageIndex, int pageSize, Expression<Func<T, TKey>> sorting, bool isDescending = false) : base(sorting, isDescending) |
| 83 | + { |
| 84 | + PageIndex = pageIndex; |
| 85 | + PageSize = pageSize; |
| 86 | + } |
| 87 | + |
| 88 | + #endregion |
| 89 | + |
| 90 | + #region Overrides of SortingOptions<TEntity,TKey> |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Applies the current options to the specified query. |
| 94 | + /// </summary> |
| 95 | + /// <param name="query">The query.</param> |
| 96 | + /// <returns>The new query with the defined options applied.</returns> |
| 97 | + public override IQueryable<T> Apply(IQueryable<T> query) |
| 98 | + { |
| 99 | + var pageCount = (int)Math.Ceiling(query.Count() / (decimal)PageSize); |
| 100 | + |
| 101 | + if (PageIndex > pageCount) |
| 102 | + throw new ArgumentOutOfRangeException(nameof(PageIndex), "Cannot be greater than the total number of pages."); |
| 103 | + |
| 104 | + PageCount = pageCount; |
| 105 | + |
| 106 | + query = base.Apply(query); |
| 107 | + |
| 108 | + return query.Skip((PageIndex - 1) * PageSize).Take(PageSize); |
| 109 | + } |
| 110 | + |
| 111 | + #endregion |
| 112 | + } |
| 113 | +} |
0 commit comments