2024-02-25 15:40:58 +00:00
|
|
|
using Asp.Versioning;
|
2023-08-13 17:33:54 +01:00
|
|
|
using Humanizer;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using OliverBooth.Data.Blog;
|
2024-02-24 14:52:43 +00:00
|
|
|
using OliverBooth.Data.Web;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Services;
|
|
|
|
|
2024-02-25 15:40:58 +00:00
|
|
|
namespace OliverBooth.Controllers.Api.v1;
|
2023-08-13 17:33:54 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a controller for the blog API.
|
|
|
|
/// </summary>
|
|
|
|
[ApiController]
|
2024-02-25 15:40:58 +00:00
|
|
|
[Route("api/v{version:apiVersion}/blog")]
|
2023-08-13 17:33:54 +01:00
|
|
|
[Produces("application/json")]
|
2024-02-25 15:40:58 +00:00
|
|
|
[ApiVersion(1)]
|
2023-08-13 17:33:54 +01:00
|
|
|
public sealed class BlogApiController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
2024-02-24 14:52:43 +00:00
|
|
|
private readonly IUserService _userService;
|
2023-08-13 17:33:54 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BlogApiController" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
2024-02-24 14:52:43 +00:00
|
|
|
/// <param name="userService">The <see cref="IUserService" />.</param>
|
|
|
|
public BlogApiController(IBlogPostService blogPostService, IUserService userService)
|
2023-08-13 17:33:54 +01:00
|
|
|
{
|
|
|
|
_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)
|
|
|
|
{
|
2024-02-25 14:19:07 +00:00
|
|
|
if (!_userService.TryGetUser(id, out IUser? author))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2023-08-13 17:33:54 +01:00
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
{
|
|
|
|
id = author.Id,
|
|
|
|
name = author.DisplayName,
|
|
|
|
avatarUrl = author.AvatarUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("post/{id:guid?}")]
|
|
|
|
public IActionResult GetPost(Guid id)
|
|
|
|
{
|
2024-02-25 14:19:07 +00:00
|
|
|
if (!_blogPostService.TryGetPost(id, out IBlogPost? post))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|