Skip to content

Commit e401638

Browse files
committed
feat: smart pagination
1 parent 5b21df4 commit e401638

File tree

8 files changed

+91
-53
lines changed

8 files changed

+91
-53
lines changed

Blog/Blog/Controllers/HomeController.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ public IActionResult Details(int? id)
7373
return View(post);
7474
}
7575

76-
public IActionResult Privacy()
77-
{
78-
return View();
79-
}
80-
8176
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8277
public IActionResult Error()
8378
{

Blog/Blog/Controllers/PostsController.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Blog.Models;
1010
using Blog.Models.Repositories;
1111
using Microsoft.AspNetCore.Http;
12+
using Blog.Models.ViewModels;
1213

1314
namespace Blog.Controllers
1415
{
@@ -18,6 +19,7 @@ public class PostsController : Controller
1819
private readonly ITagRepository tagsRepository;
1920
private readonly IFileManager fileManager;
2021

22+
private readonly int ItemsPerPage = 1;
2123
private readonly string PostForm = "PostForm";
2224

2325
public PostsController(IPostRepository postsRepository, ITagRepository tagsRepository, IFileManager fileManager)
@@ -27,15 +29,37 @@ public PostsController(IPostRepository postsRepository, ITagRepository tagsRepos
2729
this.fileManager = fileManager;
2830
}
2931

30-
public IActionResult Index(string tag = "", string title = "")
32+
public IActionResult Index(string tag = "", string title = "", int page = 1)
33+
{
34+
var posts = GetPosts(tag, title);
35+
int totalPosts = posts.Count();
36+
37+
if (totalPosts > ItemsPerPage)
38+
posts = posts.Skip((page - 1) * ItemsPerPage).Take(ItemsPerPage);
39+
40+
PostsViewModel homeViewModel = new PostsViewModel()
41+
{
42+
Posts = posts,
43+
PagingInformation = new PagingInformation()
44+
{
45+
CurrentPage = page,
46+
ItemsPerPage = ItemsPerPage,
47+
TotalItems = totalPosts
48+
}
49+
};
50+
51+
return View(homeViewModel);
52+
}
53+
54+
private IEnumerable<Post> GetPosts(string tag = "", string title = "")
3155
{
3256
if (!string.IsNullOrEmpty(tag))
33-
return View(postsRepository.GetByTag(tag));
57+
return postsRepository.GetByTag(tag);
3458

3559
if (!string.IsNullOrEmpty(title))
36-
return View(postsRepository.GetByTitle(title));
60+
return postsRepository.GetByTitle(title);
3761

38-
return View(postsRepository.Get());
62+
return postsRepository.Get();
3963
}
4064

4165
public IActionResult Details(int? id)

Blog/Blog/Models/ViewModels/PostViewModel.cs renamed to Blog/Blog/Models/ViewModels/PostsViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace Blog.Models.ViewModels
88
{
9-
public class PostViewModel
9+
public class PostsViewModel
1010
{
11-
public Post Post { get; set; }
12-
public IFormFile FormFile { get; set; }
11+
public IEnumerable<Post> Posts { get; set; }
12+
public PagingInformation PagingInformation { get; set; }
1313
}
1414
}

Blog/Blog/TagsHelper/PageLinkTagHelper.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,55 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
3535
TagBuilder tag = new TagBuilder("ul");
3636
tag.AddCssClass("pagination");
3737

38-
TagBuilder currentItem = CreateTag(PagingInformation.CurrentPage, urlHelper);
39-
4038
if (PagingInformation.HasPreviousPage)
4139
{
42-
TagBuilder previousItem = CreateTag(PagingInformation.CurrentPage - 1, urlHelper, "Previous");
43-
tag.InnerHtml.AppendHtml(previousItem);
40+
TagBuilder previousPage = CreateTag(PagingInformation.CurrentPage - 1, urlHelper, "Previous");
41+
TagBuilder firstPage = CreateTag(1, urlHelper, "First Page");
42+
tag.InnerHtml.AppendHtml(firstPage);
43+
tag.InnerHtml.AppendHtml(previousPage);
44+
}
45+
46+
if (PagingInformation.TotalPages <= 6)
47+
{
48+
CreatePagination(tag, urlHelper, 1, PagingInformation.TotalPages);
4449
}
50+
else if (PagingInformation.TotalPages > 6)
51+
{
52+
if (PagingInformation.CurrentPage <= 3)
53+
CreatePagination(tag, urlHelper, 1, 3);
54+
else if (PagingInformation.CurrentPage > 3)
55+
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage - 3, PagingInformation.CurrentPage);
56+
57+
int leftPages = PagingInformation.TotalPages - PagingInformation.CurrentPage;
4558

46-
tag.InnerHtml.AppendHtml(currentItem);
59+
if (leftPages > 0 && leftPages <= 2)
60+
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage + 1, PagingInformation.CurrentPage + leftPages);
61+
else if (PagingInformation.CurrentPage <= 3)
62+
CreatePagination(tag, urlHelper, PagingInformation.TotalPages - 2, PagingInformation.TotalPages);
63+
else if (leftPages > 0)
64+
CreatePagination(tag, urlHelper, PagingInformation.CurrentPage + 1, PagingInformation.CurrentPage + 3);
65+
}
4766

4867
if (PagingInformation.HasNextPage)
4968
{
50-
TagBuilder nextItem = CreateTag(PagingInformation.CurrentPage + 1, urlHelper, "Next");
51-
tag.InnerHtml.AppendHtml(nextItem);
69+
TagBuilder nextPage = CreateTag(PagingInformation.CurrentPage + 1, urlHelper, "Next");
70+
TagBuilder lastPage = CreateTag(PagingInformation.TotalPages, urlHelper, "Last Page");
71+
tag.InnerHtml.AppendHtml(nextPage);
72+
tag.InnerHtml.AppendHtml(lastPage);
5273
}
5374

5475
output.Content.AppendHtml(tag);
5576
}
5677

78+
private void CreatePagination(TagBuilder tag, IUrlHelper urlHelper, int start, int end)
79+
{
80+
for (int i = start; i <= end; i++)
81+
{
82+
TagBuilder current = CreateTag(i, urlHelper);
83+
tag.InnerHtml.AppendHtml(current);
84+
}
85+
}
86+
5787
private TagBuilder CreateTag(int pageNumber, IUrlHelper urlHelper, string title = null)
5888
{
5989
TagBuilder item = new TagBuilder("li");
@@ -67,7 +97,7 @@ private TagBuilder CreateTag(int pageNumber, IUrlHelper urlHelper, string title
6797
item.AddCssClass("page-item");
6898
link.AddCssClass("page-link");
6999

70-
link.InnerHtml.Append(title?? pageNumber.ToString());
100+
link.InnerHtml.Append(title ?? pageNumber.ToString());
71101
item.InnerHtml.AppendHtml(link);
72102
return item;
73103
}

Blog/Blog/Views/Home/Index.cshtml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,18 @@
5555
</div>
5656
}
5757
}
58+
else
59+
{
60+
<div class="col-md-12 mt-2 text-center text-danger">
61+
<h4>No News Found</h4>
62+
</div>
63+
}
64+
65+
66+
<!-- PAGINATION -->
67+
<div paging-information="@Model.PagingInformation" page-action="Index" class="mx-auto"></div>
5868
</div>
5969
</div>
6070
</div>
61-
62-
<!-- PAGINATION -->
63-
<div paging-information="@Model.PagingInformation" page-action="Index"></div>
6471
</section>
6572

Blog/Blog/Views/Home/Privacy.cshtml

Lines changed: 0 additions & 6 deletions
This file was deleted.

Blog/Blog/Views/Posts/Index.cshtml

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model IEnumerable<Blog.Models.Post>
1+
@model Blog.Models.ViewModels.PostsViewModel
22

33
@{
44
ViewData["Title"] = "Index";
@@ -40,30 +40,18 @@
4040
<table class="table table-striped">
4141
<thead class="thead-dark">
4242
<tr>
43-
<th>
44-
@Html.DisplayNameFor(model => model.Title)
45-
</th>
46-
<th>
47-
@Html.DisplayNameFor(model => model.Views)
48-
</th>
49-
<th class="d-none d-md-table-cell">
50-
@Html.DisplayNameFor(model => model.Public)
51-
</th>
52-
<th class="d-none d-md-table-cell">
53-
@Html.DisplayNameFor(model => model.Created)
54-
</th>
55-
<th class="d-none d-md-table-cell">
56-
@Html.DisplayNameFor(model => model.Updated)
57-
</th>
58-
<th>
59-
Actions
60-
</th>
43+
<th>Title</th>
44+
<th>Views</th>
45+
<th class="d-none d-md-table-cell">Public</th>
46+
<th class="d-none d-md-table-cell">Created</th>
47+
<th class="d-none d-md-table-cell">Updated</th>
48+
<th>Actions</th>
6149
</tr>
6250
</thead>
6351
<tbody>
64-
@if (Model.Any())
52+
@if (Model.Posts.Any())
6553
{
66-
@foreach (var item in Model)
54+
@foreach (var item in Model.Posts)
6755
{
6856
<tr>
6957
<td>
@@ -94,4 +82,7 @@
9482
}
9583
</tbody>
9684
</table>
85+
86+
<!-- PAGINATION -->
87+
<div paging-information="@Model.PagingInformation" page-action="Index" class="mx-auto"></div>
9788
</section>

Blog/Blog/Views/Shared/_Layout.cshtml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
<li class="nav-item">
3030
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
3131
</li>
32-
<li class="nav-item">
33-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
34-
</li>
3532
<li class="nav-item">
3633
<a class="nav-link text-dark" asp-area="" asp-controller="Posts" asp-action="Index">Posts</a>
3734
</li>

0 commit comments

Comments
 (0)