feat: add links to next and prev articles

This commit is contained in:
Oliver Booth 2023-09-19 19:29:21 +01:00
parent 2fd4b704cd
commit d114870f87
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
3 changed files with 71 additions and 0 deletions

View File

@ -1,6 +1,8 @@
@page "/blog/{year:int}/{month:int}/{day:int}/{slug}" @page "/blog/{year:int}/{month:int}/{day:int}/{slug}"
@using Humanizer @using Humanizer
@using OliverBooth.Data.Blog @using OliverBooth.Data.Blog
@using OliverBooth.Services
@inject IBlogPostService BlogPostService
@model Article @model Article
@if (Model.Post is not { } post) @if (Model.Post is not { } post)
@ -54,6 +56,41 @@
<hr> <hr>
<div class="row">
<div class="col-sm-12 col-md-6">
@if (BlogPostService.GetPreviousPost(post) is { } previousPost)
{
<small>Previous Post</small>
<p class="lead">
<a asp-page="Article"
asp-route-year="@previousPost.Published.Year.ToString("0000")"
asp-route-month="@previousPost.Published.Month.ToString("00")"
asp-route-day="@previousPost.Published.Day.ToString("00")"
asp-route-slug="@previousPost.Slug">
@previousPost.Title
</a>
</p>
}
</div>
<div class="col-sm-12 col-md-6" style="text-align: right;">
@if (BlogPostService.GetNextPost(post) is { } nextPost)
{
<small>Next Post</small>
<p class="lead">
<a asp-page="Article"
asp-route-year="@nextPost.Published.Year.ToString("0000")"
asp-route-month="@nextPost.Published.Month.ToString("00")"
asp-route-day="@nextPost.Published.Day.ToString("00")"
asp-route-slug="@nextPost.Slug">
@nextPost.Title
</a>
</p>
}
</div>
</div>
<hr>
@if (post.EnableComments) @if (post.EnableComments)
{ {
<div id="disqus_thread"></div> <div id="disqus_thread"></div>

View File

@ -66,6 +66,26 @@ internal sealed class BlogPostService : IBlogPostService
.ToArray().Select(CacheAuthor).ToArray(); .ToArray().Select(CacheAuthor).ToArray();
} }
/// <inheritdoc />
public IBlogPost? GetNextPost(IBlogPost blogPost)
{
using BlogContext context = _dbContextFactory.CreateDbContext();
return context.BlogPosts
.Where(p => p.Visibility == BlogPostVisibility.Published)
.OrderBy(post => post.Published)
.FirstOrDefault(post => post.Published > blogPost.Published);
}
/// <inheritdoc />
public IBlogPost? GetPreviousPost(IBlogPost blogPost)
{
using BlogContext context = _dbContextFactory.CreateDbContext();
return context.BlogPosts
.Where(p => p.Visibility == BlogPostVisibility.Published)
.OrderByDescending(post => post.Published)
.FirstOrDefault(post => post.Published < blogPost.Published);
}
/// <inheritdoc /> /// <inheritdoc />
public string RenderExcerpt(IBlogPost post, out bool wasTrimmed) public string RenderExcerpt(IBlogPost post, out bool wasTrimmed)
{ {

View File

@ -34,6 +34,20 @@ public interface IBlogPostService
/// <returns>A collection of blog posts.</returns> /// <returns>A collection of blog posts.</returns>
IReadOnlyList<IBlogPost> GetBlogPosts(int page, int pageSize = 10); IReadOnlyList<IBlogPost> GetBlogPosts(int page, int pageSize = 10);
/// <summary>
/// Returns the next blog post from the specified blog post.
/// </summary>
/// <param name="blogPost">The blog post whose next post to return.</param>
/// <returns>The next blog post from the specified blog post.</returns>
IBlogPost? GetNextPost(IBlogPost blogPost);
/// <summary>
/// Returns the previous blog post from the specified blog post.
/// </summary>
/// <param name="blogPost">The blog post whose previous post to return.</param>
/// <returns>The previous blog post from the specified blog post.</returns>
IBlogPost? GetPreviousPost(IBlogPost blogPost);
/// <summary> /// <summary>
/// Renders the excerpt of the specified blog post. /// Renders the excerpt of the specified blog post.
/// </summary> /// </summary>