Compare commits

..

3 Commits

1 changed files with 16 additions and 15 deletions

View File

@ -1,6 +1,5 @@
using Humanizer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
@ -17,24 +16,19 @@ public sealed class BlogApiController : ControllerBase
_blogService = blogService;
}
[Route("count")]
[HttpGet("count")]
public IActionResult Count()
{
return new JsonResult(new { count = _blogService.AllPosts.Count });
if (!ValidateReferer()) return NotFound();
return Ok(new { count = _blogService.AllPosts.Count });
}
[Route("all/{skip:int?}/{take:int?}")]
[HttpGet("all/{skip:int?}/{take:int?}")]
public IActionResult GetAllBlogPosts(int skip = 0, int take = -1)
{
if (!ValidateReferer()) return NotFound();
if (take == -1) take = _blogService.AllPosts.Count;
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
return Ok(_blogService.AllPosts.Skip(skip).Take(take).Select(post => new
{
id = post.Id,
commentsEnabled = post.EnableComments,
@ -58,15 +52,22 @@ public sealed class BlogApiController : ControllerBase
}));
}
[Route("author/{id:int}")]
[HttpGet("author/{id:int}")]
public IActionResult GetAuthor(int id)
{
if (!ValidateReferer()) return NotFound();
if (!_blogService.TryGetAuthor(id, out Author? author)) return NotFound();
return new JsonResult(new
return Ok(new
{
name = author.Name,
avatarHash = author.AvatarHash,
});
}
private bool ValidateReferer()
{
var referer = Request.Headers["Referer"].ToString();
return referer.StartsWith(Url.PageLink("/Blog/Index")!);
}
}