2023-08-08 12:46:24 +01:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using Humanizer;
|
|
|
|
|
using Markdig;
|
2023-08-08 02:06:11 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-08 01:30:32 +01:00
|
|
|
|
using OliverBooth.Data;
|
|
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
|
|
|
|
|
|
namespace OliverBooth.Services;
|
|
|
|
|
|
|
|
|
|
public sealed class BlogService
|
|
|
|
|
{
|
2023-08-08 12:25:28 +01:00
|
|
|
|
private readonly IDbContextFactory<BlogContext> _dbContextFactory;
|
2023-08-08 12:46:24 +01:00
|
|
|
|
private readonly MarkdownPipeline _markdownPipeline;
|
2023-08-08 01:30:32 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="BlogService" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dbContextFactory">The <see cref="IDbContextFactory{TContext}" />.</param>
|
2023-08-08 12:46:24 +01:00
|
|
|
|
/// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param>
|
|
|
|
|
public BlogService(IDbContextFactory<BlogContext> dbContextFactory, MarkdownPipeline markdownPipeline)
|
2023-08-08 01:30:32 +01:00
|
|
|
|
{
|
|
|
|
|
_dbContextFactory = dbContextFactory;
|
2023-08-08 12:46:24 +01:00
|
|
|
|
_markdownPipeline = markdownPipeline;
|
2023-08-08 01:30:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a read-only view of all blog posts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A read-only view of all blog posts.</returns>
|
|
|
|
|
public IReadOnlyCollection<BlogPost> AllPosts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
|
return context.BlogPosts.OrderByDescending(p => p.Published).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-08 02:06:11 +01:00
|
|
|
|
|
2023-08-08 12:46:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the processed content of a blog post.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="post">The blog post.</param>
|
|
|
|
|
/// <returns>The processed content of the blog post.</returns>
|
|
|
|
|
public string GetContent(BlogPost post)
|
|
|
|
|
{
|
|
|
|
|
return ProcessContent(post.Body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the processed excerpt of a blog post.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="post">The blog post.</param>
|
|
|
|
|
/// <param name="trimmed">
|
|
|
|
|
/// When this method returns, contains <see langword="true" /> if the content was trimmed; otherwise,
|
|
|
|
|
/// <see langword="false" />.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns>The processed excerpt of the blog post.</returns>
|
|
|
|
|
public string GetExcerpt(BlogPost post, out bool trimmed)
|
|
|
|
|
{
|
|
|
|
|
ReadOnlySpan<char> span = post.Body.AsSpan();
|
|
|
|
|
int moreIndex = span.IndexOf("<!--more-->", StringComparison.Ordinal);
|
|
|
|
|
trimmed = moreIndex != -1 || span.Length > 256;
|
|
|
|
|
string result = moreIndex != -1 ? span[..moreIndex].Trim().ToString() : post.Body.Truncate(256);
|
|
|
|
|
return ProcessContent(result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-08 02:06:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find the author of a blog post.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="post">The blog post.</param>
|
|
|
|
|
/// <param name="author">
|
|
|
|
|
/// When this method returns, contains the <see cref="Author" /> associated with the specified blog post, if the
|
|
|
|
|
/// author is found; otherwise, <see langword="null" />.
|
|
|
|
|
/// <returns><see langword="true" /> if the author is found; otherwise, <see langword="false" />.</returns>
|
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="post" /> is <see langword="null" />.</exception>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find a blog post by its publication date and slug.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="year">The year the post was published.</param>
|
|
|
|
|
/// <param name="month">The month the post was published.</param>
|
|
|
|
|
/// <param name="day">The day the post was published.</param>
|
|
|
|
|
/// <param name="slug">The slug 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 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;
|
|
|
|
|
}
|
2023-08-08 11:35:22 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-08-08 12:40:51 +01:00
|
|
|
|
/// Attempts to find a blog post by new ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="postId">The new ID of the post.</param>
|
|
|
|
|
/// <param name="post">
|
|
|
|
|
/// When this method returns, contains the <see cref="BlogPost" /> associated with ID, if the post is found;
|
|
|
|
|
/// otherwise, <see langword="null" />.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns><see langword="true" /> if the post is found; otherwise, <see langword="false" />.</returns>
|
|
|
|
|
public bool TryGetBlogPost(int postId, [NotNullWhen(true)] out BlogPost? post)
|
|
|
|
|
{
|
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
|
post = context.BlogPosts.FirstOrDefault(p => p.Id == postId);
|
|
|
|
|
return post is not null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to find a blog post by its legacy WordPress ID.
|
2023-08-08 11:35:22 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="postId">The WordPress ID of the post.</param>
|
|
|
|
|
/// <param name="post">
|
2023-08-08 12:40:51 +01:00
|
|
|
|
/// When this method returns, contains the <see cref="BlogPost" /> associated with ID, if the post is found;
|
|
|
|
|
/// otherwise, <see langword="null" />.
|
2023-08-08 11:35:22 +01:00
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns><see langword="true" /> if the post is found; otherwise, <see langword="false" />.</returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-08-08 12:46:24 +01:00
|
|
|
|
|
|
|
|
|
private string ProcessContent(string content)
|
|
|
|
|
{
|
|
|
|
|
content = content.Replace("<!--more-->", string.Empty);
|
|
|
|
|
|
|
|
|
|
while (content.Contains("\n\n"))
|
|
|
|
|
{
|
|
|
|
|
content = content.Replace("\n\n", "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Markdown.ToHtml(content.Trim(), _markdownPipeline);
|
|
|
|
|
}
|
2023-08-08 01:30:32 +01:00
|
|
|
|
}
|