2021-12-04 23:06:05 -05:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
package GuestbookNg::Model::Message;
|
|
|
|
|
|
|
|
use Mojo::Base -base, -signatures;
|
|
|
|
|
|
|
|
has 'pg';
|
|
|
|
|
2021-12-12 01:50:07 -05:00
|
|
|
sub new($class, $pg, $pg_object) {
|
|
|
|
bless {
|
|
|
|
$pg => $pg_object,
|
|
|
|
max_posts => 5
|
2022-04-22 12:44:19 -04:00
|
|
|
}, $class
|
2021-12-04 23:06:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-22 22:23:10 -05:00
|
|
|
sub get_posts($self, $this_page = undef) {
|
|
|
|
if ($this_page) {
|
|
|
|
my $row_count = $self->{'max_posts'};
|
2021-12-25 01:46:39 -05:00
|
|
|
my $offset = ($this_page - 1) * $row_count;
|
2021-12-22 22:23:10 -05:00
|
|
|
|
2022-07-22 00:49:33 -04:00
|
|
|
$self->pg->db->query(<<~'END_SQL', $row_count, $offset)->arrays();
|
2022-01-02 01:31:33 -05:00
|
|
|
SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
|
2021-12-22 22:23:10 -05:00
|
|
|
visitor_name,
|
2022-01-01 19:22:46 -05:00
|
|
|
message,
|
2022-01-11 22:13:33 -05:00
|
|
|
homepage_url,
|
|
|
|
message_id
|
2021-12-31 22:49:45 -05:00
|
|
|
FROM messages
|
2022-01-02 01:31:33 -05:00
|
|
|
WHERE NOT is_spam
|
2021-12-22 22:23:10 -05:00
|
|
|
ORDER BY message_date DESC
|
|
|
|
LIMIT ? OFFSET ?;
|
|
|
|
END_SQL
|
|
|
|
}
|
|
|
|
else {
|
2022-07-22 00:49:33 -04:00
|
|
|
$self->pg->db->query(<<~'END_SQL')->arrays()
|
2022-01-02 01:31:33 -05:00
|
|
|
SELECT TO_CHAR(message_date, 'Dy Mon DD HH:MI:SS AM TZ YYYY'),
|
2021-12-22 22:23:10 -05:00
|
|
|
visitor_name,
|
2022-01-01 19:22:46 -05:00
|
|
|
message,
|
2022-01-11 22:13:33 -05:00
|
|
|
homepage_url,
|
|
|
|
message_id
|
2021-12-31 22:49:45 -05:00
|
|
|
FROM messages
|
2022-01-02 01:31:33 -05:00
|
|
|
WHERE NOT is_spam
|
2021-12-22 22:23:10 -05:00
|
|
|
ORDER BY message_date DESC;
|
|
|
|
END_SQL
|
|
|
|
}
|
2021-12-05 02:52:31 -05:00
|
|
|
}
|
|
|
|
|
2022-01-12 15:18:34 -05:00
|
|
|
sub get_spam($self, $this_page = undef) {
|
|
|
|
if ($this_page) {
|
|
|
|
my $row_count = $self->{'max_posts'};
|
|
|
|
my $offset = ($this_page - 1) * $row_count;
|
|
|
|
|
2022-07-22 00:49:33 -04:00
|
|
|
$self->pg->db->query(<<~'END_SQL', $row_count, $offset)->arrays();
|
2022-01-12 15:18:34 -05:00
|
|
|
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 {
|
2022-07-22 00:49:33 -04:00
|
|
|
$self->pg->db->query(<<~'END_SQL')->arrays()
|
2022-01-12 15:18:34 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:31:33 -05:00
|
|
|
sub create_post($self, $name, $message, $url = undef, $spam = 1) {
|
2021-12-31 23:45:21 -05:00
|
|
|
if ($url) {
|
2022-01-02 01:31:33 -05:00
|
|
|
$self->pg->db->query(<<~'END_SQL', $name, $message, $url, $spam)
|
2022-01-02 02:30:10 -05:00
|
|
|
INSERT INTO messages (visitor_name, message, homepage_url, is_spam)
|
|
|
|
VALUES (?, ?, ?, ?);
|
2021-12-31 23:45:21 -05:00
|
|
|
END_SQL
|
|
|
|
}
|
|
|
|
else {
|
2022-01-02 01:31:33 -05:00
|
|
|
$self->pg->db->query(<<~'END_SQL', $name, $message, $spam)
|
2022-01-02 02:30:10 -05:00
|
|
|
INSERT INTO messages (visitor_name, message, is_spam)
|
|
|
|
VALUES (?, ?, ?);
|
2021-12-31 23:45:21 -05:00
|
|
|
END_SQL
|
|
|
|
}
|
2021-12-05 02:52:31 -05:00
|
|
|
}
|
|
|
|
|
2021-12-12 01:50:07 -05:00
|
|
|
sub max_posts($self, $value = undef) {
|
2022-04-04 22:56:45 -04:00
|
|
|
$self->{'max_posts'} = $value // $self->{'max_posts'}
|
2021-12-12 01:50:07 -05:00
|
|
|
}
|
|
|
|
|
2022-01-12 15:18:34 -05:00
|
|
|
sub get_last_page($self, $want_spam = undef) {
|
|
|
|
my $post_count = $want_spam ? $self->get_spam_count() : $self->get_post_count();
|
2022-01-01 19:22:46 -05:00
|
|
|
my $last_page = int($post_count / $self->{'max_posts'});
|
2021-12-22 22:23:10 -05:00
|
|
|
|
2021-12-18 23:03:53 -05:00
|
|
|
# Add a page if we have "remainder" posts
|
2022-04-04 22:56:45 -04:00
|
|
|
$post_count % $self->{'max_posts'} ? ++$last_page : $last_page;
|
2021-12-12 01:50:07 -05:00
|
|
|
}
|
|
|
|
|
2021-12-22 22:23:10 -05:00
|
|
|
sub get_post_count($self) {
|
2022-04-04 22:56:45 -04:00
|
|
|
$self->pg->db->query(<<~'END_SQL')->text()
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM messages
|
|
|
|
WHERE NOT is_spam;
|
|
|
|
END_SQL
|
2021-12-22 22:23:10 -05:00
|
|
|
}
|
|
|
|
|
2022-01-12 15:18:34 -05:00
|
|
|
sub get_spam_count($self) {
|
2022-04-04 22:56:45 -04:00
|
|
|
$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
|
2022-01-12 15:18:34 -05:00
|
|
|
}
|
|
|
|
|
2022-07-22 00:49:33 -04:00
|
|
|
sub get_last_message_id($self) {
|
|
|
|
$self->pg->db->query(<<~'END_SQL')->text()
|
|
|
|
SELECT MAX(message_id)
|
|
|
|
FROM messages;
|
|
|
|
END_SQL
|
|
|
|
}
|
|
|
|
|
2021-12-04 23:06:05 -05:00
|
|
|
1;
|