2023-08-13 17:33:54 +01:00
|
|
|
using Humanizer;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
using OliverBooth.Services;
|
|
|
|
|
|
|
|
namespace OliverBooth.Controllers.Blog;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a controller for the blog API.
|
|
|
|
/// </summary>
|
|
|
|
[ApiController]
|
|
|
|
[Route("api/blog")]
|
|
|
|
[Produces("application/json")]
|
|
|
|
public sealed class BlogApiController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
|
|
|
private readonly IBlogUserService _userService;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BlogApiController" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
2023-08-16 14:44:57 +01:00
|
|
|
/// <param name="userService">The <see cref="IBlogUserService" />.</param>
|
2023-08-13 17:33:54 +01:00
|
|
|
public BlogApiController(IBlogPostService blogPostService, IBlogUserService userService)
|
|
|
|
{
|
|
|
|
_blogPostService = blogPostService;
|
|
|
|
_userService = userService;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Route("count")]
|
|
|
|
public IActionResult Count()
|
|
|
|
{
|
|
|
|
return Ok(new { count = _blogPostService.GetBlogPostCount() });
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("posts/{page:int?}")]
|
|
|
|
public IActionResult GetAllBlogPosts(int page = 0)
|
|
|
|
{
|
|
|
|
const int itemsPerPage = 10;
|
|
|
|
IReadOnlyList<IBlogPost> allPosts = _blogPostService.GetBlogPosts(page, itemsPerPage);
|
|
|
|
return Ok(allPosts.Select(post => CreatePostObject(post)));
|
|
|
|
}
|
|
|
|
|
2023-09-24 17:03:06 +01:00
|
|
|
[HttpGet("posts/tagged/{tag}/{page:int?}")]
|
|
|
|
public IActionResult GetTaggedBlogPosts(string tag, int page = 0)
|
|
|
|
{
|
|
|
|
const int itemsPerPage = 10;
|
|
|
|
tag = tag.Replace('-', ' ').ToLowerInvariant();
|
|
|
|
|
|
|
|
IReadOnlyList<IBlogPost> allPosts = _blogPostService.GetBlogPosts(page, itemsPerPage);
|
|
|
|
allPosts = allPosts.Where(post => post.Tags.Contains(tag)).ToList();
|
|
|
|
return Ok(allPosts.Select(post => CreatePostObject(post)));
|
|
|
|
}
|
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
[HttpGet("author/{id:guid}")]
|
|
|
|
public IActionResult GetAuthor(Guid id)
|
|
|
|
{
|
|
|
|
if (!_userService.TryGetUser(id, out IUser? author)) return NotFound();
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
{
|
|
|
|
id = author.Id,
|
|
|
|
name = author.DisplayName,
|
|
|
|
avatarUrl = author.AvatarUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("post/{id:guid?}")]
|
|
|
|
public IActionResult GetPost(Guid id)
|
|
|
|
{
|
|
|
|
if (!_blogPostService.TryGetPost(id, out IBlogPost? post)) return NotFound();
|
|
|
|
return Ok(CreatePostObject(post, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
private object CreatePostObject(IBlogPost post, bool includeContent = false)
|
|
|
|
{
|
|
|
|
return new
|
|
|
|
{
|
|
|
|
id = post.Id,
|
|
|
|
commentsEnabled = post.EnableComments,
|
|
|
|
identifier = post.GetDisqusIdentifier(),
|
|
|
|
author = post.Author.Id,
|
|
|
|
title = post.Title,
|
|
|
|
published = post.Published.ToUnixTimeSeconds(),
|
|
|
|
updated = post.Updated?.ToUnixTimeSeconds(),
|
2023-10-12 16:58:10 +01:00
|
|
|
formattedPublishDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"),
|
|
|
|
formattedUpdateDate = post.Updated?.ToString("dddd, d MMMM yyyy HH:mm"),
|
2023-08-13 17:33:54 +01:00
|
|
|
humanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(),
|
|
|
|
excerpt = _blogPostService.RenderExcerpt(post, out bool trimmed),
|
|
|
|
content = includeContent ? _blogPostService.RenderPost(post) : null,
|
|
|
|
trimmed,
|
2023-09-23 22:08:25 +01:00
|
|
|
tags = post.Tags.Select(t => t.Replace(' ', '-')),
|
2023-08-13 17:33:54 +01:00
|
|
|
url = new
|
|
|
|
{
|
|
|
|
year = post.Published.ToString("yyyy"),
|
|
|
|
month = post.Published.ToString("MM"),
|
|
|
|
day = post.Published.ToString("dd"),
|
|
|
|
slug = post.Slug
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|