From 850a1df00b4570dc6b5744cec28fddb98eaa38a7 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Tue, 13 Jul 2021 18:02:36 +0200 Subject: [PATCH 1/6] Added Pagination for BlogPosts --- LinkDotNet.Blog.Web/Pages/Index.razor | 17 ++++++-- .../Shared/BlogPostNavigation.razor | 40 +++++++++++++++++++ .../LinkDotNet.Infrastructure.csproj | 1 + .../Persistence/IRepository.cs | 10 ++++- .../InMemory/InMemoryRepository.cs | 14 +++++-- .../Persistence/RavenDb/BlogPostRepository.cs | 15 ++++--- .../Persistence/Sql/BlogPostRepository.cs | 15 ++++--- 7 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor diff --git a/LinkDotNet.Blog.Web/Pages/Index.razor b/LinkDotNet.Blog.Web/Pages/Index.razor index 09d9c8a1..0cefb4ae 100644 --- a/LinkDotNet.Blog.Web/Pages/Index.razor +++ b/LinkDotNet.Blog.Web/Pages/Index.razor @@ -2,6 +2,7 @@ @using LinkDotNet.Domain @using LinkDotNet.Infrastructure.Persistence @using Markdig +@using X.PagedList @inject IRepository _repository @inject AppConfiguration _appConfiguration @inject NavigationManager _navigationManager @@ -13,17 +14,19 @@
- @for (var i = 0; i < _blogPosts.Count; i++) + @for (var i = 0; i < _currentPage.Count; i++) { - + }
+ @code { - IList _blogPosts = new List(); + IPagedList _currentPage = new PagedList(Array.Empty(), 1, 1); + protected override async Task OnInitializedAsync() { - _blogPosts = (await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate)).ToList(); + _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1); } private string GetAbsolutePreviewImageUrl() @@ -42,4 +45,10 @@ { return Uri.TryCreate(url, UriKind.Absolute, out _); } + + private async Task GetPage(int newPage) + { + _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1, page: + newPage); + } } \ No newline at end of file diff --git a/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor b/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor new file mode 100644 index 00000000..eac2f7a4 --- /dev/null +++ b/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor @@ -0,0 +1,40 @@ +@using LinkDotNet.Domain +@using X.PagedList + + + @code { + [Parameter] + public IPagedList CurrentPage { get; set; } + + [Parameter] + public EventCallback OnPageChanged { get; set; } + + private async Task PageHasChanged(int newPage) + { + await OnPageChanged.InvokeAsync(newPage); + } + + private async Task PreviousPage() + { + await PageHasChanged(CurrentPage.PageCount - 1); + } + + private async Task NextPage() + { + await PageHasChanged(CurrentPage.PageCount + 1); + } + } \ No newline at end of file diff --git a/LinkDotNet.Infrastructure/LinkDotNet.Infrastructure.csproj b/LinkDotNet.Infrastructure/LinkDotNet.Infrastructure.csproj index 00da4b91..f225d1f4 100644 --- a/LinkDotNet.Infrastructure/LinkDotNet.Infrastructure.csproj +++ b/LinkDotNet.Infrastructure/LinkDotNet.Infrastructure.csproj @@ -15,6 +15,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/LinkDotNet.Infrastructure/Persistence/IRepository.cs b/LinkDotNet.Infrastructure/Persistence/IRepository.cs index 46986a6f..5176de32 100644 --- a/LinkDotNet.Infrastructure/Persistence/IRepository.cs +++ b/LinkDotNet.Infrastructure/Persistence/IRepository.cs @@ -1,8 +1,8 @@ using System; -using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using LinkDotNet.Domain; +using X.PagedList; namespace LinkDotNet.Infrastructure.Persistence { @@ -10,7 +10,13 @@ public interface IRepository { Task GetByIdAsync(string blogPostId); - Task> GetAllAsync(Expression> filter = null, Expression> orderBy = null, bool descending = true); + Task> GetAllAsync( + Expression> filter = null, + Expression> orderBy = null, + bool descending = true, + int page = 1, + int pageSize = 5); Task StoreAsync(BlogPost blogPost); diff --git a/LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs b/LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs index e1335759..847010b0 100644 --- a/LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs +++ b/LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs @@ -4,6 +4,7 @@ using System.Linq.Expressions; using System.Threading.Tasks; using LinkDotNet.Domain; +using X.PagedList; namespace LinkDotNet.Infrastructure.Persistence.InMemory { @@ -17,7 +18,12 @@ public Task GetByIdAsync(string blogPostId) return Task.FromResult(blogPost); } - public Task> GetAllAsync(Expression> filter = null, Expression> orderBy = null, bool descending = true) + public Task> GetAllAsync( + Expression> filter = null, + Expression> orderBy = null, + bool descending = true, + int page = 1, + int pageSize = 5) { var result = blogPosts.AsEnumerable(); if (filter != null) @@ -29,13 +35,13 @@ public Task> GetAllAsync(Expression> { if (descending) { - return Task.FromResult(result.OrderByDescending(orderBy.Compile()).AsEnumerable()); + return Task.FromResult(result.OrderByDescending(orderBy.Compile()).ToPagedList(page, pageSize)); } - return Task.FromResult(result.OrderBy(orderBy.Compile()).AsEnumerable()); + return Task.FromResult(result.OrderBy(orderBy.Compile()).ToPagedList(page, pageSize)); } - return Task.FromResult(blogPosts.AsEnumerable()); + return Task.FromResult(blogPosts.ToPagedList(page, pageSize)); } public Task StoreAsync(BlogPost blogPost) diff --git a/LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs b/LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs index 5885758c..f2d9341b 100644 --- a/LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs +++ b/LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs @@ -1,10 +1,10 @@ using System; -using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using LinkDotNet.Domain; using Raven.Client.Documents; using Raven.Client.Documents.Linq; +using X.PagedList; namespace LinkDotNet.Infrastructure.Persistence.RavenDb { @@ -23,7 +23,12 @@ public async Task GetByIdAsync(string blogPostId) return await session.LoadAsync(blogPostId); } - public async Task> GetAllAsync(Expression> filter = null, Expression> orderBy = null, bool descending = true) + public async Task> GetAllAsync( + Expression> filter = null, + Expression> orderBy = null, + bool descending = true, + int page = 1, + int pageSize = 5) { using var session = documentStore.OpenAsyncSession(); @@ -37,13 +42,13 @@ public async Task> GetAllAsync(Expression GetByIdAsync(string blogPostId) return await blogPostContext.BlogPosts.Include(b => b.Tags).SingleAsync(b => b.Id == blogPostId); } - public async Task> GetAllAsync(Expression> filter = null, Expression> orderBy = null, bool descending = true) + public async Task> GetAllAsync( + Expression> filter = null, + Expression> orderBy = null, + bool descending = true, + int page = 1, + int pageSize = 5) { var blogPosts = blogPostContext.BlogPosts.AsNoTracking().Include(b => b.Tags).AsQueryable(); @@ -35,13 +40,13 @@ public async Task> GetAllAsync(Expression Date: Tue, 13 Jul 2021 18:14:45 +0200 Subject: [PATCH 2/6] Corrected Pagination --- LinkDotNet.Blog.Web/Pages/Index.razor | 6 +++--- .../Shared/BlogPostNavigation.razor | 15 ++++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/LinkDotNet.Blog.Web/Pages/Index.razor b/LinkDotNet.Blog.Web/Pages/Index.razor index 0cefb4ae..b6db063d 100644 --- a/LinkDotNet.Blog.Web/Pages/Index.razor +++ b/LinkDotNet.Blog.Web/Pages/Index.razor @@ -19,14 +19,14 @@ } - + @code { IPagedList _currentPage = new PagedList(Array.Empty(), 1, 1); protected override async Task OnInitializedAsync() { - _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1); + _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 3); } private string GetAbsolutePreviewImageUrl() @@ -48,7 +48,7 @@ private async Task GetPage(int newPage) { - _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1, page: + _currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 3, page: newPage); } } \ No newline at end of file diff --git a/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor b/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor index eac2f7a4..16ef475e 100644 --- a/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor +++ b/LinkDotNet.Blog.Web/Shared/BlogPostNavigation.razor @@ -3,14 +3,11 @@