Skip to content

Commit ac3f546

Browse files
committed
Added pagination links for prev & next. See #4
1 parent 0afa644 commit ac3f546

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/RazorBlog/Models/PostList.cs

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ namespace RazorBlog.Models
1212
{
1313
public sealed class PostList
1414
{
15+
public sealed class ListPagination
16+
{
17+
/// <summary>
18+
/// Gets/sets if the archive has a prev link.
19+
/// </summary>
20+
public bool HasPrev { get; set; }
21+
22+
/// <summary>
23+
/// Gets/sets if the archive has a next link.
24+
/// </summary>
25+
public bool HasNext { get; set; }
26+
27+
/// <summary>
28+
/// Gets/sets the prev link.
29+
/// </summary>
30+
public string PrevLink { get; set; }
31+
32+
/// <summary>
33+
/// Gets/sets the next link.
34+
/// </summary>
35+
public string NextLink { get; set; }
36+
}
37+
1538
/// <summary>
1639
/// Gets/sets the available posts.
1740
/// </summary>
@@ -46,5 +69,10 @@ public sealed class PostList
4669
/// Gets/set the total page count.
4770
/// </summary>
4871
public int PageCount { get; set; }
72+
73+
/// <summary>
74+
/// Gets/sets the current pagination.
75+
/// </summary>
76+
public ListPagination Pagination { get; set; } = new ListPagination();
4977
}
5078
}

src/RazorBlog/Services/BlogService.cs

+13
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,25 @@ public virtual async Task<PostList> GetArchive(int page = 1, string category = n
153153
page = model.PageCount;
154154
model.Page = page;
155155

156+
// Setup pagination
157+
if (model.Page > 1)
158+
{
159+
model.Pagination.HasPrev = true;
160+
model.Pagination.PrevLink = $"{Settings.ArchiveSlug}/page/{Math.Max(model.Page - 1, 1)}";
161+
}
162+
if (model.Page < model.PageCount)
163+
{
164+
model.Pagination.HasNext = true;
165+
model.Pagination.NextLink = $"{Settings.ArchiveSlug}/page/{Math.Min(model.Page + 1, model.PageCount)}";
166+
}
167+
156168
model.Items = await query
157169
.OrderByDescending(p => p.Published)
158170
.Skip((page - 1) * Settings.PageSize)
159171
.Take(Settings.PageSize)
160172
.ToArrayAsync();
161173

174+
// Get comment count and order tags
162175
foreach (var post in model.Items)
163176
{
164177
post.CommentCount = await _db.Comments.CountAsync(c => c.PostId == post.Id && c.IsApproved);

src/RazorTemplate/Pages/Themes/Persona/Pages/_Archive.cshtml

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@page
22

3-
<main role="main" class="container">
3+
<main role="main" class="container archive">
44
<div class="row">
55
<div class="col blog-filter-header">
66
<h1>Latest posts</h1>
@@ -28,4 +28,22 @@
2828
</div>
2929
</article>
3030
}
31+
32+
@if (Blog.Archive.PageCount > 1)
33+
{
34+
<div class="row justify-content-center">
35+
<div class="col-sm-10">
36+
<div class="pagination d-block">
37+
@if (Blog.Archive.Pagination.HasPrev)
38+
{
39+
<a class="btn btn-link float-left" href="@Blog.Archive.Pagination.PrevLink">Newer posts</a>
40+
}
41+
@if (Blog.Archive.Pagination.HasNext)
42+
{
43+
<a class="btn btn-link float-right" href="@Blog.Archive.Pagination.NextLink">Older posts</a>
44+
}
45+
</div>
46+
</div>
47+
</div>
48+
}
3149
</main>

src/RazorTemplate/Pages/Themes/Persona/Scss/_base.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ footer {
130130
.archive {
131131
article {
132132
padding: 2rem 0 1rem;
133-
border-bottom: solid 1px $border_color;;
133+
border-bottom: solid 1px $border_color;
134134

135135
&:first-child {
136136
padding-top: 0;

0 commit comments

Comments
 (0)