Get latest posts

This commit is contained in:
Robert Marshall 2020-04-10 10:27:55 +01:00
parent 1837f126a3
commit b4281bea62
2 changed files with 69 additions and 3 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@ -8,6 +9,8 @@ using Robware.Data;
namespace Robware.Api.Blog.Controllers {
[ApiController]
public class BlogController : ControllerBase {
private const int DefaultPostLimit = 10;
private readonly ILogger<BlogController> _logger;
private readonly IBlogRepository _blogRepository;
@ -33,8 +36,21 @@ namespace Robware.Api.Blog.Controllers {
}
}
//[HttpGet]
//Task<IEnumerable<BlogPost>> GetLatestPosts(int limit, int offset = 0);
[HttpGet(nameof(GetLatestPosts))]
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]
//Task<BlogPost> GetLatestPost();