Cleaned up the model and return the homepage URL for get_posts()

This commit is contained in:
swag 2022-01-01 19:22:46 -05:00
parent d9b114f9ea
commit 6eca53f968

View File

@ -18,20 +18,23 @@ sub get_posts($self, $this_page = undef) {
my $row_count = $self->{'max_posts'}; my $row_count = $self->{'max_posts'};
my $offset = ($this_page - 1) * $row_count; my $offset = ($this_page - 1) * $row_count;
$self->pg->db->query(<<~'END_SQL', $row_count, $offset)->arrays(); 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'), SELECT to_char(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name, visitor_name,
message message,
homepage_url
FROM messages FROM messages
ORDER BY message_date DESC ORDER BY message_date DESC
LIMIT ? OFFSET ?; LIMIT ? OFFSET ?;
END_SQL END_SQL
} }
else { else {
$self->pg->db->query(<<~'END_SQL')->arrays() return $self->pg->db->query(<<~'END_SQL')->arrays()
SELECT to_char(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'), SELECT to_char(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
visitor_name, visitor_name,
message message,
homepage_url
FROM messages FROM messages
ORDER BY message_date DESC; ORDER BY message_date DESC;
END_SQL END_SQL
@ -40,8 +43,13 @@ sub get_posts($self, $this_page = undef) {
sub create_post($self, $name, $message, $url = undef) { sub create_post($self, $name, $message, $url = undef) {
if ($url) { if ($url) {
$self->pg->db->query(<<~'END_SQL', $name, $url, $message) $self->pg->db->query(<<~'END_SQL', $name, $message, $url)
INSERT INTO messages (message_date, visitor_name, homepage_url, message) INSERT INTO messages (
message_date,
visitor_name,
message,
homepage_url
)
VALUES (NOW(), ?, ?, ?); VALUES (NOW(), ?, ?, ?);
END_SQL END_SQL
} }
@ -51,22 +59,24 @@ sub create_post($self, $name, $message, $url = undef) {
VALUES (NOW(), ?, ?); VALUES (NOW(), ?, ?);
END_SQL END_SQL
} }
return;
} }
sub max_posts($self, $value = undef) { sub max_posts($self, $value = undef) {
$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) {
my $post_count = $self->get_post_count(); my $post_count = $self->get_post_count();
my $last_page = sprintf('%d', $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
return $post_count % $self->{'max_posts'} ? ++$last_page : $last_page; return $post_count % $self->{'max_posts'} ? ++$last_page : $last_page;
} }
sub get_post_count($self) { sub get_post_count($self) {
$self->pg->db->query('SELECT count(*) FROM messages;')->text() return $self->pg->db->query('SELECT count(*) FROM messages;')->text()
} }
1; 1;