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;
+ }
}