Add blog index with pagination
This commit is contained in:
parent
68fa2cf1c7
commit
b8f371e3e8
5 changed files with 78 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue