Implement paging

This commit is contained in:
swagg boi 2021-12-11 21:02:54 -05:00
parent 1eed8adf61
commit 00922379c6
3 changed files with 18 additions and 3 deletions

View file

@ -3,3 +3,4 @@ requires 'Mojo::File';
requires 'Test::Mojo';
requires 'Mojolicious::Lite';
requires 'Mojo::Pg';
requires 'Mojolicious::Plugin::TagHelpers::Pagination';

View file

@ -11,6 +11,7 @@ use Data::Dumper; # Uncomment for debugging
# Plugins
plugin 'Config';
plugin 'TagHelpers::Pagination';
# Helpers
helper pg => sub {
@ -38,9 +39,21 @@ app->pg->migrations->from_dir('migrations')->migrate(1);
# Routes
get '/' => sub ($c) {
my $max_posts = 5;
my $posts = $c->message->get_posts();
my $last_page = sprintf('%d', scalar(@$posts) / $max_posts) + 1;
my $this_page = $c->param('page') || $last_page;
my $last_post = $this_page * $max_posts - 1;
my $first_post = $last_post - $max_posts + 1;
my @view_posts = grep defined, @$posts[$first_post..$last_post];
$c->render(posts => $posts);
$c->stash(
view_posts => \@view_posts,
this_page => $this_page,
last_page => $last_page
);
$c->render();
} => 'index';
any '/sign' => sub ($c) {

View file

@ -1,7 +1,7 @@
% layout 'default';
% title 'Home';
<h2>Messages from the World Wide Web</h2>
<% for my $row (reverse @$posts) { %>
<% for my $row (reverse @$view_posts) { %>
<table>
<tr>
<th>Date:</th>
@ -18,3 +18,4 @@
</table>
<br>
<% } %>
<%= pagination($this_page, $last_page, '?page={page}') %>