Get latest blog post

This commit is contained in:
Robert Marshall 2020-04-10 10:35:08 +01:00
parent c1eff209eb
commit 67c71d4d06
2 changed files with 33 additions and 4 deletions

View file

@ -108,5 +108,29 @@ namespace Robware.Api.Blog.Tests {
var controller = new BlogController(logger, repo);
(await controller.GetLatestPosts(1, 1000)).Result.Should().BeOfType<NotFoundObjectResult>();
}
[Fact]
public async Task GetLatestPost_ReturnsBlogPosts() {
var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>();
repo.GetLatestPostAsync().Returns(new BlogPost());
var expectation = new BlogPost();
var controller = new BlogController(logger, repo);
(await controller.GetLatestPost()).Value.Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task GetLatestPost_WhenNoPostExists_Returns404() {
var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>();
repo.GetLatestPostAsync().Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo);
(await controller.GetLatestPost()).Result.Should().BeOfType<NotFoundObjectResult>();
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@ -53,8 +52,14 @@ namespace Robware.Api.Blog.Controllers {
}
[HttpGet(nameof(GetLatestPost))]
public async Task<BlogPost> GetLatestPost() {
return null;
public async Task<ActionResult<BlogPost>> GetLatestPost() {
try {
return await _blogRepository.GetLatestPostAsync();
}
catch (ItemNotFoundException e) {
_logger.Log(LogLevel.Error, e.Message);
return NotFound("Could not find blog post");
}
}
//[HttpGet]