From ba91a9994fd91f8da87ddc7f360e136baff804ae Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 11 Dec 2021 21:09:27 -0500 Subject: [PATCH] Add better pagination to photos page --- ProjectLighthouse/Pages/PhotosPage.cshtml | 6 +++++- ProjectLighthouse/Pages/PhotosPage.cshtml.cs | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ProjectLighthouse/Pages/PhotosPage.cshtml b/ProjectLighthouse/Pages/PhotosPage.cshtml index e579cbc3..86e16661 100644 --- a/ProjectLighthouse/Pages/PhotosPage.cshtml +++ b/ProjectLighthouse/Pages/PhotosPage.cshtml @@ -20,4 +20,8 @@ { Previous Page } -Next Page \ No newline at end of file +@(Model.PageNumber + 1) / @(Model.PageAmount) +@if (Model.PageNumber < Model.PageAmount - 1) +{ + Next Page +} \ No newline at end of file diff --git a/ProjectLighthouse/Pages/PhotosPage.cshtml.cs b/ProjectLighthouse/Pages/PhotosPage.cshtml.cs index d243f2f6..f5e132b0 100644 --- a/ProjectLighthouse/Pages/PhotosPage.cshtml.cs +++ b/ProjectLighthouse/Pages/PhotosPage.cshtml.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -18,6 +19,8 @@ namespace LBPUnion.ProjectLighthouse.Pages public int PhotoCount; + public int PageAmount; + public List Photos; public PhotosPage([NotNull] Database database) : base(database) {} @@ -27,6 +30,12 @@ namespace LBPUnion.ProjectLighthouse.Pages this.PhotoCount = await StatisticsHelper.PhotoCount(); this.PageNumber = pageNumber; + this.PageAmount = (int)Math.Ceiling((double)this.PhotoCount / ServerStatics.PageSize); + + if (this.PageNumber < 0 || this.PageNumber >= this.PageAmount) + { + return this.Redirect($"/photos/{Math.Clamp(this.PageNumber, 0, this.PageAmount - 1)}"); + } this.Photos = await this.Database.Photos.Include (p => p.Creator)