Implement totally broken remark paging

This commit is contained in:
swaggboi 2022-08-22 22:17:14 -04:00
parent 6ccbaf2245
commit 3be6dae9f0
5 changed files with 49 additions and 12 deletions

View File

@ -44,9 +44,9 @@ get '/', sub ($c) { $c->redirect_to('view') };
group {
under 'view';
get '/:page', [page => qr/[0-9]+/], {page => 1}, sub ($c) {
my $base_path = $c->match->path_for(page => undef)->{'path'};
my $this_page = $c->param('page');
get '/:view_page', [view_page => qr/[0-9]+/], {view_page => 1}, sub ($c) {
my $base_path = $c->match->path_for(view_page => undef)->{'path'};
my $this_page = $c->param('view_page');
my $last_page = $c->thread->get_last_page();
my $threads = $c->thread->get_threads_by_page($this_page);
@ -97,14 +97,22 @@ any [qw{GET POST}], '/post', sub ($c) {
group {
under '/thread';
get '/:thread_id', [thread_id => qr/[0-9]+/], sub ($c) {
get '/:thread_id/:remark_page',
[thread_id => qr/[0-9]+/, remark_page => qr/[0-9]+/],
{remark_page => 1}, sub ($c) {
my $thread_id = $c->param('thread_id');
my $thread = $c->thread->get_thread_by_id($thread_id);
my $base_path = $c->match->path_for(remark_page => undef)->{'path'};
my $this_page = $c->param('remark_page');
my $last_page = $c->remark->get_last_page_by_thread_id($thread_id);
my $remarks = $c->remark->get_remarks_by_thread_id($thread_id);
if (my $thread_body = %$thread{'body'}) {
$c->stash(
thread => $thread,
base_path => $base_path,
this_page => $this_page,
last_page => $last_page,
remarks => $remarks
)
}
@ -115,7 +123,7 @@ group {
)
}
$c->render();
$c->render(template => 'thread_id');
};
};

View File

@ -24,7 +24,8 @@ Run the tests locally (against development environment)
## TODOs
1. Paging for remarks in single thread view
1. Paging for remarks is broken because I conflated ascending and
descending... Need to come back to this I need sleep
1. Form to create new remarks
1. I'm kinda hardcoding the single-thread view `link_to` in the
templates because I cannot for the life of me figure out how to use

View File

@ -14,10 +14,13 @@ sub new($class, $pg, $pg_reference) {
}, $class
}
sub get_remarks_by_thread_id($self, $thread_id) {
sub get_remarks_by_thread_id($self, $thread_id, $this_page = 1) {
my $date_format = %$self{'date_format'};
my $row_count = %$self{'remarks_per_page'};
my $offset = ($this_page - 1) * $row_count;
my @data = ($date_format, $thread_id, $row_count, $offset);
$self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hashes();
$self->pg->db->query(<<~'END_SQL', @data)->hashes();
SELECT remark_id AS id,
TO_CHAR(remark_date, ?) AS date,
remark_author AS author,
@ -25,12 +28,13 @@ sub get_remarks_by_thread_id($self, $thread_id) {
FROM remarks
WHERE thread_id = ?
AND NOT hidden_status
ORDER BY remark_date ASC;
ORDER BY remark_date ASC
LIMIT ? OFFSET ?;
END_SQL
}
sub remarks_per_page($self, $value = undef) {
$self->{'remarks_per_page'} = $value // $self->{'remarks_per_page'};
$self->{'remarks_per_page'} = $value // $self->{'remarks_per_page'}
}
sub create_remark(
@ -55,4 +59,23 @@ sub create_remark(
END_SQL
}
sub get_remark_count_by_thread_id($self, $thread_id) {
$self->pg->db->query(<<~'END_SQL', $thread_id)->hash->{'count'}
SELECT COUNT(*) AS count
FROM remarks
WHERE thread_id = ?
AND NOT hidden_status;
END_SQL
}
sub get_last_page_by_thread_id($self, $thread_id) {
my $remark_count = $self->get_remark_count_by_thread_id($thread_id);
my $last_page = int($remark_count / $self->{'remarks_per_page'});
# Add a page for 'remainder' posts
$last_page++ if $remark_count % $self->{'remarks_per_page'};
$last_page;
}
1;

View File

@ -21,4 +21,9 @@
</article>
<% } =%>
</div>
<% if ($last_page && $last_page != 1) { =%>
<nav>
<%= pagination $this_page, $last_page, ($base_path . '/{page}') %>
</nav>
<% } %>
<% } =%>