2023-08-11 15:51:20 +01:00
|
|
|
using Humanizer;
|
2023-08-11 17:16:26 +01:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2023-08-10 14:34:52 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-08-10 04:56:12 +01:00
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
using OliverBooth.Services;
|
|
|
|
|
2023-08-11 16:32:11 +01:00
|
|
|
namespace OliverBooth.Controllers;
|
2023-08-10 04:56:12 +01:00
|
|
|
|
2023-08-11 15:41:02 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Represents a controller for the blog API.
|
|
|
|
/// </summary>
|
2023-08-11 16:32:11 +01:00
|
|
|
[ApiController]
|
|
|
|
[Route("api/blog")]
|
|
|
|
[Produces("application/json")]
|
2023-08-11 17:16:26 +01:00
|
|
|
[EnableCors("BlogApi")]
|
2023-08-10 04:56:12 +01:00
|
|
|
public sealed class BlogApiController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly BlogService _blogService;
|
|
|
|
|
2023-08-11 15:41:02 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BlogApiController" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="blogService">The <see cref="BlogService" />.</param>
|
2023-08-10 04:56:12 +01:00
|
|
|
public BlogApiController(BlogService blogService)
|
|
|
|
{
|
|
|
|
_blogService = blogService;
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:32:11 +01:00
|
|
|
[Route("count")]
|
2023-08-10 04:56:12 +01:00
|
|
|
public IActionResult Count()
|
|
|
|
{
|
2023-08-10 22:56:49 +01:00
|
|
|
if (!ValidateReferer()) return NotFound();
|
2023-08-10 22:55:52 +01:00
|
|
|
return Ok(new { count = _blogService.AllPosts.Count });
|
2023-08-10 04:56:12 +01:00
|
|
|
}
|
|
|
|
|
2023-08-10 22:57:13 +01:00
|
|
|
[HttpGet("all/{skip:int?}/{take:int?}")]
|
2023-08-10 04:56:12 +01:00
|
|
|
public IActionResult GetAllBlogPosts(int skip = 0, int take = -1)
|
|
|
|
{
|
2023-08-10 22:56:49 +01:00
|
|
|
if (!ValidateReferer()) return NotFound();
|
2023-08-10 04:56:12 +01:00
|
|
|
if (take == -1) take = _blogService.AllPosts.Count;
|
2023-08-10 22:55:52 +01:00
|
|
|
return Ok(_blogService.AllPosts.Skip(skip).Take(take).Select(post => new
|
2023-08-10 04:56:12 +01:00
|
|
|
{
|
|
|
|
id = post.Id,
|
|
|
|
commentsEnabled = post.EnableComments,
|
|
|
|
identifier = post.GetDisqusIdentifier(),
|
|
|
|
author = post.AuthorId,
|
|
|
|
title = post.Title,
|
|
|
|
published = post.Published.ToUnixTimeSeconds(),
|
2023-08-10 15:32:34 +01:00
|
|
|
formattedDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"),
|
2023-08-10 04:56:12 +01:00
|
|
|
updated = post.Updated?.ToUnixTimeSeconds(),
|
2023-08-10 22:55:52 +01:00
|
|
|
humanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(),
|
2023-08-10 04:56:12 +01:00
|
|
|
excerpt = _blogService.GetExcerpt(post, out bool trimmed),
|
|
|
|
trimmed,
|
2023-08-11 15:42:48 +01:00
|
|
|
url = Url.Page("/Article",
|
2023-08-10 04:56:12 +01:00
|
|
|
new
|
|
|
|
{
|
2023-08-11 15:42:48 +01:00
|
|
|
area = "blog",
|
2023-08-10 22:53:15 +01:00
|
|
|
year = post.Published.ToString("yyyy"),
|
|
|
|
month = post.Published.ToString("MM"),
|
|
|
|
day = post.Published.ToString("dd"),
|
2023-08-10 04:56:12 +01:00
|
|
|
slug = post.Slug
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:09:13 +01:00
|
|
|
[HttpGet("author/{id:guid}")]
|
|
|
|
public IActionResult GetAuthor(Guid id)
|
2023-08-10 04:56:12 +01:00
|
|
|
{
|
2023-08-10 22:56:49 +01:00
|
|
|
if (!ValidateReferer()) return NotFound();
|
2023-08-10 04:56:12 +01:00
|
|
|
if (!_blogService.TryGetAuthor(id, out Author? author)) return NotFound();
|
|
|
|
|
2023-08-10 22:55:52 +01:00
|
|
|
return Ok(new
|
2023-08-10 04:56:12 +01:00
|
|
|
{
|
2023-08-11 16:32:11 +01:00
|
|
|
id = author.Id,
|
2023-08-10 04:56:12 +01:00
|
|
|
name = author.Name,
|
|
|
|
avatarHash = author.AvatarHash,
|
|
|
|
});
|
|
|
|
}
|
2023-08-10 22:57:13 +01:00
|
|
|
|
2023-08-10 22:56:49 +01:00
|
|
|
private bool ValidateReferer()
|
|
|
|
{
|
|
|
|
var referer = Request.Headers["Referer"].ToString();
|
2023-08-11 16:32:11 +01:00
|
|
|
return referer.StartsWith(Url.PageLink("/index",values: new{area="blog"})!);
|
2023-08-10 22:56:49 +01:00
|
|
|
}
|
2023-08-10 04:56:12 +01:00
|
|
|
}
|