From adcd539bfab74540d791829974236d94f718f27d Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:41:21 +0800 Subject: [PATCH 01/15] move code from X.PagedList and remove PublicAPI attribute --- src/Moonglade.Web/Moonglade.Web.csproj | 1 - src/Moonglade.Web/PagedList/BasePagedList.cs | 125 ++++++ src/Moonglade.Web/PagedList/IPagedList.cs | 125 ++++++ src/Moonglade.Web/PagedList/PagedList.cs | 149 +++++++ .../PagedList/PagedListExtensions.cs | 384 ++++++++++++++++++ .../PagedList/PagedListMetaData.cs | 127 ++++++ .../PagedList/StaticPagedList.cs | 61 +++ 7 files changed, 971 insertions(+), 1 deletion(-) create mode 100644 src/Moonglade.Web/PagedList/BasePagedList.cs create mode 100644 src/Moonglade.Web/PagedList/IPagedList.cs create mode 100644 src/Moonglade.Web/PagedList/PagedList.cs create mode 100644 src/Moonglade.Web/PagedList/PagedListExtensions.cs create mode 100644 src/Moonglade.Web/PagedList/PagedListMetaData.cs create mode 100644 src/Moonglade.Web/PagedList/StaticPagedList.cs diff --git a/src/Moonglade.Web/Moonglade.Web.csproj b/src/Moonglade.Web/Moonglade.Web.csproj index adb6ed750..295c2385d 100644 --- a/src/Moonglade.Web/Moonglade.Web.csproj +++ b/src/Moonglade.Web/Moonglade.Web.csproj @@ -42,7 +42,6 @@ - diff --git a/src/Moonglade.Web/PagedList/BasePagedList.cs b/src/Moonglade.Web/PagedList/BasePagedList.cs new file mode 100644 index 000000000..4563052e6 --- /dev/null +++ b/src/Moonglade.Web/PagedList/BasePagedList.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace X.PagedList; + +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// The type of object the collection should contain. +/// +/// +public abstract class BasePagedList : PagedListMetaData, IPagedList +{ + protected readonly List Subset = new(); + + public const int DefaultPageSize = 100; + + /// + /// Parameterless constructor. + /// + protected internal BasePagedList() + { + } + + /// + /// Initializes a new instance of a type deriving from and sets properties + /// needed to calculate position and size data on the subset and superset. + /// + /// The one-based index of the subset of objects contained by this instance. + /// The maximum size of any individual subset. + /// The size of the superset. + protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCount) + { + if (pageNumber < 1) + { + throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); + } + + if (pageSize < 1) + { + throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); + } + + if (totalItemCount < 0) + { + throw new ArgumentOutOfRangeException($"totalItemCount = {totalItemCount}. TotalItemCount cannot be less than 0."); + } + + // set source to blank list if superset is null to prevent exceptions + TotalItemCount = totalItemCount; + + PageSize = pageSize; + PageNumber = pageNumber; + PageCount = TotalItemCount > 0 + ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) + : 0; + + var pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount; + + HasPreviousPage = pageNumberIsGood && PageNumber > 1; + HasNextPage = pageNumberIsGood && PageNumber < PageCount; + IsFirstPage = pageNumberIsGood && PageNumber == 1; + IsLastPage = pageNumberIsGood && PageNumber == PageCount; + + var numberOfFirstItemOnPage = (PageNumber - 1) * PageSize + 1; + + FirstItemOnPage = pageNumberIsGood ? numberOfFirstItemOnPage : 0; + + var numberOfLastItemOnPage = numberOfFirstItemOnPage + PageSize - 1; + + LastItemOnPage = pageNumberIsGood + ? numberOfLastItemOnPage > TotalItemCount + ? TotalItemCount + : numberOfLastItemOnPage + : 0; + } + + #region IPagedList Members + + /// + /// Returns an enumerator that iterates through the BasePagedList<T>. + /// + /// A BasePagedList<T>.Enumerator for the BasePagedList<T>. + public IEnumerator GetEnumerator() + { + return Subset.GetEnumerator(); + } + + /// + /// Returns an enumerator that iterates through the BasePagedList<T>. + /// + /// A BasePagedList<T>.Enumerator for the BasePagedList<T>. + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Gets the element at the specified index. + /// + ///The zero-based index of the element to get. + public T this[int index] => Subset[index]; + + /// + /// Gets the number of elements contained on this page. + /// + public virtual int Count => Subset.Count; + + /// + /// Gets a non-enumerable copy of this paged list. + /// + ///A non-enumerable copy of this paged list. + public PagedListMetaData GetMetaData() + { + return new PagedListMetaData(this); + } + + #endregion +} \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/IPagedList.cs b/src/Moonglade.Web/PagedList/IPagedList.cs new file mode 100644 index 000000000..44678052c --- /dev/null +++ b/src/Moonglade.Web/PagedList/IPagedList.cs @@ -0,0 +1,125 @@ +using System.Collections.Generic; + +namespace X.PagedList; + +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// The type of object the collection should contain. +/// +public interface IPagedList : IPagedList, IReadOnlyList +{ + /// + /// Gets a non-enumerable copy of this paged list. + /// + ///A non-enumerable copy of this paged list. + PagedListMetaData GetMetaData(); +} + +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +public interface IPagedList +{ + /// + /// Total number of subsets within the superset. + /// + /// + /// Total number of subsets within the superset. + /// + int PageCount { get; } + + /// + /// Total number of objects contained within the superset. + /// + /// + /// Total number of objects contained within the superset. + /// + int TotalItemCount { get; } + + /// + /// One-based index of this subset within the superset, zero if the superset is empty. + /// + /// + /// One-based index of this subset within the superset, zero if the superset is empty. + /// + int PageNumber { get; } + + /// + /// Maximum size any individual subset. + /// + /// + /// Maximum size any individual subset. + /// + int PageSize { get; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the first subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the first subset within the superset. + /// + bool HasPreviousPage { get; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the last subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the last subset within the superset. + /// + bool HasNextPage { get; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the first subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the first subset within the superset. + /// + bool IsFirstPage { get; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the last subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the last subset within the superset. + /// + bool IsLastPage { get; } + + /// + /// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + /// + /// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + int FirstItemOnPage { get; } + + /// + /// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + /// + /// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + int LastItemOnPage { get; } +} \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/PagedList.cs b/src/Moonglade.Web/PagedList/PagedList.cs new file mode 100644 index 000000000..74bd8c66d --- /dev/null +++ b/src/Moonglade.Web/PagedList/PagedList.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; + +namespace X.PagedList; + +public class PagedList : BasePagedList +{ + /// + /// Initializes a new instance of the class that divides the supplied superset into + /// subsets the size of the supplied pageSize. The instance then only contains the objects contained in the + /// subset specified by index. + /// + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// Expression for Order + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedList(IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) + : base(pageNumber, pageSize, superset?.Count() ?? 0) + { + // add items to internal list + if (TotalItemCount > 0) + { + InitSubset(superset, keySelector.Compile(), pageNumber, pageSize); + } + } + + public PagedList(IQueryable superset, Func keySelectorMethod, int pageNumber, int pageSize) + : base(pageNumber, pageSize, superset?.Count() ?? 0) + { + if (TotalItemCount > 0) + { + InitSubset(superset, keySelectorMethod, pageNumber, pageSize); + } + } + + private void InitSubset(IEnumerable superset, Func keySelectorMethod, int pageNumber, int pageSize) + { + // add items to internal list + + var skip = (pageNumber - 1) * pageSize; + var items = superset.OrderBy(keySelectorMethod).Skip(skip).Take(pageSize).ToList(); + + Subset.AddRange(items); + } +} + +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// The type of object the collection should contain. +/// +/// +/// +/// +public class PagedList : BasePagedList +{ + /// + /// Initializes a new instance of the class that divides the supplied superset + /// into subsets the size of the supplied pageSize. The instance then only contains the objects contained + /// in the subset specified by index. + /// + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedList(IQueryable superset, int pageNumber, int pageSize) + : base(pageNumber, pageSize, superset?.Count() ?? 0) + { + if (TotalItemCount > 0 && superset != null) + { + var skip = (pageNumber - 1) * pageSize; + + Subset.AddRange(superset.Skip(skip).Take(pageSize)); + } + } + + /// + /// Initializes a new instance of the class that divides the supplied superset + /// into subsets the size of the supplied pageSize. The instance then only contains the objects contained in + /// the subset specified by index. + /// + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedList(IEnumerable superset, int pageNumber, int pageSize) + : this(superset.AsQueryable(), pageNumber, pageSize) + { + } + + /// + /// For Clone PagedList + /// + /// Source PagedList + /// Source collection + public PagedList(IPagedList pagedList, IEnumerable collection) + { + TotalItemCount = pagedList.TotalItemCount; + PageSize = pagedList.PageSize; + PageNumber = pagedList.PageNumber; + PageCount = pagedList.PageCount; + HasPreviousPage = pagedList.HasPreviousPage; + HasNextPage = pagedList.HasNextPage; + IsFirstPage = pagedList.IsFirstPage; + IsLastPage = pagedList.IsLastPage; + FirstItemOnPage = pagedList.FirstItemOnPage; + LastItemOnPage = pagedList.LastItemOnPage; + + Subset.AddRange(collection); + + if (Subset.Count > PageSize) + { + throw new Exception($"{nameof(collection)} size can't be greater than PageSize"); + } + + } + + /// + /// Create empty static paged list + /// + /// + /// + public static PagedList Empty(int pageSize = DefaultPageSize) => + new(Array.Empty(), 1, pageSize); +} \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/PagedListExtensions.cs b/src/Moonglade.Web/PagedList/PagedListExtensions.cs new file mode 100644 index 000000000..190195b5c --- /dev/null +++ b/src/Moonglade.Web/PagedList/PagedListExtensions.cs @@ -0,0 +1,384 @@ +using JetBrains.Annotations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace X.PagedList; + +/// +/// Container for extension methods designed to simplify the creation of instances of . +/// +public static class PagedListExtensions +{ + /// + /// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and + /// say 'shoes.Split(5)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. + /// The number of pages this collection should be split into. + /// A subset of this collection of objects, split into n pages. + public static IEnumerable> Split(this IEnumerable superset, int numberOfPages) + { + int take = Convert.ToInt32(Math.Ceiling(superset.Count() / (double)numberOfPages)); + + var result = new List>(); + + for (int i = 0; i < numberOfPages; i++) + { + var chunk = superset.Skip(i * take).Take(take).ToList(); + + if (chunk.Any()) + { + result.Add(chunk); + }; + } + + return result; + } + + /// + /// Splits a collection of objects into an unknown number of pages with n items per page (for example, + /// if I have a list of 45 shoes and say 'shoes.Partition(10)' I will now have 4 pages of 10 shoes and + /// 1 page of 5 shoes. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. + /// The maximum number of items each page may contain. + /// A subset of this collection of objects, split into pages of maximum size n. + public static IEnumerable> Partition(this IEnumerable superset, int pageSize) + { + // Cache this to avoid evaluating it twice + var count = superset.Count(); + + if (count < pageSize) + { + yield return superset; + } + else + { + var numberOfPages = Math.Ceiling(count / (double)pageSize); + + for (var i = 0; i < numberOfPages; i++) + { + yield return superset.Skip(pageSize * i).Take(pageSize); + } + } + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// A subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IEnumerable superset, int pageNumber, int pageSize) + { + return new PagedList(superset ?? new List(), pageNumber, pageSize); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// The total size of set + /// A subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount) + { + if (pageNumber < 1) + { + throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); + } + + if (pageSize < 1) + { + throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); + } + + if (superset == null) + { + return StaticPagedList.Empty(pageSize); + } + + var supersetCount = superset.Count(); + + if (supersetCount > totalSetCount) + { + throw new ArgumentOutOfRangeException($"superset count = {supersetCount} superset count cannot be more than {totalSetCount.Value}."); + } + + List subset; + + var totalCount = totalSetCount ?? supersetCount; + + if ((totalCount <= 0 || totalSetCount.HasValue) && supersetCount <= pageSize) + { + subset = superset.ToList(); + } + else + { + var skip = (pageNumber - 1) * pageSize; + + subset = superset.Skip(skip).Take(pageSize).ToList(); + } + + return new StaticPagedList(subset, pageNumber, pageSize, totalCount); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the + /// collection implements , it will be treated as such. + /// + /// A subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IEnumerable superset) + { + var supersetSize = superset?.Count() ?? 0; + var pageSize = supersetSize == 0 ? 1 : supersetSize; + + return new PagedList(superset ?? new List(), 1, pageSize); + } + + /// + /// Cast to Custom Type + /// + /// Source + /// Selector + /// Input Type + /// Result Type + /// New PagedList + public static IPagedList Select(this IPagedList source, Func selector) + { + var subset = ((IEnumerable)source).Select(selector); + + return new PagedList(source, subset); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// Type For Compare + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// Expression for Order + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// + /// A subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) + { + return new PagedList(superset, keySelector, pageNumber, pageSize); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// Type For Compare + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// Expression for Order + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IEnumerable superset, Expression> keySelector, int pageNumber, int pageSize) + { + return new PagedList(superset.AsQueryable(), keySelector, pageNumber, pageSize); + } + + /// + /// Async creates a subset of this collection of objects that can be individually accessed by index and + /// containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// The total size of set + /// + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount, CancellationToken cancellationToken) + { + return await Task.Factory.StartNew(() => + { + return ToPagedList(superset, pageNumber, pageSize, totalSetCount); + + }, cancellationToken); + } + + /// + /// Async creates a subset of this collection of objects that can be individually accessed by index and + /// containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// The total size of set + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount = null) + { + return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// + /// The total size of set + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IEnumerable superset, int pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null) + { + return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, cancellationToken); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// The collection of objects to be divided into subsets. If the collection + /// implements , it will be treated as such. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance. + /// + /// The maximum size of any individual subset. + /// The total size of set + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IEnumerable superset, int pageNumber, int pageSize, int? totalSetCount = null) + { + return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None); + } + + /// + /// Async creates a subset of this collection of objects that can be individually accessed by index (defaulting + /// to the first page) and containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance, defaulting to the first page. + /// + /// The maximum size of any individual subset. + /// + /// The total size of set + /// + /// A subset of this collection of objects that can be individually accessed by index and containing + /// metadata about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IQueryable superset, int? pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null) + { + return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, cancellationToken); + } + + /// + /// Async creates a subset of this collection of objects that can be individually accessed by index + /// (defaulting to the first page) and containing metadata about the collection of objects the subset + /// was created from. + /// + /// The type of object the collection should contain. + /// + /// + /// The one-based index of the subset of objects to be contained by this instance, + /// defaulting to the first page. + /// + /// The maximum size of any individual subset. + /// The total size of set + /// + /// A subset of this collection of objects that can be individually accessed by index and containing metadata + /// about the collection of objects the subset was created from. + /// + /// + public static async Task> ToPagedListAsync(this IQueryable superset, int? pageNumber, int pageSize, int? totalSetCount = null) + { + return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, CancellationToken.None); + } + + private static IQueryable AsQueryable(IQueryable superset) + { + return superset ?? new EnumerableQuery(new List()); + } + + private static IQueryable AsQueryable(IEnumerable superset) + { + return superset == null ? new EnumerableQuery(new List()) : superset.AsQueryable(); + } +} diff --git a/src/Moonglade.Web/PagedList/PagedListMetaData.cs b/src/Moonglade.Web/PagedList/PagedListMetaData.cs new file mode 100644 index 000000000..5025cf64a --- /dev/null +++ b/src/Moonglade.Web/PagedList/PagedListMetaData.cs @@ -0,0 +1,127 @@ + +namespace X.PagedList; + +/// +/// Non-enumerable version of the PagedList class. +/// +public class PagedListMetaData : IPagedList +{ + /// + /// Protected constructor that allows for instantiation without passing in a separate list. + /// + protected PagedListMetaData() + { + } + + /// + /// Non-enumerable version of the PagedList class. + /// + ///A PagedList (likely enumerable) to copy metadata from. + public PagedListMetaData(IPagedList pagedList) + { + PageCount = pagedList.PageCount; + TotalItemCount = pagedList.TotalItemCount; + PageNumber = pagedList.PageNumber; + PageSize = pagedList.PageSize; + HasPreviousPage = pagedList.HasPreviousPage; + HasNextPage = pagedList.HasNextPage; + IsFirstPage = pagedList.IsFirstPage; + IsLastPage = pagedList.IsLastPage; + FirstItemOnPage = pagedList.FirstItemOnPage; + LastItemOnPage = pagedList.LastItemOnPage; + } + + #region IPagedList Members + + /// + /// Total number of subsets within the superset. + /// + /// + /// Total number of subsets within the superset. + /// + public int PageCount { get; protected set; } + + /// + /// Total number of objects contained within the superset. + /// + /// + /// Total number of objects contained within the superset. + /// + public int TotalItemCount { get; protected set; } + + /// + /// One-based index of this subset within the superset, zero if the superset is empty. + /// + /// + /// One-based index of this subset within the superset, zero if the superset is empty. + /// + public int PageNumber { get; protected set; } + + /// + /// Maximum size any individual subset. + /// + /// + /// Maximum size any individual subset. + /// + public int PageSize { get; protected set; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the first subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the first subset within the superset. + /// + public bool HasPreviousPage { get; protected set; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the last subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is NOT the last subset within the superset. + /// + public bool HasNextPage { get; protected set; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the first subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and + /// this is the first subset within the superset. + /// + public bool IsFirstPage { get; protected set; } + + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and + /// this is the last subset within the superset. + /// + /// + /// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this + /// is the last subset within the superset. + /// + public bool IsLastPage { get; protected set; } + + /// + /// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + /// + /// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + public int FirstItemOnPage { get; protected set; } + + /// + /// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + /// + /// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber + /// is greater than PageCount. + /// + public int LastItemOnPage { get; protected set; } + + #endregion +} \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/StaticPagedList.cs b/src/Moonglade.Web/PagedList/StaticPagedList.cs new file mode 100644 index 000000000..bfeeb0988 --- /dev/null +++ b/src/Moonglade.Web/PagedList/StaticPagedList.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace X.PagedList; + +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// +/// Represents a subset of a collection of objects that can be individually accessed by index and containing +/// metadata about the superset collection of objects this subset was created from. +/// +/// The type of object the collection should contain. +/// +/// +/// +/// +public class StaticPagedList : BasePagedList +{ + /// + /// Initializes a new instance of the class that contains the already + /// divided subset and information about the size of the superset and the subset's position within it. + /// + /// The single subset this collection should represent. + /// + /// Supply the ".MetaData" property of an existing IPagedList instance to recreate + /// it here (such as when creating a new instance of a PagedList after having used Automapper to convert + /// its contents to a DTO.) + /// + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public StaticPagedList(IEnumerable subset, IPagedList metaData) + : this(subset, metaData.PageNumber, metaData.PageSize, metaData.TotalItemCount) + { + } + + /// + /// Initializes a new instance of the class that contains the already + /// divided subset and information about the size of the superset and the subset's position within it. + /// + /// The single subset this collection should represent. + /// The one-based index of the subset of objects contained by this instance. + /// The maximum size of any individual subset. + /// The size of the superset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public StaticPagedList(IEnumerable subset, int pageNumber, int pageSize, int totalItemCount) + : base(pageNumber, pageSize, totalItemCount) + { + Subset.AddRange(subset); + } + + /// + /// Create empty static paged list + /// + /// + /// + public static StaticPagedList Empty(int pageSize = DefaultPageSize) => + new(Array.Empty(), 1, pageSize, 0); +} \ No newline at end of file From 925302c6772f5fa3258ce13860932f6caec4524c Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:42:03 +0800 Subject: [PATCH 02/15] move namespace --- src/Moonglade.Web/PagedList/BasePagedList.cs | 6 ++---- src/Moonglade.Web/PagedList/HtmlHelper.cs | 1 - .../PagedList/HtmlHelperExtension.cs | 1 - src/Moonglade.Web/PagedList/IPagedList.cs | 4 +--- src/Moonglade.Web/PagedList/PagedList.cs | 13 +++++-------- .../PagedList/PagedListExtensions.cs | 18 ++++++------------ .../PagedList/PagedListMetaData.cs | 2 +- src/Moonglade.Web/PagedList/StaticPagedList.cs | 5 +---- .../Pages/Admin/Comments.cshtml.cs | 2 +- src/Moonglade.Web/Pages/Admin/Post.cshtml.cs | 2 +- src/Moonglade.Web/Pages/CategoryList.cshtml.cs | 2 +- src/Moonglade.Web/Pages/Featured.cshtml.cs | 2 +- src/Moonglade.Web/Pages/Index.cshtml.cs | 2 +- .../Pages/Shared/_PostList.cshtml | 2 +- src/Moonglade.Web/Pages/TagList.cshtml.cs | 2 +- 15 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/Moonglade.Web/PagedList/BasePagedList.cs b/src/Moonglade.Web/PagedList/BasePagedList.cs index 4563052e6..4bccd9f09 100644 --- a/src/Moonglade.Web/PagedList/BasePagedList.cs +++ b/src/Moonglade.Web/PagedList/BasePagedList.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections; -namespace X.PagedList; +namespace Moonglade.Web.PagedList; /// /// Represents a subset of a collection of objects that can be individually accessed by index and containing diff --git a/src/Moonglade.Web/PagedList/HtmlHelper.cs b/src/Moonglade.Web/PagedList/HtmlHelper.cs index a6ec9ac1e..19a80d5bf 100644 --- a/src/Moonglade.Web/PagedList/HtmlHelper.cs +++ b/src/Moonglade.Web/PagedList/HtmlHelper.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc.Rendering; using System.Collections.Immutable; -using X.PagedList; namespace Moonglade.Web.PagedList; diff --git a/src/Moonglade.Web/PagedList/HtmlHelperExtension.cs b/src/Moonglade.Web/PagedList/HtmlHelperExtension.cs index a66785c01..d1d7ef284 100644 --- a/src/Moonglade.Web/PagedList/HtmlHelperExtension.cs +++ b/src/Moonglade.Web/PagedList/HtmlHelperExtension.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using System.Web; -using X.PagedList; namespace Moonglade.Web.PagedList; diff --git a/src/Moonglade.Web/PagedList/IPagedList.cs b/src/Moonglade.Web/PagedList/IPagedList.cs index 44678052c..a02aa8283 100644 --- a/src/Moonglade.Web/PagedList/IPagedList.cs +++ b/src/Moonglade.Web/PagedList/IPagedList.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace X.PagedList; +namespace Moonglade.Web.PagedList; /// /// Represents a subset of a collection of objects that can be individually accessed by index and containing diff --git a/src/Moonglade.Web/PagedList/PagedList.cs b/src/Moonglade.Web/PagedList/PagedList.cs index 74bd8c66d..b3c3e8dca 100644 --- a/src/Moonglade.Web/PagedList/PagedList.cs +++ b/src/Moonglade.Web/PagedList/PagedList.cs @@ -1,9 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; +using System.Linq.Expressions; -namespace X.PagedList; +namespace Moonglade.Web.PagedList; public class PagedList : BasePagedList { @@ -89,7 +86,7 @@ public PagedList(IQueryable superset, int pageNumber, int pageSize) if (TotalItemCount > 0 && superset != null) { var skip = (pageNumber - 1) * pageSize; - + Subset.AddRange(superset.Skip(skip).Take(pageSize)); } } @@ -131,7 +128,7 @@ public PagedList(IPagedList pagedList, IEnumerable collection) LastItemOnPage = pagedList.LastItemOnPage; Subset.AddRange(collection); - + if (Subset.Count > PageSize) { throw new Exception($"{nameof(collection)} size can't be greater than PageSize"); @@ -144,6 +141,6 @@ public PagedList(IPagedList pagedList, IEnumerable collection) /// /// /// - public static PagedList Empty(int pageSize = DefaultPageSize) => + public static PagedList Empty(int pageSize = DefaultPageSize) => new(Array.Empty(), 1, pageSize); } \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/PagedListExtensions.cs b/src/Moonglade.Web/PagedList/PagedListExtensions.cs index 190195b5c..52a58d28e 100644 --- a/src/Moonglade.Web/PagedList/PagedListExtensions.cs +++ b/src/Moonglade.Web/PagedList/PagedListExtensions.cs @@ -1,12 +1,6 @@ -using JetBrains.Annotations; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; +using System.Linq.Expressions; -namespace X.PagedList; +namespace Moonglade.Web.PagedList; /// /// Container for extension methods designed to simplify the creation of instances of . @@ -125,14 +119,14 @@ public static IPagedList ToPagedList(this IQueryable superset, int page } var supersetCount = superset.Count(); - + if (supersetCount > totalSetCount) { throw new ArgumentOutOfRangeException($"superset count = {supersetCount} superset count cannot be more than {totalSetCount.Value}."); } List subset; - + var totalCount = totalSetCount ?? supersetCount; if ((totalCount <= 0 || totalSetCount.HasValue) && supersetCount <= pageSize) @@ -180,7 +174,7 @@ public static IPagedList ToPagedList(this IEnumerable superset) public static IPagedList Select(this IPagedList source, Func selector) { var subset = ((IEnumerable)source).Select(selector); - + return new PagedList(source, subset); } @@ -376,7 +370,7 @@ private static IQueryable AsQueryable(IQueryable superset) { return superset ?? new EnumerableQuery(new List()); } - + private static IQueryable AsQueryable(IEnumerable superset) { return superset == null ? new EnumerableQuery(new List()) : superset.AsQueryable(); diff --git a/src/Moonglade.Web/PagedList/PagedListMetaData.cs b/src/Moonglade.Web/PagedList/PagedListMetaData.cs index 5025cf64a..5d942494e 100644 --- a/src/Moonglade.Web/PagedList/PagedListMetaData.cs +++ b/src/Moonglade.Web/PagedList/PagedListMetaData.cs @@ -1,5 +1,5 @@  -namespace X.PagedList; +namespace Moonglade.Web.PagedList; /// /// Non-enumerable version of the PagedList class. diff --git a/src/Moonglade.Web/PagedList/StaticPagedList.cs b/src/Moonglade.Web/PagedList/StaticPagedList.cs index bfeeb0988..085af18f0 100644 --- a/src/Moonglade.Web/PagedList/StaticPagedList.cs +++ b/src/Moonglade.Web/PagedList/StaticPagedList.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -namespace X.PagedList; +namespace Moonglade.Web.PagedList; /// /// Represents a subset of a collection of objects that can be individually accessed by index and containing diff --git a/src/Moonglade.Web/Pages/Admin/Comments.cshtml.cs b/src/Moonglade.Web/Pages/Admin/Comments.cshtml.cs index d7b09deb9..b0f5eed72 100644 --- a/src/Moonglade.Web/Pages/Admin/Comments.cshtml.cs +++ b/src/Moonglade.Web/Pages/Admin/Comments.cshtml.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Mvc.RazorPages; -using X.PagedList; +using Moonglade.Web.PagedList; namespace Moonglade.Web.Pages.Admin; diff --git a/src/Moonglade.Web/Pages/Admin/Post.cshtml.cs b/src/Moonglade.Web/Pages/Admin/Post.cshtml.cs index 3e3fef550..e84b9234f 100644 --- a/src/Moonglade.Web/Pages/Admin/Post.cshtml.cs +++ b/src/Moonglade.Web/Pages/Admin/Post.cshtml.cs @@ -1,8 +1,8 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Moonglade.Core.PostFeature; using Moonglade.Data.Spec; +using Moonglade.Web.PagedList; using System.ComponentModel.DataAnnotations; -using X.PagedList; namespace Moonglade.Web.Pages.Admin; diff --git a/src/Moonglade.Web/Pages/CategoryList.cshtml.cs b/src/Moonglade.Web/Pages/CategoryList.cshtml.cs index 4bd80a0b9..a344269b2 100644 --- a/src/Moonglade.Web/Pages/CategoryList.cshtml.cs +++ b/src/Moonglade.Web/Pages/CategoryList.cshtml.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Moonglade.Core.CategoryFeature; using Moonglade.Core.PostFeature; -using X.PagedList; +using Moonglade.Web.PagedList; namespace Moonglade.Web.Pages; diff --git a/src/Moonglade.Web/Pages/Featured.cshtml.cs b/src/Moonglade.Web/Pages/Featured.cshtml.cs index 3f3a9ceba..b1014b4a6 100644 --- a/src/Moonglade.Web/Pages/Featured.cshtml.cs +++ b/src/Moonglade.Web/Pages/Featured.cshtml.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Moonglade.Core.PostFeature; -using X.PagedList; +using Moonglade.Web.PagedList; namespace Moonglade.Web.Pages; diff --git a/src/Moonglade.Web/Pages/Index.cshtml.cs b/src/Moonglade.Web/Pages/Index.cshtml.cs index af2bac093..c6ad7877a 100644 --- a/src/Moonglade.Web/Pages/Index.cshtml.cs +++ b/src/Moonglade.Web/Pages/Index.cshtml.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Moonglade.Core.PostFeature; using Moonglade.Data.Spec; -using X.PagedList; +using Moonglade.Web.PagedList; namespace Moonglade.Web.Pages; diff --git a/src/Moonglade.Web/Pages/Shared/_PostList.cshtml b/src/Moonglade.Web/Pages/Shared/_PostList.cshtml index 7d134b117..e091351c4 100644 --- a/src/Moonglade.Web/Pages/Shared/_PostList.cshtml +++ b/src/Moonglade.Web/Pages/Shared/_PostList.cshtml @@ -1,5 +1,5 @@ @using Moonglade.Web.PagedList -@model X.PagedList.StaticPagedList +@model StaticPagedList @if (Model.Count == 0) { diff --git a/src/Moonglade.Web/Pages/TagList.cshtml.cs b/src/Moonglade.Web/Pages/TagList.cshtml.cs index 436fbdf3b..359008c85 100644 --- a/src/Moonglade.Web/Pages/TagList.cshtml.cs +++ b/src/Moonglade.Web/Pages/TagList.cshtml.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Moonglade.Core.PostFeature; using Moonglade.Core.TagFeature; -using X.PagedList; +using Moonglade.Web.PagedList; namespace Moonglade.Web.Pages; From b75b31061f6c688e92de4e2ead31fe540015760f Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:42:31 +0800 Subject: [PATCH 03/15] remove unused code --- .../PagedList/GoToFormRenderOptions.cs | 57 ------------------- 1 file changed, 57 deletions(-) delete mode 100644 src/Moonglade.Web/PagedList/GoToFormRenderOptions.cs diff --git a/src/Moonglade.Web/PagedList/GoToFormRenderOptions.cs b/src/Moonglade.Web/PagedList/GoToFormRenderOptions.cs deleted file mode 100644 index a5c170c56..000000000 --- a/src/Moonglade.Web/PagedList/GoToFormRenderOptions.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace Moonglade.Web.PagedList; - -/// -/// Options for configuring the output of . -/// -public class GoToFormRenderOptions -{ - /// - /// The default settings, with configurable querystring key (input field name). - /// - public GoToFormRenderOptions(string inputFieldName) - { - LabelFormat = "Go to page:"; - SubmitButtonFormat = "Go"; - InputFieldName = inputFieldName; - InputFieldType = "number"; - } - - /// - /// The default settings. - /// - public GoToFormRenderOptions() : this("page") - { - } - - /// - /// The text to show in the form's input label. - /// - /// - /// "Go to page:" - /// - public string LabelFormat { get; set; } - - /// - /// The text to show in the form's submit button. - /// - /// - /// "Go" - /// - public string SubmitButtonFormat { get; set; } - - /// - /// The querystring key this form should submit the new page number as. - /// - /// - /// "page" - /// - public string InputFieldName { get; set; } - - /// - /// The HTML input type for this field. Defaults to the HTML5 "number" type, but can be changed to "text" if targetting previous versions of HTML. - /// - /// - /// "number" - /// - public string InputFieldType { get; set; } -} \ No newline at end of file From bd34e9ca4688ad6d6a938fc8efff3edf1f86c206 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:44:23 +0800 Subject: [PATCH 04/15] Delete PagedListExtensions.cs --- .../PagedList/PagedListExtensions.cs | 378 ------------------ 1 file changed, 378 deletions(-) delete mode 100644 src/Moonglade.Web/PagedList/PagedListExtensions.cs diff --git a/src/Moonglade.Web/PagedList/PagedListExtensions.cs b/src/Moonglade.Web/PagedList/PagedListExtensions.cs deleted file mode 100644 index 52a58d28e..000000000 --- a/src/Moonglade.Web/PagedList/PagedListExtensions.cs +++ /dev/null @@ -1,378 +0,0 @@ -using System.Linq.Expressions; - -namespace Moonglade.Web.PagedList; - -/// -/// Container for extension methods designed to simplify the creation of instances of . -/// -public static class PagedListExtensions -{ - /// - /// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and - /// say 'shoes.Split(5)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. - /// The number of pages this collection should be split into. - /// A subset of this collection of objects, split into n pages. - public static IEnumerable> Split(this IEnumerable superset, int numberOfPages) - { - int take = Convert.ToInt32(Math.Ceiling(superset.Count() / (double)numberOfPages)); - - var result = new List>(); - - for (int i = 0; i < numberOfPages; i++) - { - var chunk = superset.Skip(i * take).Take(take).ToList(); - - if (chunk.Any()) - { - result.Add(chunk); - }; - } - - return result; - } - - /// - /// Splits a collection of objects into an unknown number of pages with n items per page (for example, - /// if I have a list of 45 shoes and say 'shoes.Partition(10)' I will now have 4 pages of 10 shoes and - /// 1 page of 5 shoes. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. - /// The maximum number of items each page may contain. - /// A subset of this collection of objects, split into pages of maximum size n. - public static IEnumerable> Partition(this IEnumerable superset, int pageSize) - { - // Cache this to avoid evaluating it twice - var count = superset.Count(); - - if (count < pageSize) - { - yield return superset; - } - else - { - var numberOfPages = Math.Ceiling(count / (double)pageSize); - - for (var i = 0; i < numberOfPages; i++) - { - yield return superset.Skip(pageSize * i).Take(pageSize); - } - } - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// A subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IEnumerable superset, int pageNumber, int pageSize) - { - return new PagedList(superset ?? new List(), pageNumber, pageSize); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// The total size of set - /// A subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount) - { - if (pageNumber < 1) - { - throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); - } - - if (pageSize < 1) - { - throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); - } - - if (superset == null) - { - return StaticPagedList.Empty(pageSize); - } - - var supersetCount = superset.Count(); - - if (supersetCount > totalSetCount) - { - throw new ArgumentOutOfRangeException($"superset count = {supersetCount} superset count cannot be more than {totalSetCount.Value}."); - } - - List subset; - - var totalCount = totalSetCount ?? supersetCount; - - if ((totalCount <= 0 || totalSetCount.HasValue) && supersetCount <= pageSize) - { - subset = superset.ToList(); - } - else - { - var skip = (pageNumber - 1) * pageSize; - - subset = superset.Skip(skip).Take(pageSize).ToList(); - } - - return new StaticPagedList(subset, pageNumber, pageSize, totalCount); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the - /// collection implements , it will be treated as such. - /// - /// A subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IEnumerable superset) - { - var supersetSize = superset?.Count() ?? 0; - var pageSize = supersetSize == 0 ? 1 : supersetSize; - - return new PagedList(superset ?? new List(), 1, pageSize); - } - - /// - /// Cast to Custom Type - /// - /// Source - /// Selector - /// Input Type - /// Result Type - /// New PagedList - public static IPagedList Select(this IPagedList source, Func selector) - { - var subset = ((IEnumerable)source).Select(selector); - - return new PagedList(source, subset); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// Type For Compare - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// Expression for Order - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// - /// A subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) - { - return new PagedList(superset, keySelector, pageNumber, pageSize); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// Type For Compare - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// Expression for Order - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IEnumerable superset, Expression> keySelector, int pageNumber, int pageSize) - { - return new PagedList(superset.AsQueryable(), keySelector, pageNumber, pageSize); - } - - /// - /// Async creates a subset of this collection of objects that can be individually accessed by index and - /// containing metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// The total size of set - /// - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount, CancellationToken cancellationToken) - { - return await Task.Factory.StartNew(() => - { - return ToPagedList(superset, pageNumber, pageSize, totalSetCount); - - }, cancellationToken); - } - - /// - /// Async creates a subset of this collection of objects that can be individually accessed by index and - /// containing metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// The total size of set - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IQueryable superset, int pageNumber, int pageSize, int? totalSetCount = null) - { - return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// - /// The total size of set - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IEnumerable superset, int pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null) - { - return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, cancellationToken); - } - - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// The total size of set - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IEnumerable superset, int pageNumber, int pageSize, int? totalSetCount = null) - { - return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None); - } - - /// - /// Async creates a subset of this collection of objects that can be individually accessed by index (defaulting - /// to the first page) and containing metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance, defaulting to the first page. - /// - /// The maximum size of any individual subset. - /// - /// The total size of set - /// - /// A subset of this collection of objects that can be individually accessed by index and containing - /// metadata about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IQueryable superset, int? pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null) - { - return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, cancellationToken); - } - - /// - /// Async creates a subset of this collection of objects that can be individually accessed by index - /// (defaulting to the first page) and containing metadata about the collection of objects the subset - /// was created from. - /// - /// The type of object the collection should contain. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance, - /// defaulting to the first page. - /// - /// The maximum size of any individual subset. - /// The total size of set - /// - /// A subset of this collection of objects that can be individually accessed by index and containing metadata - /// about the collection of objects the subset was created from. - /// - /// - public static async Task> ToPagedListAsync(this IQueryable superset, int? pageNumber, int pageSize, int? totalSetCount = null) - { - return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, CancellationToken.None); - } - - private static IQueryable AsQueryable(IQueryable superset) - { - return superset ?? new EnumerableQuery(new List()); - } - - private static IQueryable AsQueryable(IEnumerable superset) - { - return superset == null ? new EnumerableQuery(new List()) : superset.AsQueryable(); - } -} From 9561c0bcbfa521388d580b51c588a4b4f5d46266 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:45:04 +0800 Subject: [PATCH 05/15] remove unused code --- src/Moonglade.Web/PagedList/PagedList.cs | 83 ------------------------ 1 file changed, 83 deletions(-) diff --git a/src/Moonglade.Web/PagedList/PagedList.cs b/src/Moonglade.Web/PagedList/PagedList.cs index b3c3e8dca..b576b084f 100644 --- a/src/Moonglade.Web/PagedList/PagedList.cs +++ b/src/Moonglade.Web/PagedList/PagedList.cs @@ -2,54 +2,6 @@ namespace Moonglade.Web.PagedList; -public class PagedList : BasePagedList -{ - /// - /// Initializes a new instance of the class that divides the supplied superset into - /// subsets the size of the supplied pageSize. The instance then only contains the objects contained in the - /// subset specified by index. - /// - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// Expression for Order - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public PagedList(IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) - : base(pageNumber, pageSize, superset?.Count() ?? 0) - { - // add items to internal list - if (TotalItemCount > 0) - { - InitSubset(superset, keySelector.Compile(), pageNumber, pageSize); - } - } - - public PagedList(IQueryable superset, Func keySelectorMethod, int pageNumber, int pageSize) - : base(pageNumber, pageSize, superset?.Count() ?? 0) - { - if (TotalItemCount > 0) - { - InitSubset(superset, keySelectorMethod, pageNumber, pageSize); - } - } - - private void InitSubset(IEnumerable superset, Func keySelectorMethod, int pageNumber, int pageSize) - { - // add items to internal list - - var skip = (pageNumber - 1) * pageSize; - var items = superset.OrderBy(keySelectorMethod).Skip(skip).Take(pageSize).ToList(); - - Subset.AddRange(items); - } -} - /// /// Represents a subset of a collection of objects that can be individually accessed by index and containing /// metadata about the superset collection of objects this subset was created from. @@ -108,39 +60,4 @@ public PagedList(IEnumerable superset, int pageNumber, int pageSize) : this(superset.AsQueryable(), pageNumber, pageSize) { } - - /// - /// For Clone PagedList - /// - /// Source PagedList - /// Source collection - public PagedList(IPagedList pagedList, IEnumerable collection) - { - TotalItemCount = pagedList.TotalItemCount; - PageSize = pagedList.PageSize; - PageNumber = pagedList.PageNumber; - PageCount = pagedList.PageCount; - HasPreviousPage = pagedList.HasPreviousPage; - HasNextPage = pagedList.HasNextPage; - IsFirstPage = pagedList.IsFirstPage; - IsLastPage = pagedList.IsLastPage; - FirstItemOnPage = pagedList.FirstItemOnPage; - LastItemOnPage = pagedList.LastItemOnPage; - - Subset.AddRange(collection); - - if (Subset.Count > PageSize) - { - throw new Exception($"{nameof(collection)} size can't be greater than PageSize"); - } - - } - - /// - /// Create empty static paged list - /// - /// - /// - public static PagedList Empty(int pageSize = DefaultPageSize) => - new(Array.Empty(), 1, pageSize); } \ No newline at end of file From d29692b68f146b95e05be2b8273c478750b93b2e Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:46:27 +0800 Subject: [PATCH 06/15] Update PagedList.cs --- src/Moonglade.Web/PagedList/PagedList.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Moonglade.Web/PagedList/PagedList.cs b/src/Moonglade.Web/PagedList/PagedList.cs index b576b084f..b789445ed 100644 --- a/src/Moonglade.Web/PagedList/PagedList.cs +++ b/src/Moonglade.Web/PagedList/PagedList.cs @@ -1,6 +1,4 @@ -using System.Linq.Expressions; - -namespace Moonglade.Web.PagedList; +namespace Moonglade.Web.PagedList; /// /// Represents a subset of a collection of objects that can be individually accessed by index and containing From a4e8bcd7ec309440348de0d656840b547bf1867c Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:48:27 +0800 Subject: [PATCH 07/15] remove comment --- src/Moonglade.Web/PagedList/PagedListDisplayMode.cs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Moonglade.Web/PagedList/PagedListDisplayMode.cs b/src/Moonglade.Web/PagedList/PagedListDisplayMode.cs index 30b70eea8..e43470a54 100644 --- a/src/Moonglade.Web/PagedList/PagedListDisplayMode.cs +++ b/src/Moonglade.Web/PagedList/PagedListDisplayMode.cs @@ -5,18 +5,7 @@ /// public enum PagedListDisplayMode { - /// - /// Always render. - /// Always, - - /// - /// Never render. - /// Never, - - /// - /// Only render when there is data that makes sense to show (context sensitive). - /// IfNeeded -} \ No newline at end of file +} From f4a1a8a9f1fd060791abbcae8ab8766fa1486ae7 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:49:43 +0800 Subject: [PATCH 08/15] Delete PagedList.cs --- src/Moonglade.Web/PagedList/PagedList.cs | 61 ------------------------ 1 file changed, 61 deletions(-) delete mode 100644 src/Moonglade.Web/PagedList/PagedList.cs diff --git a/src/Moonglade.Web/PagedList/PagedList.cs b/src/Moonglade.Web/PagedList/PagedList.cs deleted file mode 100644 index b789445ed..000000000 --- a/src/Moonglade.Web/PagedList/PagedList.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace Moonglade.Web.PagedList; - -/// -/// Represents a subset of a collection of objects that can be individually accessed by index and containing -/// metadata about the superset collection of objects this subset was created from. -/// -/// -/// Represents a subset of a collection of objects that can be individually accessed by index and containing -/// metadata about the superset collection of objects this subset was created from. -/// -/// The type of object the collection should contain. -/// -/// -/// -/// -public class PagedList : BasePagedList -{ - /// - /// Initializes a new instance of the class that divides the supplied superset - /// into subsets the size of the supplied pageSize. The instance then only contains the objects contained - /// in the subset specified by index. - /// - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// - /// The maximum size of any individual subset. - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public PagedList(IQueryable superset, int pageNumber, int pageSize) - : base(pageNumber, pageSize, superset?.Count() ?? 0) - { - if (TotalItemCount > 0 && superset != null) - { - var skip = (pageNumber - 1) * pageSize; - - Subset.AddRange(superset.Skip(skip).Take(pageSize)); - } - } - - /// - /// Initializes a new instance of the class that divides the supplied superset - /// into subsets the size of the supplied pageSize. The instance then only contains the objects contained in - /// the subset specified by index. - /// - /// - /// The collection of objects to be divided into subsets. If the collection - /// implements , it will be treated as such. - /// - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public PagedList(IEnumerable superset, int pageNumber, int pageSize) - : this(superset.AsQueryable(), pageNumber, pageSize) - { - } -} \ No newline at end of file From 9fbe754e78305683f227475bce48830cc05bcc49 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:50:42 +0800 Subject: [PATCH 09/15] remove unused code --- src/Moonglade.Web/PagedList/BasePagedList.cs | 9 --------- src/Moonglade.Web/PagedList/IPagedList.cs | 6 +----- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Moonglade.Web/PagedList/BasePagedList.cs b/src/Moonglade.Web/PagedList/BasePagedList.cs index 4bccd9f09..65f3f05f1 100644 --- a/src/Moonglade.Web/PagedList/BasePagedList.cs +++ b/src/Moonglade.Web/PagedList/BasePagedList.cs @@ -110,14 +110,5 @@ IEnumerator IEnumerable.GetEnumerator() /// public virtual int Count => Subset.Count; - /// - /// Gets a non-enumerable copy of this paged list. - /// - ///A non-enumerable copy of this paged list. - public PagedListMetaData GetMetaData() - { - return new PagedListMetaData(this); - } - #endregion } \ No newline at end of file diff --git a/src/Moonglade.Web/PagedList/IPagedList.cs b/src/Moonglade.Web/PagedList/IPagedList.cs index a02aa8283..1dfd2d83c 100644 --- a/src/Moonglade.Web/PagedList/IPagedList.cs +++ b/src/Moonglade.Web/PagedList/IPagedList.cs @@ -12,11 +12,7 @@ /// public interface IPagedList : IPagedList, IReadOnlyList { - /// - /// Gets a non-enumerable copy of this paged list. - /// - ///A non-enumerable copy of this paged list. - PagedListMetaData GetMetaData(); + } /// From fe80687f34bbaebd7140db0c35052eb4d96cc24f Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:52:07 +0800 Subject: [PATCH 10/15] remove unused method --- .../PagedList/StaticPagedList.cs | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/src/Moonglade.Web/PagedList/StaticPagedList.cs b/src/Moonglade.Web/PagedList/StaticPagedList.cs index 085af18f0..34cbe1ace 100644 --- a/src/Moonglade.Web/PagedList/StaticPagedList.cs +++ b/src/Moonglade.Web/PagedList/StaticPagedList.cs @@ -11,27 +11,9 @@ /// The type of object the collection should contain. /// /// -/// /// public class StaticPagedList : BasePagedList { - /// - /// Initializes a new instance of the class that contains the already - /// divided subset and information about the size of the superset and the subset's position within it. - /// - /// The single subset this collection should represent. - /// - /// Supply the ".MetaData" property of an existing IPagedList instance to recreate - /// it here (such as when creating a new instance of a PagedList after having used Automapper to convert - /// its contents to a DTO.) - /// - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public StaticPagedList(IEnumerable subset, IPagedList metaData) - : this(subset, metaData.PageNumber, metaData.PageSize, metaData.TotalItemCount) - { - } - /// /// Initializes a new instance of the class that contains the already /// divided subset and information about the size of the superset and the subset's position within it. @@ -47,12 +29,4 @@ public StaticPagedList(IEnumerable subset, int pageNumber, int pageSize, int { Subset.AddRange(subset); } - - /// - /// Create empty static paged list - /// - /// - /// - public static StaticPagedList Empty(int pageSize = DefaultPageSize) => - new(Array.Empty(), 1, pageSize, 0); } \ No newline at end of file From 87beab4a588404ca3d30cab8234511eda22c91fa Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:54:15 +0800 Subject: [PATCH 11/15] remove ITagBuilderFactory interface --- src/Moonglade.Web/PagedList/HtmlHelper.cs | 4 ++-- src/Moonglade.Web/PagedList/ITagBuilderFactory.cs | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Moonglade.Web/PagedList/HtmlHelper.cs b/src/Moonglade.Web/PagedList/HtmlHelper.cs index 19a80d5bf..6f75aee3c 100644 --- a/src/Moonglade.Web/PagedList/HtmlHelper.cs +++ b/src/Moonglade.Web/PagedList/HtmlHelper.cs @@ -5,9 +5,9 @@ namespace Moonglade.Web.PagedList; public class HtmlHelper { - private readonly ITagBuilderFactory _tagBuilderFactory; + private readonly TagBuilderFactory _tagBuilderFactory; - public HtmlHelper(ITagBuilderFactory tagBuilderFactory) + public HtmlHelper(TagBuilderFactory tagBuilderFactory) { _tagBuilderFactory = tagBuilderFactory; } diff --git a/src/Moonglade.Web/PagedList/ITagBuilderFactory.cs b/src/Moonglade.Web/PagedList/ITagBuilderFactory.cs index ed066d799..e62fddf48 100644 --- a/src/Moonglade.Web/PagedList/ITagBuilderFactory.cs +++ b/src/Moonglade.Web/PagedList/ITagBuilderFactory.cs @@ -2,12 +2,7 @@ namespace Moonglade.Web.PagedList; -public interface ITagBuilderFactory -{ - TagBuilder Create(string tagName); -} - -internal sealed class TagBuilderFactory : ITagBuilderFactory +public sealed class TagBuilderFactory { public TagBuilder Create(string tagName) => new(tagName); -} \ No newline at end of file +} From d9e49d166fad56e23969bb66fb2b13cb9a1f1df8 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:54:38 +0800 Subject: [PATCH 12/15] rename file --- .../PagedList/{ITagBuilderFactory.cs => TagBuilderFactory.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Moonglade.Web/PagedList/{ITagBuilderFactory.cs => TagBuilderFactory.cs} (100%) diff --git a/src/Moonglade.Web/PagedList/ITagBuilderFactory.cs b/src/Moonglade.Web/PagedList/TagBuilderFactory.cs similarity index 100% rename from src/Moonglade.Web/PagedList/ITagBuilderFactory.cs rename to src/Moonglade.Web/PagedList/TagBuilderFactory.cs From dfee8e66667f15812c4deb4326dd13e6e364ef90 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:56:54 +0800 Subject: [PATCH 13/15] Update BasePagedList.cs --- src/Moonglade.Web/PagedList/BasePagedList.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Moonglade.Web/PagedList/BasePagedList.cs b/src/Moonglade.Web/PagedList/BasePagedList.cs index 65f3f05f1..a59e6954e 100644 --- a/src/Moonglade.Web/PagedList/BasePagedList.cs +++ b/src/Moonglade.Web/PagedList/BasePagedList.cs @@ -17,8 +17,6 @@ public abstract class BasePagedList : PagedListMetaData, IPagedList { protected readonly List Subset = new(); - public const int DefaultPageSize = 100; - /// /// Parameterless constructor. /// From 9199e829b2ccf16290deabe91aa475ecbfd77af0 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:57:53 +0800 Subject: [PATCH 14/15] inline methods --- src/Moonglade.Web/PagedList/HtmlHelper.cs | 5 +++-- src/Moonglade.Web/PagedList/TagBuilderExtensions.cs | 10 ---------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Moonglade.Web/PagedList/HtmlHelper.cs b/src/Moonglade.Web/PagedList/HtmlHelper.cs index 6f75aee3c..50709b3d1 100644 --- a/src/Moonglade.Web/PagedList/HtmlHelper.cs +++ b/src/Moonglade.Web/PagedList/HtmlHelper.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc.Rendering; using System.Collections.Immutable; +using Microsoft.AspNetCore.Html; namespace Moonglade.Web.PagedList; @@ -16,12 +17,12 @@ public HtmlHelper(TagBuilderFactory tagBuilderFactory) private static void SetInnerText(TagBuilder tagBuilder, string innerText) { - tagBuilder.SetInnerText(innerText); + tagBuilder.InnerHtml.SetContent(innerText); } private static void AppendHtml(TagBuilder tagBuilder, string innerHtml) { - tagBuilder.AppendHtml(innerHtml); + tagBuilder.InnerHtml.AppendHtml(innerHtml); } private static string TagBuilderToString(TagBuilder tagBuilder) diff --git a/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs b/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs index 58fff82bb..9827b74cb 100644 --- a/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs +++ b/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs @@ -6,16 +6,6 @@ namespace Moonglade.Web.PagedList; public static class TagBuilderExtensions { - public static void AppendHtml(this TagBuilder tagBuilder, string innerHtml) - { - tagBuilder.InnerHtml.AppendHtml(innerHtml); - } - - public static void SetInnerText(this TagBuilder tagBuilder, string innerText) - { - tagBuilder.InnerHtml.SetContent(innerText); - } - public static string ToString(this TagBuilder tagBuilder, TagRenderMode renderMode, HtmlEncoder encoder = null) { encoder ??= HtmlEncoder.Create(new TextEncoderSettings()); From af62916a971f974dd64788fcae9c50804903aa54 Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 12 Oct 2023 13:58:54 +0800 Subject: [PATCH 15/15] inline methods --- src/Moonglade.Web/PagedList/HtmlHelper.cs | 9 +++++++-- .../PagedList/TagBuilderExtensions.cs | 18 ------------------ 2 files changed, 7 insertions(+), 20 deletions(-) delete mode 100644 src/Moonglade.Web/PagedList/TagBuilderExtensions.cs diff --git a/src/Moonglade.Web/PagedList/HtmlHelper.cs b/src/Moonglade.Web/PagedList/HtmlHelper.cs index 50709b3d1..9caaa8913 100644 --- a/src/Moonglade.Web/PagedList/HtmlHelper.cs +++ b/src/Moonglade.Web/PagedList/HtmlHelper.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc.Rendering; using System.Collections.Immutable; +using System.Text.Encodings.Web; using Microsoft.AspNetCore.Html; namespace Moonglade.Web.PagedList; @@ -27,8 +28,12 @@ private static void AppendHtml(TagBuilder tagBuilder, string innerHtml) private static string TagBuilderToString(TagBuilder tagBuilder) { - return tagBuilder - .ToString(TagRenderMode.Normal); + var encoder = HtmlEncoder.Create(new TextEncoderSettings()); + + using var writer = new StringWriter() as TextWriter; + tagBuilder.WriteTo(writer, encoder); + + return writer.ToString(); } private TagBuilder WrapInListItem(string text) diff --git a/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs b/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs deleted file mode 100644 index 9827b74cb..000000000 --- a/src/Moonglade.Web/PagedList/TagBuilderExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.AspNetCore.Html; -using Microsoft.AspNetCore.Mvc.Rendering; -using System.Text.Encodings.Web; - -namespace Moonglade.Web.PagedList; - -public static class TagBuilderExtensions -{ - public static string ToString(this TagBuilder tagBuilder, TagRenderMode renderMode, HtmlEncoder encoder = null) - { - encoder ??= HtmlEncoder.Create(new TextEncoderSettings()); - - using var writer = new StringWriter() as TextWriter; - tagBuilder.WriteTo(writer, encoder); - - return writer.ToString(); - } -} \ No newline at end of file