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;
|
2024-02-24 14:52:43 +00:00
|
|
|
using OliverBooth.Data.Web;
|
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;
|
2024-02-24 14:52:43 +00:00
|
|
|
private readonly IUserService _userService;
|
2023-08-13 17:33:54 +01:00
|
|
|
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>
|
2024-02-24 14:52:43 +00:00
|
|
|
/// <param name="userService">The <see cref="IUserService" />.</param>
|
2023-08-13 17:33:54 +01:00
|
|
|
/// <param name="markdownPipeline">The <see cref="MarkdownPipeline" />.</param>
|
|
|
|
public BlogPostService(IDbContextFactory<BlogContext> dbContextFactory,
|
2024-02-24 14:52:43 +00:00
|
|
|
IUserService userService,
|
2023-08-13 17:33:54 +01:00
|
|
|
MarkdownPipeline markdownPipeline)
|
2023-08-12 20:13:47 +01:00
|
|
|
{
|
|
|
|
_dbContextFactory = dbContextFactory;
|
2024-02-24 14:52:43 +00:00
|
|
|
_userService = userService;
|
2023-08-13 17:33:54 +01:00
|
|
|
_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();
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:06:30 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public IReadOnlyList<IBlogPostDraft> GetDrafts(IBlogPost post)
|
|
|
|
{
|
|
|
|
if (post is null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(post));
|
|
|
|
}
|
|
|
|
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
return context.BlogPostDrafts.Where(d => d.Id == post.Id).OrderBy(d => d.Updated).ToArray();
|
|
|
|
}
|
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
/// <inheritdoc />
|
2024-02-25 14:15:21 +00:00
|
|
|
public IReadOnlyList<IBlogPost> GetAllBlogPosts(int limit = -1,
|
|
|
|
BlogPostVisibility visibility = BlogPostVisibility.Published)
|
2023-08-12 20:13:47 +01:00
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
2024-02-25 14:15:21 +00:00
|
|
|
IQueryable<BlogPost> ordered = context.BlogPosts;
|
|
|
|
if (visibility != (BlogPostVisibility)(-1))
|
|
|
|
{
|
|
|
|
ordered = ordered.Where(p => p.Visibility == visibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = ordered.OrderByDescending(post => post.Published);
|
2023-08-13 17:33:54 +01:00
|
|
|
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
|
2023-08-20 14:22:52 +01:00
|
|
|
.Where(p => p.Visibility == BlogPostVisibility.Published)
|
2023-08-12 20:13:47 +01:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2023-09-19 19:29:21 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
public IBlogPost? GetNextPost(IBlogPost blogPost)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
return context.BlogPosts
|
|
|
|
.Where(p => p.Visibility == BlogPostVisibility.Published)
|
|
|
|
.OrderBy(post => post.Published)
|
|
|
|
.FirstOrDefault(post => post.Published > blogPost.Published);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public IBlogPost? GetPreviousPost(IBlogPost blogPost)
|
|
|
|
{
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
|
|
|
return context.BlogPosts
|
|
|
|
.Where(p => p.Visibility == BlogPostVisibility.Published)
|
|
|
|
.OrderByDescending(post => post.Published)
|
|
|
|
.FirstOrDefault(post => post.Published < blogPost.Published);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-26 02:50:48 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void UpdatePost(IBlogPost post)
|
|
|
|
{
|
|
|
|
if (post is null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(post));
|
|
|
|
}
|
|
|
|
|
|
|
|
using BlogContext context = _dbContextFactory.CreateDbContext();
|
2024-02-29 18:06:30 +00:00
|
|
|
BlogPost cached = context.BlogPosts.First(p => p.Id == post.Id);
|
|
|
|
context.BlogPostDrafts.Add(BlogPostDraft.CreateFromBlogPost(cached));
|
2024-02-26 02:50:48 +00:00
|
|
|
context.Update(post);
|
|
|
|
context.SaveChanges();
|
|
|
|
}
|
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
private BlogPost CacheAuthor(BlogPost post)
|
|
|
|
{
|
|
|
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
|
|
|
|
if (post.Author is not null)
|
|
|
|
{
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
|
2024-02-24 14:52:43 +00:00
|
|
|
if (_userService.TryGetUser(post.AuthorId, out IUser? user) && user is IBlogAuthor author)
|
2023-08-12 20:13:47 +01:00
|
|
|
{
|
|
|
|
post.Author = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
|
|
|
}
|
|
|
|
}
|