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
|
|
|
|
}
|
2021-12-04 23:06:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-05 02:52:31 -05:00
|
|
|
sub get_posts($self) {
|
|
|
|
$self->pg->db->query('SELECT date, name, msg FROM messages;')->arrays()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub send_post($self, $name, $msg) {
|
|
|
|
$self->pg->db->query(
|
|
|
|
'INSERT INTO messages (date, name, msg)
|
|
|
|
VALUES (NOW(), ?, ?);', $name, $msg
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-12 01:50:07 -05:00
|
|
|
sub view_posts($self, $this_page, $last_page, @posts) {
|
|
|
|
my $last_post = $this_page * $self->{'max_posts'} - 1;
|
|
|
|
my $first_post = $last_post - $self->{'max_posts'} + 1;
|
|
|
|
|
|
|
|
grep defined, @posts[$first_post..$last_post];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub max_posts($self, $value = undef) {
|
|
|
|
$self->{'max_posts'} = $value ? $value : $self->{'max_posts'}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_last_page($self, @posts) {
|
2021-12-18 23:03:53 -05:00
|
|
|
# Add a page if we have "remainder" posts
|
|
|
|
if (scalar(@posts) % $self->{'max_posts'}) {
|
|
|
|
sprintf('%d', scalar(@posts) / $self->{'max_posts'}) + 1
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf('%d', scalar(@posts) / $self->{'max_posts'})
|
|
|
|
}
|
2021-12-12 01:50:07 -05:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:06:05 -05:00
|
|
|
1;
|