2023-08-12 20:13:47 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-08-13 17:33:54 +01:00
|
|
|
using Humanizer;
|
|
|
|
using Markdig;
|
2023-08-12 20:13:47 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Data.Blog;
|
2023-08-12 20:13:47 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
namespace OliverBooth.Services;
|
2023-08-12 20:13:47 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an implementation of <see cref="IBlogPostService" />.
|
|
|
|
/// </summary>
|
|
|
|
internal sealed class BlogPostService : IBlogPostService
|
|
|
|
{
|
|
|
|
private readonly IDbContextFactory<BlogContext> _dbContextFactory;
|
2023-08-13 17:33:54 +01:00
|
|
|
private readonly IBlogUserService _blogUserService;
|
|
|
|
private readonly MarkdownPipeline _markdownPipeline;
|
2023-08-12 20:13:47 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BlogPostService" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbContextFactory">
|
|
|
|
/// The <see cref="IDbContextFactory{TContext}" /> used to create a <see cref="BlogContext" />.
|
|
|
|
/// </param>
|
2023-08-13 17:33:54 +01:00
|
|
|
/// <param name="blogUserService">The <see cref="IBlogUserService" />.</param>
|
|
|
|
/// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param>
|
|
|
|
public BlogPostService(IDbContextFactory<BlogContext> dbContextFactory,
|
|
|
|
IBlogUserService blogUserService,
|
|
|
|
MarkdownPipeline markdownPipeline)
|
2023-08-12 20:13:47 +01:00
|
|
|
{
|
|
|
|
_dbContextFactory = dbContextFactory;
|
2023-08-13 17:33:54 +01:00
|
|
|
_blogUserService = blogUserService;
|
|
|
|
_markdownPipeline = markdownPipeline;
|
2023-08-12 20:13:47 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
public int GetBlogPostCount()
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
return context.BlogPosts.Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-08-12 20:13:47 +01:00
|
|
|
public IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
2023-08-13 17:33:54 +01:00
|
|
|
IQueryable<BlogPost> ordered = context.BlogPosts.OrderByDescending(post => post.Published);
|
|
|
|
if (limit > -1)
|
|
|
|
{
|
|
|
|
ordered = ordered.Take(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ordered.AsEnumerable().Select(CacheAuthor).ToArray();
|
2023-08-12 20:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public IReadOnlyList<IBlogPost> GetBlogPosts(int page, int pageSize = 10)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
return context.BlogPosts
|
|
|
|
.OrderByDescending(post => post.Published)
|
|
|
|
.Skip(page * pageSize)
|
|
|
|
.Take(pageSize)
|
2023-08-13 17:33:54 +01:00
|
|
|
.ToArray().Select(CacheAuthor).ToArray();
|
2023-08-12 20:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string RenderExcerpt(IBlogPost post, out bool wasTrimmed)
|
|
|
|
{
|
2023-08-13 17:33:54 +01:00
|
|
|
string body = post.Body;
|
|
|
|
int moreIndex = body.IndexOf("<!--more-->", StringComparison.Ordinal);
|
|
|
|
|
|
|
|
if (moreIndex == -1)
|
|
|
|
{
|
|
|
|
string excerpt = body.Truncate(255, "...");
|
|
|
|
wasTrimmed = body.Length > 255;
|
|
|
|
return Markdig.Markdown.ToHtml(excerpt, _markdownPipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
wasTrimmed = true;
|
|
|
|
return Markdig.Markdown.ToHtml(body[..moreIndex], _markdownPipeline);
|
2023-08-12 20:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string RenderPost(IBlogPost post)
|
|
|
|
{
|
2023-08-13 17:33:54 +01:00
|
|
|
return Markdig.Markdown.ToHtml(post.Body, _markdownPipeline);
|
2023-08-12 20:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryGetPost(Guid id, [NotNullWhen(true)] out IBlogPost? post)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
post = context.BlogPosts.Find(id);
|
|
|
|
if (post is null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheAuthor((BlogPost)post);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryGetPost(int id, [NotNullWhen(true)] out IBlogPost? post)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == id);
|
|
|
|
if (post is null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheAuthor((BlogPost)post);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryGetPost(DateOnly publishDate, string slug, [NotNullWhen(true)] out IBlogPost? post)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
post = context.BlogPosts.FirstOrDefault(post => post.Published.Year == publishDate.Year &&
|
|
|
|
post.Published.Month == publishDate.Month &&
|
|
|
|
post.Published.Day == publishDate.Day &&
|
|
|
|
post.Slug == slug);
|
|
|
|
|
|
|
|
if (post is null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheAuthor((BlogPost)post);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private BlogPost CacheAuthor(BlogPost post)
|
|
|
|
{
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
|
|
|
if (post.Author is not null)
|
|
|
|
{
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
if (_blogUserService.TryGetUser(post.AuthorId, out IUser? user) && user is IBlogAuthor author)
|
2023-08-12 20:13:47 +01:00
|
|
|
{
|
|
|
|
post.Author = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
}
|