Skip to content

Commit

Permalink
Add paging to the Split topics feature (#2026)
Browse files Browse the repository at this point in the history
* add paging to split topics, this avoids the form limit and also avoids loading thousands of posts resulting in a very slow page

* clean up unused class from a previous wip
  • Loading branch information
Masterjun3 authored Nov 5, 2024
1 parent b2e1671 commit 4416778
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
8 changes: 8 additions & 0 deletions TASVideos/Pages/Forum/Topics/Split.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
}

<partial Name="_ForumHeader" />
<div condition="Model.TotalPages > 1" class="btn-group flex-wrap" role="group">
@for (int iteratorPage = 1; iteratorPage <= Model.TotalPages; iteratorPage++)
{
<a asp-page="@ViewContext.Page()"
asp-route-CurrentPage="@iteratorPage"
type="button" class="btn btn-secondary border-dark flex-grow-0 @(iteratorPage == Model.CurrentPage ? "active" : "")">@iteratorPage</a>
}
</div>
<form client-side-validation="true" method="post">
<input type="hidden" asp-for="Topic.ForumName" />
<input type="hidden" asp-for="Topic.ForumId" />
Expand Down
59 changes: 43 additions & 16 deletions TASVideos/Pages/Forum/Topics/Split.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public class SplitModel(
[FromRoute]
public int Id { get; set; }

[FromQuery]
public int CurrentPage { get; set; }
public int TotalPages { get; set; }

[BindProperty]
public TopicSplit Topic { get; set; } = new();

Expand Down Expand Up @@ -120,7 +124,7 @@ await publisher.SendForum(

private async Task<TopicSplit?> PopulatePosts()
{
return await db.ForumTopics
TopicSplit? topicSplit = await db.ForumTopics
.ExcludeRestricted(CanSeeRestricted)
.Where(t => t.Id == Id)
.Select(t => new TopicSplit
Expand All @@ -130,22 +134,44 @@ await publisher.SendForum(
CreateNewTopicIn = t.Forum!.Id,
ForumId = t.Forum.Id,
ForumName = t.Forum.Name,
Posts = t.ForumPosts
.Select(p => new TopicSplit.Post
{
Id = p.Id,
PostCreateTimestamp = p.CreateTimestamp,
EnableBbCode = p.EnableBbCode,
EnableHtml = p.EnableHtml,
Subject = p.Subject,
Text = p.Text,
PosterName = p.Poster!.UserName,
PosterAvatar = p.Poster.Avatar
})
.OrderBy(p => p.PostCreateTimestamp)
.ToList()
PostsCount = t.ForumPosts.Count,
})
.SingleOrDefaultAsync();

if (topicSplit is not null)
{
const int PageSize = 500;
TotalPages = ((topicSplit!.PostsCount - 1) / PageSize) + 1;

if (CurrentPage <= 0 || CurrentPage > TotalPages)
{
CurrentPage = TotalPages;
}

int leftover = topicSplit!.PostsCount % PageSize;
int take = CurrentPage == 1 ? leftover : PageSize;
int skip = CurrentPage == 1 ? 0 : leftover + (PageSize * (CurrentPage - 2));

topicSplit.Posts = await db.ForumPosts
.Where(fp => fp.TopicId == Id)
.Select(p => new TopicSplit.Post
{
Id = p.Id,
PostCreateTimestamp = p.CreateTimestamp,
EnableBbCode = p.EnableBbCode,
EnableHtml = p.EnableHtml,
Subject = p.Subject,
Text = p.Text,
PosterName = p.Poster!.UserName,
PosterAvatar = p.Poster.Avatar
})
.OrderBy(p => p.PostCreateTimestamp)
.Skip(skip)
.Take(take)
.ToListAsync();
}

return topicSplit;
}

private async Task PopulateAvailableForums()
Expand All @@ -161,7 +187,8 @@ public class TopicSplit
public string Title { get; init; } = "";
public int ForumId { get; init; }
public string ForumName { get; init; } = "";
public List<Post> Posts { get; init; } = [];
public int PostsCount { get; set; }
public List<Post> Posts { get; set; } = [];

public class Post
{
Expand Down

0 comments on commit 4416778

Please sign in to comment.