Display replies in single thread view

This commit is contained in:
swaggboi 2022-08-20 12:42:04 -04:00
parent d73c7d5ac6
commit 17557a72a4
4 changed files with 43 additions and 7 deletions

View File

@ -100,9 +100,13 @@ group {
get '/:thread_id', [message_id => qr/[0-9]+/], sub ($c) { get '/:thread_id', [message_id => qr/[0-9]+/], sub ($c) {
my $thread_id = $c->param('thread_id'); my $thread_id = $c->param('thread_id');
my $thread = $c->thread->get_thread_by_id($thread_id); my $thread = $c->thread->get_thread_by_id($thread_id);
my $replies = $c->reply->get_replies_by_thread_id($thread_id);
if (%$thread{'body'}) { if (my $thread_body = %$thread{'body'}) {
$c->stash(thread => $thread) $c->stash(
thread => $thread,
replies => $replies
)
} }
else { else {
$c->stash( $c->stash(

View File

@ -13,4 +13,21 @@ sub new($class, $pg, $pg_reference) {
}, $class }, $class
} }
sub get_replies_by_thread_id($self, $thread_id) {
$self->pg->db->query(<<~'END_SQL', $thread_id)->hashes()
SELECT reply_id AS id,
TO_CHAR(reply_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY') AS date,
reply_author AS author,
reply_body AS body
FROM replies
WHERE thread_id = ?
AND NOT hidden_status
ORDER BY reply_date ASC;
END_SQL
}
sub exception($self, $exception) {
say $exception
}
1; 1;

View File

@ -92,4 +92,8 @@ sub get_thread_by_id($self, $thread_id) {
END_SQL END_SQL
} }
sub exception($self, $exception) {
say $exception
}
1; 1;

View File

@ -1,13 +1,24 @@
% layout 'main'; % layout 'main';
% title "View Thread - #$thread->{'id'}"; % title "View Thread - #$thread->{'id'}";
<h2><%= title %></h2> <h2><%= title %></h2>
<div class="threads"> <% if (my $thread_body = %$thread{'body'}) { =%>
<% if (my $thread = $thread) { %> <div class="threads">
<article class="thread"> <article class="thread">
<h3 class="title"><%= %$thread{'title'} %></h3> <h3 class="title"><%= %$thread{'title'} %></h3>
<h4 class="date"><%= %$thread{'date'} %></h4> <h4 class="date"><%= %$thread{'date'} %></h4>
<h5 class="author"><%= %$thread{'author'} %></h5> <h5 class="author"><%= %$thread{'author'} %></h5>
<p class="body"><%= %$thread{'body'} %></p> <p class="body"><%= $thread_body %></p>
</article>
</div>
<% } =%>
<% if (my $first_reply = @$replies[0]) { =%>
<div class="replies">
<% for my $reply (@$replies) { =%>
<article class="reply">
<h4 class="date"><%= %$reply{'date'} %></h4>
<h5 class="author"><%= %$reply{'author'} %></h5>
<p class="body"><%= %$reply{'body'} %></p>
</article> </article>
<% } =%> <% } =%>
</div> </div>
<% } =%>