2023-08-10 22:53:15 +01:00
|
|
|
|
using Humanizer;
|
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;
|
|
|
|
|
|
|
|
|
|
namespace OliverBooth.Controllers;
|
|
|
|
|
|
|
|
|
|
[Controller]
|
|
|
|
|
[Route("/api/blog")]
|
|
|
|
|
public sealed class BlogApiController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly BlogService _blogService;
|
|
|
|
|
|
|
|
|
|
public BlogApiController(BlogService blogService)
|
|
|
|
|
{
|
|
|
|
|
_blogService = blogService;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 22:57:13 +01:00
|
|
|
|
[HttpGet("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,
|
|
|
|
|
url = Url.Page("/Blog/Article",
|
|
|
|
|
new
|
|
|
|
|
{
|
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-10 22:57:13 +01:00
|
|
|
|
[HttpGet("author/{id:int}")]
|
2023-08-10 04:56:12 +01:00
|
|
|
|
public IActionResult GetAuthor(int id)
|
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
return referer.StartsWith(Url.PageLink("/Blog/Index")!);
|
|
|
|
|
}
|
2023-08-10 04:56:12 +01:00
|
|
|
|
}
|