From 69a6f4a3afdc6e9d4cced304f82efda575f2a582 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 8 Aug 2023 02:06:11 +0100 Subject: [PATCH] refactor: delegate post lookup to BlogService --- OliverBooth/Pages/Blog/Article.cshtml.cs | 48 +++++++++++++++--------- OliverBooth/Services/BlogService.cs | 47 ++++++++++++++++++++++- 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/OliverBooth/Pages/Blog/Article.cshtml.cs b/OliverBooth/Pages/Blog/Article.cshtml.cs index 0d1166e..b6c1086 100644 --- a/OliverBooth/Pages/Blog/Article.cshtml.cs +++ b/OliverBooth/Pages/Blog/Article.cshtml.cs @@ -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; +/// +/// Represents the page model for the Article page. +/// public class Article : PageModel { - private readonly IDbContextFactory _dbContextFactory; + private readonly BlogService _blogService; private readonly MarkdownPipeline _markdownPipeline; - public Article(IDbContextFactory dbContextFactory, MarkdownPipeline markdownPipeline) + /// + /// Initializes a new instance of the class. + /// + /// The . + /// The . + public Article(BlogService blogService, MarkdownPipeline markdownPipeline) { - _dbContextFactory = dbContextFactory; + _blogService = blogService; _markdownPipeline = markdownPipeline; } - public Author Author { get; private set; } + /// + /// Gets the author of the post. + /// + /// The author of the post. + public Author Author { get; private set; } = null!; /// /// Gets a value indicating whether the post is a legacy WordPress post. @@ -26,10 +37,19 @@ public class Article : PageModel /// /// if the post is a legacy WordPress post; otherwise, . /// - public bool IsWordPressLegacyPost => Post?.WordPressId.HasValue ?? false; + public bool IsWordPressLegacyPost => Post.WordPressId.HasValue; - public BlogPost Post { get; private set; } = new(); + /// + /// Gets the requested blog post. + /// + /// The requested blog post. + public BlogPost Post { get; private set; } = null!; + /// + /// Sanitizes the content of the blog post. + /// + /// The content of the blog post. + /// The sanitized content of the blog post. public string SanitizeContent(string content) { content = content.Replace("", 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(); } } diff --git a/OliverBooth/Services/BlogService.cs b/OliverBooth/Services/BlogService.cs index dc005d2..2d9ded4 100644 --- a/OliverBooth/Services/BlogService.cs +++ b/OliverBooth/Services/BlogService.cs @@ -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(); } } + + /// + /// Attempts to find the author of a blog post. + /// + /// The blog post. + /// + /// When this method returns, contains the associated with the specified blog post, if the + /// author is found; otherwise, . + /// if the author is found; otherwise, . + /// is . + 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; + } + + /// + /// Attempts to find a blog post by its publication date and slug. + /// + /// The year the post was published. + /// The month the post was published. + /// The day the post was published. + /// The slug of the post. + /// + /// When this method returns, contains the associated with the specified publication + /// date and slug, if the post is found; otherwise, . + /// + /// if the post is found; otherwise, . + /// is . + 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; + } }