Return blog post count
This commit is contained in:
parent
67c71d4d06
commit
71bddddc13
2 changed files with 34 additions and 3 deletions
|
@ -132,5 +132,27 @@ namespace Robware.Api.Blog.Tests {
|
|||
var controller = new BlogController(logger, repo);
|
||||
(await controller.GetLatestPost()).Result.Should().BeOfType<NotFoundObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCount_ReturnsBlogPostCount() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
|
||||
repo.GetCountAsync().Returns(1);
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.GetCount()).Value.Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCount_WhenNoPostsExist_ReturnsZero() {
|
||||
var logger = Substitute.For<ILogger<BlogController>>();
|
||||
var repo = Substitute.For<IBlogRepository>();
|
||||
|
||||
repo.GetCountAsync().Throws(new ItemNotFoundException("", null));
|
||||
|
||||
var controller = new BlogController(logger, repo);
|
||||
(await controller.GetCount()).Value.Should().Be(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Linq;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -62,8 +63,16 @@ namespace Robware.Api.Blog.Controllers {
|
|||
}
|
||||
}
|
||||
|
||||
//[HttpGet]
|
||||
//Task<int> GetCount();
|
||||
[HttpGet(nameof(GetCount))]
|
||||
public async Task<ActionResult<int>> GetCount() {
|
||||
try {
|
||||
return await _blogRepository.GetCountAsync();
|
||||
}
|
||||
catch (ItemNotFoundException e) {
|
||||
_logger.Log(LogLevel.Error, e.Message);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//[HttpPost]
|
||||
//Task<BlogPost> SavePost(BlogPost post);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue