summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/ForumController.cs
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-04-20 09:20:05 -0400
committerMichael <[email protected]>2017-04-20 09:20:05 -0400
commitf5c33c367a4c7b834d7c684c008180a35af071bc (patch)
tree2b00d67d569b554d24c91751febf33e3deffb441 /Project-Unite/Controllers/ForumController.cs
parentd3935e31b57bc7669a615d52f5e441dbed7acec4 (diff)
downloadproject-unite-f5c33c367a4c7b834d7c684c008180a35af071bc.tar.gz
project-unite-f5c33c367a4c7b834d7c684c008180a35af071bc.tar.bz2
project-unite-f5c33c367a4c7b834d7c684c008180a35af071bc.zip
paginate forum posts
Diffstat (limited to 'Project-Unite/Controllers/ForumController.cs')
-rw-r--r--Project-Unite/Controllers/ForumController.cs29
1 files changed, 28 insertions, 1 deletions
diff --git a/Project-Unite/Controllers/ForumController.cs b/Project-Unite/Controllers/ForumController.cs
index b65ed3e..5b36f20 100644
--- a/Project-Unite/Controllers/ForumController.cs
+++ b/Project-Unite/Controllers/ForumController.cs
@@ -318,9 +318,13 @@ namespace Project_Unite.Controllers
return RedirectToAction("ViewUnread");
}
+
+
[Authorize]
- public ActionResult ViewTopic(string id, bool triedtolikeowntopic = false)
+ public ActionResult ViewTopic(string id, bool triedtolikeowntopic = false, int page = 1)
{
+ int realpage = page - 1;
+ int pageSize = 10;
if (triedtolikeowntopic)
ViewBag.Error = "You cannot like or dislike your own topic!";
var db = new ApplicationDbContext();
@@ -329,8 +333,31 @@ namespace Project_Unite.Controllers
return new HttpStatusCodeResult(404);
if (!ACL.CanSee(User.Identity.Name, topic.Parent))
return new HttpStatusCodeResult(403);
+ int pages = topic.Posts.GetPageCount(pageSize);
+ ViewBag.Page = realpage;
+ ViewBag.PageCount = pages;
+ ViewBag.PageSize = pageSize;
return View(topic);
}
}
+
+ public static class PaginationExtensions
+ {
+ public static int GetPageCount<T>(this IEnumerable<T> collection, int pageSize)
+ {
+ return (collection.Count() + pageSize - 1) / pageSize;
+ }
+
+ public static IEnumerable<T> GetItemsOnPage<T>(this IEnumerable<T> collection, int page, int pageSize)
+ {
+ var lst = collection.ToList();
+
+ for(int i = pageSize * page; i < pageSize + (pageSize * page) && i < lst.Count(); i++)
+ {
+ yield return lst[i];
+ }
+
+ }
+ }
} \ No newline at end of file