From 02355ee08b51bf50b6a5c18dc308db87f5661d49 Mon Sep 17 00:00:00 2001 From: swag Date: Wed, 12 Jan 2022 15:18:34 -0500 Subject: [PATCH] Implement /spam route --- README.md | 1 - guestbook-ng.pl | 14 ++++++++++ lib/GuestbookNg/Model/Message.pm | 44 ++++++++++++++++++++++++++++++-- t/basic.t | 2 ++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 290c087..d9316a2 100644 --- a/README.md +++ b/README.md @@ -54,5 +54,4 @@ Add the `-v` option for more verbose output ## TODOs -1. /spam route would be interesting 1. Visitor counter diff --git a/guestbook-ng.pl b/guestbook-ng.pl index a6797d2..d0452d5 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -58,6 +58,20 @@ get '/' => sub ($c) { $c->render(); } => '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) { my $v = $c->validation(); diff --git a/lib/GuestbookNg/Model/Message.pm b/lib/GuestbookNg/Model/Message.pm index 0cd6509..71bed9d 100644 --- a/lib/GuestbookNg/Model/Message.pm +++ b/lib/GuestbookNg/Model/Message.pm @@ -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) { if ($url) { $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'} } -sub get_last_page($self) { - my $post_count = $self->get_post_count(); +sub get_last_page($self, $want_spam = undef) { + my $post_count = $want_spam ? $self->get_spam_count() : $self->get_post_count(); my $last_page = int($post_count / $self->{'max_posts'}); # Add a page if we have "remainder" posts @@ -82,4 +114,12 @@ sub get_post_count($self) { 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; diff --git a/t/basic.t b/t/basic.t index 0a6d78b..b5aea2b 100644 --- a/t/basic.t +++ b/t/basic.t @@ -19,6 +19,8 @@ $t->ua->max_redirects(1); $t->get_ok('/')->status_is(200) ->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->post_ok('/sign', form => \%form)->status_is(200);