Implement get_post_by_id() and the message_id view

This commit is contained in:
swagg boi 2022-04-04 22:56:45 -04:00
parent 1809a765f2
commit f2ca5cc1b6
3 changed files with 56 additions and 16 deletions

View file

@ -132,12 +132,20 @@ any [qw{GET POST}], '/sign' => sub ($c) {
$c->render();
};
under '/message';
group {
under '/message';
get '/:id', [id => qr/[0-9]+/] => sub ($c) {
my $message_id = $c->param('id');
get '/:message_id', [message_id => qr/[0-9]+/] => sub ($c) {
my $message_id = $c->param('message_id');
my @view_post = $c->message->get_post_by_id($message_id);
$c->render(text => "You've requested message number: $message_id");
$c->stash(status => 404) unless $view_post[0];
$c->stash(view_post => @view_post);
#$c->render(text => "You've requested message number: $message_id");
$c->render();
};
};
# Send it

View file

@ -95,7 +95,7 @@ sub create_post($self, $name, $message, $url = undef, $spam = 1) {
}
sub max_posts($self, $value = undef) {
return $self->{'max_posts'} = $value // $self->{'max_posts'}
$self->{'max_posts'} = $value // $self->{'max_posts'}
}
sub get_last_page($self, $want_spam = undef) {
@ -103,23 +103,35 @@ sub get_last_page($self, $want_spam = undef) {
my $last_page = int($post_count / $self->{'max_posts'});
# Add a page if we have "remainder" posts
return $post_count % $self->{'max_posts'} ? ++$last_page : $last_page;
$post_count % $self->{'max_posts'} ? ++$last_page : $last_page;
}
sub get_post_count($self) {
return $self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE NOT is_spam;
END_SQL
$self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE NOT is_spam;
END_SQL
}
sub get_spam_count($self) {
return $self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE is_spam;
END_SQL
$self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE is_spam;
END_SQL
}
sub get_post_by_id($self, $message_id) {
$self->pg->db->query(<<~'END_SQL', $message_id)->array()
SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name,
message,
homepage_url,
message_id
FROM messages
WHERE message_id = ?;
END_SQL
}
1;

View file

@ -0,0 +1,20 @@
% layout 'default';
% title 'View';
<h2>Messages from the World Wide Web</h2>
<div class="articles">
<article>
<h3><%= @$view_post[0] %></h3>
<div class="message field">
<blockquote><p><%= @$view_post[2] %></p></blockquote>
</div>
<div class="name field">
<p style="margin: 0 0 0 0;">
- <%= @$view_post[1] %>
<% if (@$view_post[3]) { =%>
(<%= link_to Homepage => @$view_post[3] %>)
<% } =%>
</p>
</div>
<span hidden>Message ID: <%= @$view_post[4] %></span>
</article>
</div>