From 7f4ef10960e081481225646e35bf7e9b411496f8 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 8 Aug 2023 11:35:22 +0100 Subject: [PATCH] refactor: delegate id lookup to BlogService --- OliverBooth/Pages/Blog/Index.cshtml.cs | 48 ++++++++------------------ OliverBooth/Services/BlogService.cs | 17 +++++++++ 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/OliverBooth/Pages/Blog/Index.cshtml.cs b/OliverBooth/Pages/Blog/Index.cshtml.cs index 56a1306..26e4ca9 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml.cs +++ b/OliverBooth/Pages/Blog/Index.cshtml.cs @@ -1,23 +1,20 @@ using Humanizer; 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; public class Index : PageModel { - private readonly IDbContextFactory _dbContextFactory; + private readonly BlogService _blogService; - public Index(IDbContextFactory dbContextFactory) + public Index(BlogService blogService) { - _dbContextFactory = dbContextFactory; + _blogService = blogService; } - public IReadOnlyCollection BlogPosts { get; private set; } = ArraySegment.Empty; - public string SanitizeContent(string content) { content = content.Replace("", string.Empty); @@ -38,35 +35,18 @@ public class Index : PageModel return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256); } - public Author? GetAuthor(BlogPost post) - { - using BlogContext context = _dbContextFactory.CreateDbContext(); - return context.Authors.FirstOrDefault(a => a.Id == post.AuthorId); - } - public IActionResult OnGet([FromQuery(Name = "p")] int? postId = null) { - using BlogContext context = _dbContextFactory.CreateDbContext(); - if (postId is null) + if (!postId.HasValue) return Page(); + if (!_blogService.TryGetWordPressBlogPost(postId.Value, out BlogPost? post)) return NotFound(); + + var route = new { - BlogPosts = context.BlogPosts.ToArray(); - return Page(); - } - - BlogPost? post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId); - - if (post is not null) - { - var route = new - { - year = post.Published.Year, - month = post.Published.Month, - day = post.Published.Day, - slug = post.Slug - }; - return Redirect(Url.Page("/Blog/Article", route)!); - } - - return NotFound(); + year = post.Published.Year, + month = post.Published.Month, + day = post.Published.Day, + slug = post.Slug + }; + return Redirect(Url.Page("/Blog/Article", route)!); } } diff --git a/OliverBooth/Services/BlogService.cs b/OliverBooth/Services/BlogService.cs index 2d9ded4..5069ef6 100644 --- a/OliverBooth/Services/BlogService.cs +++ b/OliverBooth/Services/BlogService.cs @@ -74,4 +74,21 @@ public sealed class BlogService return post is not null; } + + /// + /// Attempts to find a blog post by its publication date and slug. + /// + /// The WordPress ID 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 TryGetWordPressBlogPost(int postId, [NotNullWhen(true)] out BlogPost? post) + { + using BlogContext context = _dbContextFactory.CreateDbContext(); + post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId); + return post is not null; + } }