From 8069a255a61dfa99a2e036318f4c5982ceee4dfa Mon Sep 17 00:00:00 2001 From: swaggboi Date: Fri, 2 Sep 2022 22:45:41 -0400 Subject: [PATCH] Implement single remark view --- PostText.pl | 28 +++++++++++++++---- README.md | 4 ++- lib/PostText/Model/Remark.pm | 22 +++++++++++++++ templates/list_page.html.ep | 2 +- templates/remark_id.html.ep | 14 ++++++++++ templates/thread_id.html.ep | 2 +- ...emark_page.html.ep => thread_page.html.ep} | 0 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 templates/remark_id.html.ep rename templates/{remark_page.html.ep => thread_page.html.ep} (100%) diff --git a/PostText.pl b/PostText.pl index 025de8c..26930d7 100755 --- a/PostText.pl +++ b/PostText.pl @@ -122,7 +122,7 @@ group { ); return $c->redirect_to( - 'remark_page', + 'thread_page', {thread_id => $thread_id} ); } @@ -144,13 +144,13 @@ group { group { under '/thread/:thread_id', [thread_id => qr/[0-9]+/]; - get '/:remark_page', - [remark_page => qr/[0-9]+/], - {remark_page => 1}, sub ($c) { # My editor is so confused by this lol + get '/:thread_page', + [thread_page => qr/[0-9]+/], + {thread_page => 1}, sub ($c) { # My editor is so confused by this lol my $thread_id = $c->param('thread_id'); my $thread = $c->thread->by_id($thread_id); - my $base_path = $c->match->path_for(remark_page => undef)->{'path'}; - my $this_page = $c->param('remark_page'); + my $base_path = $c->match->path_for(thread_page => undef)->{'path'}; + my $this_page = $c->param('thread_page'); my $last_page = $c->remark->last_page_for($thread_id); my $remarks = $c->remark->by_page_for($thread_id, $this_page); @@ -177,6 +177,22 @@ group { }; }; +# Remark +group { + under '/remark'; + + get '/:remark_id', [remark_id => qr/[0-9]+/], sub ($c) { + my $remark_id = $c->param('remark_id'); + my $remark = $c->remark->by_id($remark_id); + + $c->stash(status => 404) unless $remark->{'id'}; + + $c->stash(remark => $remark); + + $c->render; + }; +}; + # Configure things app->secrets(app->config->{'secrets'}) || die $@; diff --git a/README.md b/README.md index 692cb57..8dc1a99 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,9 @@ Run the tests locally (against development environment) ## TODOs -1. Single remark view +1. Add hyperlink to single remarks +1. More tests (single remark view at least... maybe more) +1. Grow into Mojo hybrid ## Crazy future ideas diff --git a/lib/PostText/Model/Remark.pm b/lib/PostText/Model/Remark.pm index f10cee1..a3e3135 100644 --- a/lib/PostText/Model/Remark.pm +++ b/lib/PostText/Model/Remark.pm @@ -86,4 +86,26 @@ sub last_for($self, $thread_id) { END_SQL } +sub by_id($self, $remark_id) { + my $date_format = $self->{'date_format'}; + + $self->pg->db->query(<<~'END_SQL', $date_format, $remark_id)->hash; + SELECT remark_id AS id, + TO_CHAR(remark_date, ?) AS date, + remark_author AS author, + remark_body AS body, + thread_id + FROM remarks + WHERE remark_id = ?; + END_SQL +} + +sub thread_id_for($self, $remark_id) { + $self->pg->db->query(<<~'END_SQL', $remark_id)->hash->{'thread_id'} + SELECT thread_id + FROM remarks + WHERE remark_id = ?; + END_SQL +} + 1; diff --git a/templates/list_page.html.ep b/templates/list_page.html.ep index 805a3da..9e4aa61 100644 --- a/templates/list_page.html.ep +++ b/templates/list_page.html.ep @@ -6,7 +6,7 @@
<%= link_to "#$thread->{'id'}", - remark_page => {thread_id => $thread->{'id'}} %> + thread_page => {thread_id => $thread->{'id'}} %>

<%= %$thread{'title'} %>

<%= %$thread{'date'} %>

diff --git a/templates/remark_id.html.ep b/templates/remark_id.html.ep new file mode 100644 index 0000000..46b3898 --- /dev/null +++ b/templates/remark_id.html.ep @@ -0,0 +1,14 @@ +% layout 'main'; +% title "Remark - #$remark->{'id'}"; +

<%= title %>

+
+
+ #<%= %$remark{'id'} %> +

<%= %$remark{'date'} %>

+
<%= %$remark{'author'} %>
+

<%= %$remark{'body'} %>

+
+
+ diff --git a/templates/thread_id.html.ep b/templates/thread_id.html.ep index d981a4c..2544146 100644 --- a/templates/thread_id.html.ep +++ b/templates/thread_id.html.ep @@ -28,7 +28,7 @@
<%= link_to "#$thread->{'id'}", - remark_page => {thread_id => $thread->{'id'}} %> + thread_page => {thread_id => $thread->{'id'}} %>

<%= %$thread{'title'} %>

<%= %$thread{'date'} %>

diff --git a/templates/remark_page.html.ep b/templates/thread_page.html.ep similarity index 100% rename from templates/remark_page.html.ep rename to templates/thread_page.html.ep