Created function to search posts

This commit is contained in:
swaggboi 2023-10-27 16:57:21 -04:00
parent 7dc52a9655
commit f2e63b5ca3
3 changed files with 75 additions and 4 deletions

View File

@ -13,6 +13,7 @@ use HTML::Restrict;
use PostText::Model::Thread; use PostText::Model::Thread;
use PostText::Model::Remark; use PostText::Model::Remark;
use PostText::Model::Moderator; use PostText::Model::Moderator;
use PostText::Model::Page;
sub startup($self) { sub startup($self) {
$self->plugin('Config'); $self->plugin('Config');
@ -59,6 +60,10 @@ sub startup($self) {
) )
}); });
$self->helper(page => sub ($c) {
state $moderator = PostText::Model::Page->new(pg => $c->pg)
});
$self->helper(truncate_text => sub ($c, $input_text) { $self->helper(truncate_text => sub ($c, $input_text) {
my $truncated_text = 299 < length($input_text) my $truncated_text = 299 < length($input_text)
? substr($input_text, 0, 299) . '…' : $input_text; ? substr($input_text, 0, 299) . '…' : $input_text;
@ -85,7 +90,7 @@ sub startup($self) {
# Finish configuring some things # Finish configuring some things
$self->secrets($self->config->{'secrets'}) || die $@; $self->secrets($self->config->{'secrets'}) || die $@;
$self->pg->migrations->from_dir('migrations')->migrate(14); $self->pg->migrations->from_dir('migrations')->migrate(15);
if (my $threads_per_page = $self->config->{'threads_per_page'}) { if (my $threads_per_page = $self->config->{'threads_per_page'}) {
$self->thread->per_page($threads_per_page) $self->thread->per_page($threads_per_page)
@ -95,6 +100,10 @@ sub startup($self) {
$self->remark->per_page($remarks_per_page) $self->remark->per_page($remarks_per_page)
} }
if (my $results_per_page = $self->config->{'results_per_page'}) {
$self->page->per_page($results_per_page)
}
$self->asset->process; $self->asset->process;
push @{$self->commands->namespaces}, 'PostText::Command'; push @{$self->commands->namespaces}, 'PostText::Command';

View File

@ -0,0 +1,62 @@
package PostText::Model::Page;
use Mojo::Base -base, -signatures;
has 'pg';
has per_page => 5;
has date_format => 'Dy, FMDD Mon YYYY HH24:MI:SS TZHTZM';
# args: date_format, search_query, limit, offset
# SELECT 'thread' AS post_type,
# thread_id AS post_id,
# TO_CHAR(thread_date, $1) AS post_date,
# thread_author AS post_author,
# thread_body AS post_body,
# ts_rank(search_tokens, plainto_tsquery('english', $2)) AS search_rank
# FROM threads
# WHERE search_tokens @@ plainto_tsquery('english', $2)
# UNION ALL
# SELECT 'remark',
# remark_id,
# remark_date,
# remark_author,
# remark_body,
# ts_rank(search_tokens, plainto_tsquery('english', $2))
# FROM remarks
# WHERE search_tokens @@ plainto_tsquery('english', $2)
# ORDER BY search_rank DESC, post_date DESC
# LIMIT $3 OFFSET $4;
sub search($self, $search_query, $this_page = 1) {
my $date_format = $self->date_format;
my $row_count = $self->per_page;
my $offset = ($this_page - 1) * $row_count;
my @data = ($date_format, $search_query, $row_count, $offset);
$self->pg->db->query(<<~'END_SQL', @data)->hashes;
SELECT 'thread' AS post_type,
thread_id AS post_id,
TO_CHAR(thread_date, $1) AS post_date,
thread_author AS post_author,
thread_body AS post_body,
ts_rank(search_tokens, plainto_tsquery('english', $2)) AS search_rank
FROM threads
WHERE search_tokens @@ plainto_tsquery('english', $2)
UNION ALL
SELECT 'remark',
remark_id,
TO_CHAR(remark_date, $1),
remark_author,
remark_body,
ts_rank(search_tokens, plainto_tsquery('english', $2))
FROM remarks
WHERE search_tokens @@ plainto_tsquery('english', $2)
ORDER BY search_rank DESC, post_date DESC
LIMIT $3 OFFSET $4;
END_SQL
}
1;