Implement shortened method names and create aliases to old names

This commit is contained in:
swagg boi 2022-09-02 20:40:26 -04:00
parent 2054de053e
commit 29a11f9886
2 changed files with 47 additions and 22 deletions

View file

@ -14,13 +14,13 @@ sub new($class, $pg, $pg_reference) {
}, $class
}
sub get_remarks_by_thread_id($self, $thread_id, $this_page = 1) {
sub by_page_for($self, $thread_id, $this_page = 1) {
my $date_format = %$self{'date_format'};
my $row_count = %$self{'remarks_per_page'};
my $offset = ($this_page - 1) * $row_count;
my @data = ($date_format, $thread_id, $row_count, $offset);
$self->pg->db->query(<<~'END_SQL', @data)->hashes();
$self->pg->db->query(<<~'END_SQL', @data)->hashes;
SELECT remark_id AS id,
TO_CHAR(remark_date, ?) AS date,
remark_author AS author,
@ -33,12 +33,15 @@ sub get_remarks_by_thread_id($self, $thread_id, $this_page = 1) {
END_SQL
}
sub remarks_per_page($self, $value = undef) {
*get_remarks_by_thread_id = \&by_page_for;
sub per_page($self, $value = undef) {
$self->{'remarks_per_page'} = $value // $self->{'remarks_per_page'}
}
sub create_remark($self, $thread_id, $author, $body, $hidden = 0, $flagged = 0)
{
*remarks_per_page = \&per_page;
sub create($self, $thread_id, $author, $body, $hidden = 0, $flagged = 0) {
my @data = ($thread_id, $author, $body, $hidden, $flagged);
$self->pg->db->query(<<~'END_SQL', @data);
@ -53,7 +56,9 @@ sub create_remark($self, $thread_id, $author, $body, $hidden = 0, $flagged = 0)
END_SQL
}
sub get_remark_count_by_thread_id($self, $thread_id) {
*create_remark = \&create;
sub count_for($self, $thread_id) {
$self->pg->db->query(<<~'END_SQL', $thread_id)->hash->{'count'}
SELECT COUNT(*) AS count
FROM remarks
@ -62,8 +67,10 @@ sub get_remark_count_by_thread_id($self, $thread_id) {
END_SQL
}
sub get_last_page_by_thread_id($self, $thread_id) {
my $remark_count = $self->get_remark_count_by_thread_id($thread_id);
*get_remark_count_by_thread_id = \&count_for;
sub last_page_for($self, $thread_id) {
my $remark_count = $self->count_for($thread_id);
my $last_page = int($remark_count / $self->{'remarks_per_page'});
# Add a page for 'remainder' posts
@ -72,10 +79,12 @@ sub get_last_page_by_thread_id($self, $thread_id) {
$last_page;
}
sub last_remark($self, $thread_id) {
*get_last_page_by_thread_id = \&last_page_for;
sub last_remark_for($self, $thread_id) {
my $date_format = $self->{'date_format'};
$self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash();
$self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash;
SELECT remark_id AS id,
TO_CHAR(remark_date, ?) AS date,
remark_author AS author,
@ -87,4 +96,6 @@ sub last_remark($self, $thread_id) {
END_SQL
}
*last_remark = \&last_remark_for;
1;

View file

@ -14,7 +14,7 @@ sub new($class, $pg, $pg_reference) {
}, $class
}
sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) {
sub create($self, $author, $title, $body, $hidden = 0, $flagged = 0) {
my @data = ($author, $title, $body, $hidden, $flagged);
$self->pg->db->query(<<~'END_SQL', @data);
@ -29,8 +29,10 @@ sub create_thread($self, $author, $title, $body, $hidden = 0, $flagged = 0) {
END_SQL
}
sub get_all_threads($self) {
$self->pg->db->query(<<~'END_SQL', %$self{'date_format'})->hashes()
*create_thread = \&create;
sub dump_all($self) {
$self->pg->db->query(<<~'END_SQL', %$self{'date_format'})->hashes
SELECT thread_id AS id,
TO_CHAR(thread_date, ?) AS date,
thread_author AS author,
@ -42,13 +44,15 @@ sub get_all_threads($self) {
END_SQL
}
sub get_threads_by_page($self, $this_page = 1) {
*get_all_threads = \&dump_all;
sub by_page($self, $this_page = 1) {
my $date_format = %$self{'date_format'};
my $row_count = $self->{'threads_per_page'};
my $offset = ($this_page - 1) * $row_count;
$self->pg->db
->query(<<~'END_SQL', $date_format, $row_count, $offset)->hashes();
->query(<<~'END_SQL', $date_format, $row_count, $offset)->hashes;
SELECT thread_id AS id,
TO_CHAR(thread_date, ?) AS date,
thread_author AS author,
@ -61,12 +65,16 @@ sub get_threads_by_page($self, $this_page = 1) {
END_SQL
}
sub threads_per_page($self, $value = undef) {
$self->{'threads_per_page'} = $value // $self->{'threads_per_page'};
*get_threads_by_page = \&by_page;
sub per_page($self, $value = undef) {
$self->{'threads_per_page'} = $value // $self->{'threads_per_page'}
}
sub get_last_page($self) {
my $thread_count = $self->get_thread_count();
*threads_per_page = \&per_page;
sub last_page($self) {
my $thread_count = $self->count;
my $last_page = int($thread_count / $self->{'threads_per_page'});
# Add a page for 'remainder' posts
@ -75,7 +83,9 @@ sub get_last_page($self) {
$last_page;
}
sub get_thread_count($self) {
*get_last_page = \&last_page;
sub count($self) {
$self->pg->db->query(<<~'END_SQL')->hash->{'count'}
SELECT COUNT(*) AS count
FROM threads
@ -83,10 +93,12 @@ sub get_thread_count($self) {
END_SQL
}
sub get_thread_by_id($self, $thread_id) {
*get_thread_count = \&count;
sub by_id($self, $thread_id) {
my $date_format = %$self{'date_format'};
$self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash();
$self->pg->db->query(<<~'END_SQL', $date_format, $thread_id)->hash;
SELECT thread_id AS id,
TO_CHAR(thread_date, ?) AS date,
thread_author AS author,
@ -97,4 +109,6 @@ sub get_thread_by_id($self, $thread_id) {
END_SQL
}
*get_thread_by_id = \&by_id;
1;