refactor: delegate id lookup to BlogService
This commit is contained in:
parent
6b18b36b96
commit
7f4ef10960
@ -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,25 +35,11 @@ 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();
|
||||||
{
|
|
||||||
BlogPosts = context.BlogPosts.ToArray();
|
|
||||||
return Page();
|
|
||||||
}
|
|
||||||
|
|
||||||
BlogPost? post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId);
|
|
||||||
|
|
||||||
if (post is not null)
|
|
||||||
{
|
|
||||||
var route = new
|
var route = new
|
||||||
{
|
{
|
||||||
year = post.Published.Year,
|
year = post.Published.Year,
|
||||||
@ -66,7 +49,4 @@ public class Index : PageModel
|
|||||||
};
|
};
|
||||||
return Redirect(Url.Page("/Blog/Article", route)!);
|
return Redirect(Url.Page("/Blog/Article", route)!);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user