Implement /spam route

This commit is contained in:
swag 2022-01-12 15:18:34 -05:00
parent 3739016a50
commit 02355ee08b
4 changed files with 58 additions and 3 deletions

View File

@ -54,5 +54,4 @@ Add the `-v` option for more verbose output
## TODOs ## TODOs
1. /spam route would be interesting
1. Visitor counter 1. Visitor counter

View File

@ -58,6 +58,20 @@ get '/' => sub ($c) {
$c->render(); $c->render();
} => 'index'; } => 'index';
get '/spam' => sub ($c) {
my $this_page = $c->param('page') || 1;
my $last_page = $c->message->get_last_page('spam');
my $view_posts = $c->message->get_spam($this_page);
$c->stash(
view_posts => $view_posts,
this_page => $this_page,
last_page => $last_page
);
$c->render();
} => 'index';
any [qw{GET POST}], '/sign' => sub ($c) { any [qw{GET POST}], '/sign' => sub ($c) {
my $v = $c->validation(); my $v = $c->validation();

View File

@ -45,6 +45,38 @@ sub get_posts($self, $this_page = undef) {
} }
} }
sub get_spam($self, $this_page = undef) {
if ($this_page) {
my $row_count = $self->{'max_posts'};
my $offset = ($this_page - 1) * $row_count;
return $self->pg->db
->query(<<~'END_SQL', $row_count, $offset)->arrays();
SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name,
message,
homepage_url,
message_id
FROM messages
WHERE is_spam
ORDER BY message_date DESC
LIMIT ? OFFSET ?;
END_SQL
}
else {
return $self->pg->db->query(<<~'END_SQL')->arrays()
SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name,
message,
homepage_url,
message_id
FROM messages
WHERE is_spam
ORDER BY message_date DESC;
END_SQL
}
}
sub create_post($self, $name, $message, $url = undef, $spam = 1) { sub create_post($self, $name, $message, $url = undef, $spam = 1) {
if ($url) { if ($url) {
$self->pg->db->query(<<~'END_SQL', $name, $message, $url, $spam) $self->pg->db->query(<<~'END_SQL', $name, $message, $url, $spam)
@ -66,8 +98,8 @@ sub max_posts($self, $value = undef) {
return $self->{'max_posts'} = $value // $self->{'max_posts'} return $self->{'max_posts'} = $value // $self->{'max_posts'}
} }
sub get_last_page($self) { sub get_last_page($self, $want_spam = undef) {
my $post_count = $self->get_post_count(); my $post_count = $want_spam ? $self->get_spam_count() : $self->get_post_count();
my $last_page = int($post_count / $self->{'max_posts'}); my $last_page = int($post_count / $self->{'max_posts'});
# Add a page if we have "remainder" posts # Add a page if we have "remainder" posts
@ -82,4 +114,12 @@ sub get_post_count($self) {
END_SQL END_SQL
} }
sub get_spam_count($self) {
return $self->pg->db->query(<<~'END_SQL')->text()
SELECT COUNT(*)
FROM messages
WHERE is_spam;
END_SQL
}
1; 1;

View File

@ -19,6 +19,8 @@ $t->ua->max_redirects(1);
$t->get_ok('/')->status_is(200) $t->get_ok('/')->status_is(200)
->text_is(h2 => 'Messages from the World Wide Web'); ->text_is(h2 => 'Messages from the World Wide Web');
$t->get_ok('/spam')->status_is(200)
->text_is(h2 => 'Messages from the World Wide Web');
$t->get_ok('/sign')->status_is(200)->text_is(h2 => 'Sign the Guestbook'); $t->get_ok('/sign')->status_is(200)->text_is(h2 => 'Sign the Guestbook');
$t->post_ok('/sign', form => \%form)->status_is(200); $t->post_ok('/sign', form => \%form)->status_is(200);