Created function to search posts
This commit is contained in:
parent
7dc52a9655
commit
f2e63b5ca3
|
@ -13,6 +13,7 @@ use HTML::Restrict;
|
|||
use PostText::Model::Thread;
|
||||
use PostText::Model::Remark;
|
||||
use PostText::Model::Moderator;
|
||||
use PostText::Model::Page;
|
||||
|
||||
sub startup($self) {
|
||||
$self->plugin('Config');
|
||||
|
@ -56,7 +57,11 @@ sub startup($self) {
|
|||
state $moderator = PostText::Model::Moderator->new(
|
||||
pg => $c->pg,
|
||||
authenticator => $c->authenticator
|
||||
)
|
||||
)
|
||||
});
|
||||
|
||||
$self->helper(page => sub ($c) {
|
||||
state $moderator = PostText::Model::Page->new(pg => $c->pg)
|
||||
});
|
||||
|
||||
$self->helper(truncate_text => sub ($c, $input_text) {
|
||||
|
@ -85,7 +90,7 @@ sub startup($self) {
|
|||
# Finish configuring some things
|
||||
$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'}) {
|
||||
$self->thread->per_page($threads_per_page)
|
||||
|
@ -95,6 +100,10 @@ sub startup($self) {
|
|||
$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;
|
||||
|
||||
push @{$self->commands->namespaces}, 'PostText::Command';
|
||||
|
|
|
@ -7,7 +7,7 @@ sub flagged($self) {
|
|||
my @post_links = map {
|
||||
$self->url_for(
|
||||
'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'}
|
||||
)
|
||||
)
|
||||
} @{$flagged_posts};
|
||||
|
||||
$self->stash(post_links => \@post_links);
|
||||
|
@ -20,7 +20,7 @@ sub hidden($self) {
|
|||
my @post_links = map {
|
||||
$self->url_for(
|
||||
'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'}
|
||||
)
|
||||
)
|
||||
} @{$hidden_posts};
|
||||
|
||||
$self->stash(post_links => \@post_links);
|
||||
|
|
62
lib/PostText/Model/Page.pm
Normal file
62
lib/PostText/Model/Page.pm
Normal 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;
|
Loading…
Reference in a new issue