Skip to content

Commit

Permalink
Refactored PR 125, reducing the number of calls to GetPosts() by filt…
Browse files Browse the repository at this point in the history
…ering a full resultset. The accurate total post count is now available to calculate paging. (#129)
  • Loading branch information
xnodeoncode authored and madskristensen committed Jan 2, 2020
1 parent f492956 commit 8f031bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public BlogController(IBlogService blog, IOptionsSnapshot<BlogSettings> settings
[OutputCache(Profile = "default")]
public async Task<IActionResult> Index([FromRoute]int page = 0)
{

// get all posts so that we can obtain a count that will drive pagination.
// there are cleaner methods to do this than making two calls to the blog service
// so this may be refactored later.
// get all published posts.
var allPosts = await _blog.GetPosts();

var posts = await _blog.GetPosts(_settings.Value.PostsPerPage, _settings.Value.PostsPerPage * page);
// apply paging filter.
var posts = allPosts.Skip(_settings.Value.PostsPerPage * page).Take(_settings.Value.PostsPerPage);

ViewData["TotalPostCount"] = allPosts.Count();
ViewData["Title"] = _manifest.Name;
Expand All @@ -50,13 +48,11 @@ public async Task<IActionResult> Index([FromRoute]int page = 0)
[OutputCache(Profile = "default")]
public async Task<IActionResult> Category(string category, int page = 0)
{
// get all posts for the selected category.
var allPosts = await _blog.GetPostsByCategory(category);

// get all posts so that we can obtain a count that will drive pagination.
// there are cleaner methods to do this than making two calls to the blog service
// so this may be refactored later.
var allPosts = await _blog.GetPosts();

var posts = (await _blog.GetPostsByCategory(category)).Skip(_settings.Value.PostsPerPage * page).Take(_settings.Value.PostsPerPage);
// apply paging filter.
var posts = allPosts.Skip(_settings.Value.PostsPerPage * page).Take(_settings.Value.PostsPerPage);

ViewData["TotalPostCount"] = allPosts.Count();
ViewData["Title"] = _manifest.Name + " " + category;
Expand Down
4 changes: 1 addition & 3 deletions src/Views/Blog/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
@inject IOptionsSnapshot<BlogSettings> settings
@{
int currentPage = int.Parse(ViewContext.RouteData.Values["page"] as string ?? "0");
//int pageCount = Model.ToList().Count;
// fixed pagination issue. Model.Count() would always be equal to the number of posts per page not the actual page count.

int pageCount = int.Parse(ViewData["TotalPostCount"].ToString()) / 2;
}

Expand Down

0 comments on commit 8f031bf

Please sign in to comment.