diff --git a/PostText.pl b/PostText.pl index 7234fbd..583d096 100755 --- a/PostText.pl +++ b/PostText.pl @@ -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,15 +97,23 @@ 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, - remarks => $remarks + thread => $thread, + base_path => $base_path, + this_page => $this_page, + last_page => $last_page, + remarks => $remarks ) } else { @@ -115,7 +123,7 @@ group { ) } - $c->render(); + $c->render(template => 'thread_id'); }; }; diff --git a/README.md b/README.md index a526d7f..ab557ff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/PostText/Model/Remark.pm b/lib/PostText/Model/Remark.pm index 7f06cfb..b643bcc 100644 --- a/lib/PostText/Model/Remark.pm +++ b/lib/PostText/Model/Remark.pm @@ -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; diff --git a/templates/thread_id.html.ep b/templates/thread_id.html.ep index b22a3ad..fda63fb 100644 --- a/templates/thread_id.html.ep +++ b/templates/thread_id.html.ep @@ -21,4 +21,9 @@ <% } =%> + <% if ($last_page && $last_page != 1) { =%> + + <% } %> <% } =%> diff --git a/templates/page.html.ep b/templates/view_page.html.ep similarity index 100% rename from templates/page.html.ep rename to templates/view_page.html.ep