Get latest posts
This commit is contained in:
parent
1837f126a3
commit
b4281bea62
2 changed files with 69 additions and 3 deletions
|
@ -58,5 +58,55 @@ namespace Robware.Api.Blog.Tests {
|
||||||
var controller = new BlogController(logger, repo);
|
var controller = new BlogController(logger, repo);
|
||||||
(await controller.Get("1")).Result.Should().BeOfType<NotFoundObjectResult>();
|
(await controller.Get("1")).Result.Should().BeOfType<NotFoundObjectResult>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetLatestPosts_WithCountOfZero_ReturnsCollectionOfTenBlogPosts() {
|
||||||
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
|
var repo = Substitute.For<IBlogRepository>();
|
||||||
|
|
||||||
|
repo.GetLatestPostsAsync(10).Returns(new BlogPost[10]);
|
||||||
|
|
||||||
|
var expectation = new BlogPost[10];
|
||||||
|
|
||||||
|
var controller = new BlogController(logger, repo);
|
||||||
|
(await controller.GetLatestPosts()).Value.Should().BeEquivalentTo(expectation);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetLatestPosts_WithCountOf1_ReturnsCollectionOf1BlogPosts() {
|
||||||
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
|
var repo = Substitute.For<IBlogRepository>();
|
||||||
|
|
||||||
|
repo.GetLatestPostsAsync(1).Returns(new BlogPost[1]);
|
||||||
|
|
||||||
|
var expectation = new BlogPost[1];
|
||||||
|
|
||||||
|
var controller = new BlogController(logger, repo);
|
||||||
|
(await controller.GetLatestPosts(1)).Value.Should().BeEquivalentTo(expectation);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetLatestPosts_WithCountOf1AndOffsetOf1_ReturnsCollectionOf1BlogPosts() {
|
||||||
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
|
var repo = Substitute.For<IBlogRepository>();
|
||||||
|
|
||||||
|
repo.GetLatestPostsAsync(1, 1).Returns(new BlogPost[1]);
|
||||||
|
|
||||||
|
var expectation = new BlogPost[1];
|
||||||
|
|
||||||
|
var controller = new BlogController(logger, repo);
|
||||||
|
(await controller.GetLatestPosts(1, 1)).Value.Should().BeEquivalentTo(expectation);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetLatestPosts_WithCountOf1AndOffsetOfOutOfRange_ReturnsCollectionOf1BlogPosts() {
|
||||||
|
var logger = Substitute.For<ILogger<BlogController>>();
|
||||||
|
var repo = Substitute.For<IBlogRepository>();
|
||||||
|
|
||||||
|
repo.GetLatestPostsAsync(1, 1000).Throws(new ItemNotFoundException("", null));
|
||||||
|
|
||||||
|
var controller = new BlogController(logger, repo);
|
||||||
|
(await controller.GetLatestPosts(1, 1000)).Result.Should().BeOfType<NotFoundObjectResult>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
@ -8,6 +9,8 @@ using Robware.Data;
|
||||||
namespace Robware.Api.Blog.Controllers {
|
namespace Robware.Api.Blog.Controllers {
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class BlogController : ControllerBase {
|
public class BlogController : ControllerBase {
|
||||||
|
private const int DefaultPostLimit = 10;
|
||||||
|
|
||||||
private readonly ILogger<BlogController> _logger;
|
private readonly ILogger<BlogController> _logger;
|
||||||
private readonly IBlogRepository _blogRepository;
|
private readonly IBlogRepository _blogRepository;
|
||||||
|
|
||||||
|
@ -33,8 +36,21 @@ namespace Robware.Api.Blog.Controllers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//[HttpGet]
|
[HttpGet(nameof(GetLatestPosts))]
|
||||||
//Task<IEnumerable<BlogPost>> GetLatestPosts(int limit, int offset = 0);
|
public async Task<ActionResult<BlogPost[]>> GetLatestPosts(int count = 0, int offset = 0) {
|
||||||
|
_logger.Log(LogLevel.Information, $"{nameof(GetLatestPosts)}: {nameof(count)}={count}, {nameof(offset)}={offset}");
|
||||||
|
|
||||||
|
if (count <= 0)
|
||||||
|
count = DefaultPostLimit;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return (await _blogRepository.GetLatestPostsAsync(count, offset)).ToArray();
|
||||||
|
}
|
||||||
|
catch (ItemNotFoundException e) {
|
||||||
|
_logger.Log(LogLevel.Error, e.Message);
|
||||||
|
return NotFound("Could not get latest posts with given parameters");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//[HttpGet]
|
//[HttpGet]
|
||||||
//Task<BlogPost> GetLatestPost();
|
//Task<BlogPost> GetLatestPost();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue