refactor: delegate post lookup to BlogService
This commit is contained in:
parent
4a94a404b3
commit
69a6f4a3af
@ -1,24 +1,35 @@
|
||||
using Markdig;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OliverBooth.Data;
|
||||
using OliverBooth.Data.Blog;
|
||||
using OliverBooth.Services;
|
||||
|
||||
namespace OliverBooth.Pages.Blog;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the page model for the <c>Article</c> page.
|
||||
/// </summary>
|
||||
public class Article : PageModel
|
||||
{
|
||||
private readonly IDbContextFactory<BlogContext> _dbContextFactory;
|
||||
private readonly BlogService _blogService;
|
||||
private readonly MarkdownPipeline _markdownPipeline;
|
||||
|
||||
public Article(IDbContextFactory<BlogContext> dbContextFactory, MarkdownPipeline markdownPipeline)
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Article" /> class.
|
||||
/// </summary>
|
||||
/// <param name="blogService">The <see cref="BlogService" />.</param>
|
||||
/// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param>
|
||||
public Article(BlogService blogService, MarkdownPipeline markdownPipeline)
|
||||
{
|
||||
_dbContextFactory = dbContextFactory;
|
||||
_blogService = blogService;
|
||||
_markdownPipeline = markdownPipeline;
|
||||
}
|
||||
|
||||
public Author Author { get; private set; }
|
||||
/// <summary>
|
||||
/// Gets the author of the post.
|
||||
/// </summary>
|
||||
/// <value>The author of the post.</value>
|
||||
public Author Author { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the post is a legacy WordPress post.
|
||||
@ -26,10 +37,19 @@ public class Article : PageModel
|
||||
/// <value>
|
||||
/// <see langword="true" /> if the post is a legacy WordPress post; otherwise, <see langword="false" />.
|
||||
/// </value>
|
||||
public bool IsWordPressLegacyPost => Post?.WordPressId.HasValue ?? false;
|
||||
public bool IsWordPressLegacyPost => Post.WordPressId.HasValue;
|
||||
|
||||
public BlogPost Post { get; private set; } = new();
|
||||
/// <summary>
|
||||
/// Gets the requested blog post.
|
||||
/// </summary>
|
||||
/// <value>The requested blog post.</value>
|
||||
public BlogPost Post { get; private set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Sanitizes the content of the blog post.
|
||||
/// </summary>
|
||||
/// <param name="content">The content of the blog post.</param>
|
||||
/// <returns>The sanitized content of the blog post.</returns>
|
||||
public string SanitizeContent(string content)
|
||||
{
|
||||
content = content.Replace("<!--more-->", string.Empty);
|
||||
@ -44,20 +64,14 @@ public class Article : PageModel
|
||||
|
||||
public IActionResult OnGet(int year, int month, int day, string slug)
|
||||
{
|
||||
using BlogContext context = _dbContextFactory.CreateDbContext();
|
||||
Post = context.BlogPosts.FirstOrDefault(p => p.Published.Year == year &&
|
||||
p.Published.Month == month &&
|
||||
p.Published.Day == day &&
|
||||
p.Slug == slug)!;
|
||||
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
||||
if (Post is null)
|
||||
if (!_blogService.TryGetBlogPost(year, month, day, slug, out BlogPost? post))
|
||||
{
|
||||
Response.StatusCode = 404;
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Author = context.Authors.FirstOrDefault(a => a.Id == Post.AuthorId)!;
|
||||
Post = post;
|
||||
Author = _blogService.TryGetAuthor(post, out Author? author) ? author : null!;
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OliverBooth.Data;
|
||||
using OliverBooth.Data.Blog;
|
||||
|
||||
@ -29,4 +30,48 @@ public sealed class BlogService
|
||||
return context.BlogPosts.OrderByDescending(p => p.Published).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to find the author of a blog post.
|
||||
/// </summary>
|
||||
/// <param name="post">The blog post.</param>
|
||||
/// <param name="author">
|
||||
/// When this method returns, contains the <see cref="Author" /> associated with the specified blog post, if the
|
||||
/// author is found; otherwise, <see langword="null" />.
|
||||
/// <returns><see langword="true" /> if the author is found; otherwise, <see langword="false" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="post" /> is <see langword="null" />.</exception>
|
||||
public bool TryGetAuthor(BlogPost post, [NotNullWhen(true)] out Author? author)
|
||||
{
|
||||
if (post is null) throw new ArgumentNullException(nameof(post));
|
||||
|
||||
using BlogContext context = _dbContextFactory.CreateDbContext();
|
||||
author = context.Authors.FirstOrDefault(a => a.Id == post.AuthorId);
|
||||
|
||||
return author is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to find a blog post by its publication date and slug.
|
||||
/// </summary>
|
||||
/// <param name="year">The year the post was published.</param>
|
||||
/// <param name="month">The month the post was published.</param>
|
||||
/// <param name="day">The day the post was published.</param>
|
||||
/// <param name="slug">The slug of the post.</param>
|
||||
/// <param name="post">
|
||||
/// When this method returns, contains the <see cref="BlogPost" /> associated with the specified publication
|
||||
/// date and slug, if the post is found; otherwise, <see langword="null" />.
|
||||
/// </param>
|
||||
/// <returns><see langword="true" /> if the post is found; otherwise, <see langword="false" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="slug" /> is <see langword="null" />.</exception>
|
||||
public bool TryGetBlogPost(int year, int month, int day, string slug, [NotNullWhen(true)] out BlogPost? post)
|
||||
{
|
||||
if (slug is null) throw new ArgumentNullException(nameof(slug));
|
||||
|
||||
using BlogContext context = _dbContextFactory.CreateDbContext();
|
||||
post = context.BlogPosts.FirstOrDefault(p =>
|
||||
p.Published.Year == year && p.Published.Month == month && p.Published.Day == day &&
|
||||
p.Slug == slug);
|
||||
|
||||
return post is not null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user