Skip to content

Commit

Permalink
Added pagination links for prev & next. See #4
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Jul 21, 2018
1 parent 0afa644 commit ac3f546
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/RazorBlog/Models/PostList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ namespace RazorBlog.Models
{
public sealed class PostList
{
public sealed class ListPagination
{
/// <summary>
/// Gets/sets if the archive has a prev link.
/// </summary>
public bool HasPrev { get; set; }

/// <summary>
/// Gets/sets if the archive has a next link.
/// </summary>
public bool HasNext { get; set; }

/// <summary>
/// Gets/sets the prev link.
/// </summary>
public string PrevLink { get; set; }

/// <summary>
/// Gets/sets the next link.
/// </summary>
public string NextLink { get; set; }
}

/// <summary>
/// Gets/sets the available posts.
/// </summary>
Expand Down Expand Up @@ -46,5 +69,10 @@ public sealed class PostList
/// Gets/set the total page count.
/// </summary>
public int PageCount { get; set; }

/// <summary>
/// Gets/sets the current pagination.
/// </summary>
public ListPagination Pagination { get; set; } = new ListPagination();
}
}
13 changes: 13 additions & 0 deletions src/RazorBlog/Services/BlogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,25 @@ public virtual async Task<PostList> GetArchive(int page = 1, string category = n
page = model.PageCount;
model.Page = page;

// Setup pagination
if (model.Page > 1)
{
model.Pagination.HasPrev = true;
model.Pagination.PrevLink = $"{Settings.ArchiveSlug}/page/{Math.Max(model.Page - 1, 1)}";
}
if (model.Page < model.PageCount)
{
model.Pagination.HasNext = true;
model.Pagination.NextLink = $"{Settings.ArchiveSlug}/page/{Math.Min(model.Page + 1, model.PageCount)}";
}

model.Items = await query
.OrderByDescending(p => p.Published)
.Skip((page - 1) * Settings.PageSize)
.Take(Settings.PageSize)
.ToArrayAsync();

// Get comment count and order tags
foreach (var post in model.Items)
{
post.CommentCount = await _db.Comments.CountAsync(c => c.PostId == post.Id && c.IsApproved);
Expand Down
20 changes: 19 additions & 1 deletion src/RazorTemplate/Pages/Themes/Persona/Pages/_Archive.cshtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@page

<main role="main" class="container">
<main role="main" class="container archive">
<div class="row">
<div class="col blog-filter-header">
<h1>Latest posts</h1>
Expand Down Expand Up @@ -28,4 +28,22 @@
</div>
</article>
}

@if (Blog.Archive.PageCount > 1)
{
<div class="row justify-content-center">
<div class="col-sm-10">
<div class="pagination d-block">
@if (Blog.Archive.Pagination.HasPrev)
{
<a class="btn btn-link float-left" href="@Blog.Archive.Pagination.PrevLink">Newer posts</a>
}
@if (Blog.Archive.Pagination.HasNext)
{
<a class="btn btn-link float-right" href="@Blog.Archive.Pagination.NextLink">Older posts</a>
}
</div>
</div>
</div>
}
</main>
2 changes: 1 addition & 1 deletion src/RazorTemplate/Pages/Themes/Persona/Scss/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ footer {
.archive {
article {
padding: 2rem 0 1rem;
border-bottom: solid 1px $border_color;;
border-bottom: solid 1px $border_color;

&:first-child {
padding-top: 0;
Expand Down

0 comments on commit ac3f546

Please sign in to comment.