diff --git a/Pages/Components/PaginatedPostListButtons/Default.cshtml b/Pages/Components/PaginatedPostListButtons/Default.cshtml new file mode 100644 index 0000000..4a389be --- /dev/null +++ b/Pages/Components/PaginatedPostListButtons/Default.cshtml @@ -0,0 +1,21 @@ +@using QCVault.Utilities.Entities; +@model PaginatedList + +@{ + var prevDisabled = !Model.HasPreviousPage ? "disabled" : ""; + var nextDisabled = !Model.HasNextPage ? "disabled" : ""; +} + + +
+ + Previous + + + Next + +
diff --git a/Pages/Components/PaginatedPostListButtons/Default.cshtml.cs b/Pages/Components/PaginatedPostListButtons/Default.cshtml.cs new file mode 100644 index 0000000..92931f7 --- /dev/null +++ b/Pages/Components/PaginatedPostListButtons/Default.cshtml.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace QCVault.Pages.Components.PaginatedButtons +{ + public class DefaultModel : PageModel + { + public void OnGet() + { + } + } +} diff --git a/Pages/PostList.cshtml b/Pages/PostList.cshtml index 3f0759a..31fc3a9 100644 --- a/Pages/PostList.cshtml +++ b/Pages/PostList.cshtml @@ -10,4 +10,7 @@ + + + diff --git a/Pages/PostList.cshtml.cs b/Pages/PostList.cshtml.cs index 55e0381..04e3966 100644 --- a/Pages/PostList.cshtml.cs +++ b/Pages/PostList.cshtml.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; @@ -17,7 +14,7 @@ public class PostListModel : PageModel private readonly ILogger logger; private readonly IPostLoader postLoader; - public IEnumerable Posts { get; set; } + public PaginatedList Posts { get; set; } public PostListModel(ILogger logger, IPostLoader postLoader) { @@ -27,7 +24,7 @@ public PostListModel(ILogger logger, IPostLoader postLoader) public IActionResult OnGet(int? pageNumber) { - Posts = postLoader.VisiblePosts(); + Posts = PaginatedList.Create(postLoader.VisiblePosts().AsQueryable(), pageNumber ?? 1, 5); return Page(); } } diff --git a/Utilities/Entities/PaginatedList.cs b/Utilities/Entities/PaginatedList.cs new file mode 100644 index 0000000..41a6513 --- /dev/null +++ b/Utilities/Entities/PaginatedList.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace QCVault.Utilities.Entities +{ + public class PaginatedList : List + { + public int PageIndex { get; private set; } + public int TotalPages { get; private set; } + + public PaginatedList(List items, int count, int pageIndex, int pageSize) + { + PageIndex = pageIndex; + TotalPages = (int)Math.Ceiling(count / (double)pageSize); + + this.AddRange(items); + } + + public bool HasPreviousPage => PageIndex > 1; + + public bool HasNextPage => PageIndex < TotalPages; + + public static PaginatedList Create(IQueryable source, int pageIndex, int pageSize) + { + var count = source.Count(); + var items = source.Skip(( pageIndex - 1 ) * pageSize).Take(pageSize).ToList(); + return new PaginatedList(items, count, pageIndex, pageSize); + } + } +} diff --git a/ViewComponents/PaginatedPostListButton.cs b/ViewComponents/PaginatedPostListButton.cs new file mode 100644 index 0000000..c747763 --- /dev/null +++ b/ViewComponents/PaginatedPostListButton.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Mvc; +using QCVault.Utilities.Entities; + +namespace QCVault.Components +{ + public class PaginatedPostListButtonsViewComponent : ViewComponent + { + public PaginatedPostListButtonsViewComponent() + { + } + + public IViewComponentResult Invoke(PaginatedList paginatedPosts) + { + return View("Default", paginatedPosts); + } + } +} \ No newline at end of file