Add blog index with pagination

This commit is contained in:
Robert Marshall 2019-04-28 11:06:15 +01:00
parent 68fa2cf1c7
commit b8f371e3e8
5 changed files with 78 additions and 6 deletions

View file

@ -11,6 +11,7 @@ namespace Website.Controllers
{
public class BlogController : Controller
{
const int MaxPostsPerPage = 10;
private readonly BlogRepository _repo;
public BlogController(BlogRepository repo)
@ -18,9 +19,13 @@ namespace Website.Controllers
_repo = repo;
}
public IActionResult Index()
public async Task<IActionResult> Page(int page)
{
return View();
var offset = (page - 1) * MaxPostsPerPage;
var posts = (await _repo.GetLatestPostsAsync(MaxPostsPerPage, offset)).Select(p => new BlogPostViewModel(p));
var maxPages = (await _repo.GetCountAsync()) / MaxPostsPerPage;
var model = new BlogViewModel(posts, page, maxPages);
return View(model);
}
public async Task<IActionResult> View(int id)