Skip to content

Commit 850a1df

Browse files
committed
Added Pagination for BlogPosts
1 parent a491137 commit 850a1df

File tree

7 files changed

+92
-20
lines changed

7 files changed

+92
-20
lines changed

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@using LinkDotNet.Domain
33
@using LinkDotNet.Infrastructure.Persistence
44
@using Markdig
5+
@using X.PagedList
56
@inject IRepository _repository
67
@inject AppConfiguration _appConfiguration
78
@inject NavigationManager _navigationManager
@@ -13,17 +14,19 @@
1314
<IntroductionCard Introduction="_appConfiguration.Introduction"></IntroductionCard>
1415

1516
<div class="content px-4">
16-
@for (var i = 0; i < _blogPosts.Count; i++)
17+
@for (var i = 0; i < _currentPage.Count; i++)
1718
{
18-
<ShortBlogPost BlogPost="_blogPosts[i]" UseAlternativeStyle="@(i % 2 != 0)"></ShortBlogPost>
19+
<ShortBlogPost BlogPost="_currentPage[i]" UseAlternativeStyle="@(i % 2 != 0)"></ShortBlogPost>
1920
}
2021
</div>
22+
<BlogPostNavigation CurrentPage="@_currentPage" OnPageChanged="GetPage"></BlogPostNavigation>
2123
</section>
2224
@code {
23-
IList<BlogPost> _blogPosts = new List<BlogPost>();
25+
IPagedList<BlogPost> _currentPage = new PagedList<BlogPost>(Array.Empty<BlogPost>(), 1, 1);
26+
2427
protected override async Task OnInitializedAsync()
2528
{
26-
_blogPosts = (await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate)).ToList();
29+
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1);
2730
}
2831

2932
private string GetAbsolutePreviewImageUrl()
@@ -42,4 +45,10 @@
4245
{
4346
return Uri.TryCreate(url, UriKind.Absolute, out _);
4447
}
48+
49+
private async Task GetPage(int newPage)
50+
{
51+
_currentPage = await _repository.GetAllAsync(p => p.IsPublished, b => b.UpdatedDate, pageSize: 1, page:
52+
newPage);
53+
}
4554
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@using LinkDotNet.Domain
2+
@using X.PagedList
3+
<nav aria-label="Page navigation example">
4+
<ul class="pagination justify-content-center">
5+
<li class="page-item @(!CurrentPage.IsFirstPage ? string.Empty : "disabled")">
6+
<a class="page-link" href="#" tabindex="-1">Previous</a>
7+
</li>
8+
<li class="page-item"><a class="page-link @(!CurrentPage.IsFirstPage ? string.Empty : "disabled")"
9+
href="#"
10+
@onclick="PreviousPage">(@CurrentPage.PageNumber - 1)</a></li>
11+
<li class="page-item">
12+
<a class="page-link @(!CurrentPage.IsLastPage ? string.Empty : "disabled")"
13+
@onclick="NextPage">
14+
href="#">Next</a>
15+
</li>
16+
</ul>
17+
</nav>
18+
19+
@code {
20+
[Parameter]
21+
public IPagedList<BlogPost> CurrentPage { get; set; }
22+
23+
[Parameter]
24+
public EventCallback<int> OnPageChanged { get; set; }
25+
26+
private async Task PageHasChanged(int newPage)
27+
{
28+
await OnPageChanged.InvokeAsync(newPage);
29+
}
30+
31+
private async Task PreviousPage()
32+
{
33+
await PageHasChanged(CurrentPage.PageCount - 1);
34+
}
35+
36+
private async Task NextPage()
37+
{
38+
await PageHasChanged(CurrentPage.PageCount + 1);
39+
}
40+
}

LinkDotNet.Infrastructure/LinkDotNet.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18+
<PackageReference Include="X.PagedList" Version="8.1.0" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

LinkDotNet.Infrastructure/Persistence/IRepository.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq.Expressions;
43
using System.Threading.Tasks;
54
using LinkDotNet.Domain;
5+
using X.PagedList;
66

77
namespace LinkDotNet.Infrastructure.Persistence
88
{
99
public interface IRepository
1010
{
1111
Task<BlogPost> GetByIdAsync(string blogPostId);
1212

13-
Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true);
13+
Task<IPagedList<BlogPost>> GetAllAsync(
14+
Expression<Func<BlogPost, bool>> filter = null,
15+
Expression<Func<BlogPost,
16+
object>> orderBy = null,
17+
bool descending = true,
18+
int page = 1,
19+
int pageSize = 5);
1420

1521
Task StoreAsync(BlogPost blogPost);
1622

LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using System.Threading.Tasks;
66
using LinkDotNet.Domain;
7+
using X.PagedList;
78

89
namespace LinkDotNet.Infrastructure.Persistence.InMemory
910
{
@@ -17,7 +18,12 @@ public Task<BlogPost> GetByIdAsync(string blogPostId)
1718
return Task.FromResult(blogPost);
1819
}
1920

20-
public Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
21+
public Task<IPagedList<BlogPost>> GetAllAsync(
22+
Expression<Func<BlogPost, bool>> filter = null,
23+
Expression<Func<BlogPost, object>> orderBy = null,
24+
bool descending = true,
25+
int page = 1,
26+
int pageSize = 5)
2127
{
2228
var result = blogPosts.AsEnumerable();
2329
if (filter != null)
@@ -29,13 +35,13 @@ public Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>>
2935
{
3036
if (descending)
3137
{
32-
return Task.FromResult(result.OrderByDescending(orderBy.Compile()).AsEnumerable());
38+
return Task.FromResult(result.OrderByDescending(orderBy.Compile()).ToPagedList(page, pageSize));
3339
}
3440

35-
return Task.FromResult(result.OrderBy(orderBy.Compile()).AsEnumerable());
41+
return Task.FromResult(result.OrderBy(orderBy.Compile()).ToPagedList(page, pageSize));
3642
}
3743

38-
return Task.FromResult(blogPosts.AsEnumerable());
44+
return Task.FromResult(blogPosts.ToPagedList(page, pageSize));
3945
}
4046

4147
public Task StoreAsync(BlogPost blogPost)

LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq.Expressions;
43
using System.Threading.Tasks;
54
using LinkDotNet.Domain;
65
using Raven.Client.Documents;
76
using Raven.Client.Documents.Linq;
7+
using X.PagedList;
88

99
namespace LinkDotNet.Infrastructure.Persistence.RavenDb
1010
{
@@ -23,7 +23,12 @@ public async Task<BlogPost> GetByIdAsync(string blogPostId)
2323
return await session.LoadAsync<BlogPost>(blogPostId);
2424
}
2525

26-
public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
26+
public async Task<IPagedList<BlogPost>> GetAllAsync(
27+
Expression<Func<BlogPost, bool>> filter = null,
28+
Expression<Func<BlogPost, object>> orderBy = null,
29+
bool descending = true,
30+
int page = 1,
31+
int pageSize = 5)
2732
{
2833
using var session = documentStore.OpenAsyncSession();
2934

@@ -37,13 +42,13 @@ public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, b
3742
{
3843
if (descending)
3944
{
40-
return await query.OrderByDescending(orderBy).ToListAsync();
45+
return await query.OrderByDescending(orderBy).ToPagedListAsync(page, pageSize);
4146
}
4247

43-
return await query.OrderBy(orderBy).ToListAsync();
48+
return await query.OrderBy(orderBy).ToPagedListAsync(page, pageSize);
4449
}
4550

46-
return await query.ToListAsync();
51+
return await query.ToPagedListAsync(page, pageSize);
4752
}
4853

4954
public async Task StoreAsync(BlogPost blogPost)

LinkDotNet.Infrastructure/Persistence/Sql/BlogPostRepository.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Linq.Expressions;
54
using System.Threading.Tasks;
65
using LinkDotNet.Domain;
76
using Microsoft.EntityFrameworkCore;
7+
using X.PagedList;
88

99
namespace LinkDotNet.Infrastructure.Persistence.Sql
1010
{
@@ -22,7 +22,12 @@ public async Task<BlogPost> GetByIdAsync(string blogPostId)
2222
return await blogPostContext.BlogPosts.Include(b => b.Tags).SingleAsync(b => b.Id == blogPostId);
2323
}
2424

25-
public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
25+
public async Task<IPagedList<BlogPost>> GetAllAsync(
26+
Expression<Func<BlogPost, bool>> filter = null,
27+
Expression<Func<BlogPost, object>> orderBy = null,
28+
bool descending = true,
29+
int page = 1,
30+
int pageSize = 5)
2631
{
2732
var blogPosts = blogPostContext.BlogPosts.AsNoTracking().Include(b => b.Tags).AsQueryable();
2833

@@ -35,13 +40,13 @@ public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, b
3540
{
3641
if (descending)
3742
{
38-
return await blogPosts.OrderByDescending(orderBy).ToListAsync();
43+
return await blogPosts.OrderByDescending(orderBy).ToPagedListAsync(page, pageSize);
3944
}
4045

41-
return await blogPosts.OrderBy(orderBy).ToListAsync();
46+
return await blogPosts.OrderBy(orderBy).ToPagedListAsync(page, pageSize);
4247
}
4348

44-
return await blogPosts.ToListAsync();
49+
return await blogPosts.ToPagedListAsync(page, pageSize);
4550
}
4651

4752
public async Task StoreAsync(BlogPost blogPost)

0 commit comments

Comments
 (0)