refactor: delegate id lookup to BlogService

This commit is contained in:
Oliver Booth 2023-08-08 11:35:22 +01:00
parent 6b18b36b96
commit 7f4ef10960
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
2 changed files with 31 additions and 34 deletions

View File

@ -1,23 +1,20 @@
using Humanizer; using Humanizer;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
using OliverBooth.Services;
namespace OliverBooth.Pages.Blog; namespace OliverBooth.Pages.Blog;
public class Index : PageModel public class Index : PageModel
{ {
private readonly IDbContextFactory<BlogContext> _dbContextFactory; private readonly BlogService _blogService;
public Index(IDbContextFactory<BlogContext> dbContextFactory) public Index(BlogService blogService)
{ {
_dbContextFactory = dbContextFactory; _blogService = blogService;
} }
public IReadOnlyCollection<BlogPost> BlogPosts { get; private set; } = ArraySegment<BlogPost>.Empty;
public string SanitizeContent(string content) public string SanitizeContent(string content)
{ {
content = content.Replace("<!--more-->", string.Empty); content = content.Replace("<!--more-->", string.Empty);
@ -38,35 +35,18 @@ public class Index : PageModel
return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256); 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) public IActionResult OnGet([FromQuery(Name = "p")] int? postId = null)
{ {
using BlogContext context = _dbContextFactory.CreateDbContext(); if (!postId.HasValue) return Page();
if (postId is null) if (!_blogService.TryGetWordPressBlogPost(postId.Value, out BlogPost? post)) return NotFound();
var route = new
{ {
BlogPosts = context.BlogPosts.ToArray(); year = post.Published.Year,
return Page(); month = post.Published.Month,
} day = post.Published.Day,
slug = post.Slug
BlogPost? post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId); };
return Redirect(Url.Page("/Blog/Article", route)!);
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();
} }
} }

View File

@ -74,4 +74,21 @@ public sealed class BlogService
return post is not null; return post is not null;
} }
/// <summary>
/// Attempts to find a blog post by its publication date and slug.
/// </summary>
/// <param name="postId">The WordPress ID 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 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;
}
} }