Compare commits

..

No commits in common. "3e20e41565ad0b62b5581b52ff6ed613225e58c5" and "9d46d6495e6a3e7dc1eede5c48364ec12b511d00" have entirely different histories.

View File

@ -1,5 +1,6 @@
using Humanizer; using Humanizer;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
using OliverBooth.Services; using OliverBooth.Services;
@ -16,19 +17,24 @@ public sealed class BlogApiController : ControllerBase
_blogService = blogService; _blogService = blogService;
} }
[HttpGet("count")] [Route("count")]
public IActionResult Count() public IActionResult Count()
{ {
if (!ValidateReferer()) return NotFound(); return new JsonResult(new { count = _blogService.AllPosts.Count });
return Ok(new { count = _blogService.AllPosts.Count });
} }
[HttpGet("all/{skip:int?}/{take:int?}")] [Route("all/{skip:int?}/{take:int?}")]
public IActionResult GetAllBlogPosts(int skip = 0, int take = -1) public IActionResult GetAllBlogPosts(int skip = 0, int take = -1)
{ {
if (!ValidateReferer()) return NotFound();
if (take == -1) take = _blogService.AllPosts.Count; if (take == -1) take = _blogService.AllPosts.Count;
return Ok(_blogService.AllPosts.Skip(skip).Take(take).Select(post => new
var referer = Request.Headers["Referer"].ToString();
if (!referer.StartsWith(Url.PageLink("/Blog/Index")!))
{
return NotFound();
}
return new JsonResult(_blogService.AllPosts.Skip(skip).Take(take).Select(post => new
{ {
id = post.Id, id = post.Id,
commentsEnabled = post.EnableComments, commentsEnabled = post.EnableComments,
@ -38,7 +44,7 @@ public sealed class BlogApiController : ControllerBase
published = post.Published.ToUnixTimeSeconds(), published = post.Published.ToUnixTimeSeconds(),
formattedDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"), formattedDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"),
updated = post.Updated?.ToUnixTimeSeconds(), updated = post.Updated?.ToUnixTimeSeconds(),
humanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(), humanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(),
excerpt = _blogService.GetExcerpt(post, out bool trimmed), excerpt = _blogService.GetExcerpt(post, out bool trimmed),
trimmed, trimmed,
url = Url.Page("/Blog/Article", url = Url.Page("/Blog/Article",
@ -52,22 +58,15 @@ public sealed class BlogApiController : ControllerBase
})); }));
} }
[HttpGet("author/{id:int}")] [Route("author/{id:int}")]
public IActionResult GetAuthor(int id) public IActionResult GetAuthor(int id)
{ {
if (!ValidateReferer()) return NotFound();
if (!_blogService.TryGetAuthor(id, out Author? author)) return NotFound(); if (!_blogService.TryGetAuthor(id, out Author? author)) return NotFound();
return Ok(new return new JsonResult(new
{ {
name = author.Name, name = author.Name,
avatarHash = author.AvatarHash, avatarHash = author.AvatarHash,
}); });
} }
private bool ValidateReferer()
{
var referer = Request.Headers["Referer"].ToString();
return referer.StartsWith(Url.PageLink("/Blog/Index")!);
}
} }