diff --git a/lib/BurguillosInfo/Command/index.pm b/lib/BurguillosInfo/Command/index.pm index 47a561f..fc92c0b 100644 --- a/lib/BurguillosInfo/Command/index.pm +++ b/lib/BurguillosInfo/Command/index.pm @@ -28,7 +28,7 @@ sub run ( $self, @args ) { my $search_backend = $config->{search_backend}; my $search_index = $config->{search_index}; my $ua = Mojo::UserAgent->new; - my $posts = BurguillosInfo::Posts->new->Retrieve; + my $posts = BurguillosInfo::Posts->new->Retrieve(0); my $categories = BurguillosInfo::Categories->new->Retrieve; my $index = []; $self->_index_posts( $index, $posts ); diff --git a/lib/BurguillosInfo/Controller/Search.pm b/lib/BurguillosInfo/Controller/Search.pm index 09a05ac..2d9430f 100644 --- a/lib/BurguillosInfo/Controller/Search.pm +++ b/lib/BurguillosInfo/Controller/Search.pm @@ -11,6 +11,7 @@ use Mojo::Base 'Mojolicious::Controller', '-signatures'; use Mojo::UserAgent; use BurguillosInfo::IndexUtils; +use BurguillosInfo::Posts; my $index_utils = BurguillosInfo::IndexUtils->new; @@ -36,9 +37,23 @@ sub search ($self) { return $self->render( status => 400, json => { ok => 0 } ); } my $searchObjects = $output->{searchObjects}; + $searchObjects = [grep { $self->filterSearch($_) } @$searchObjects]; return $self->render( status => 200, json => { ok => 1, searchObjects => $searchObjects } ); } + +sub filterSearch($self, $searchObject) { + my $url = $searchObject->{url}; + my ($posts_by_categories, $posts) = BurguillosInfo::Posts->Retrieve; + my $slug; + if ($url =~ m{^/posts/([^/]+?)(?:\?.*)?$}) { + $slug = $1; + if (!defined $posts->{$slug}) { + return 0; + } + } + return 1 +} 1; diff --git a/lib/BurguillosInfo/Posts.pm b/lib/BurguillosInfo/Posts.pm index bd39522..11d95f5 100644 --- a/lib/BurguillosInfo/Posts.pm +++ b/lib/BurguillosInfo/Posts.pm @@ -32,21 +32,22 @@ sub new { return bless {}, shift; } -sub _ReturnCacheFilter { - my $self = shift; +sub _ReturnCacheFilter($self, $filters = 1) { my %posts_by_category_filtered; my %posts_by_slug_filtered; my $iso8601 = DateTime::Format::ISO8601->new; my $current_date = DateTime->now; for my $category ( keys %$cached_posts_by_category ) { for my $post ( @{ $cached_posts_by_category->{$category} } ) { - my $date_post; - eval { $date_post = $iso8601->parse_datetime( $post->{date} ); }; - if ($@) { - print Data::Dumper::Dumper $post; - } - if ( $date_post > $current_date ) { - next; + if ($filters) { + my $date_post; + eval { $date_post = $iso8601->parse_datetime( $post->{date} ); }; + if ($@) { + print $@ . ': ' . Data::Dumper::Dumper $post; + } + if ( $date_post > $current_date ) { + next; + } } $posts_by_slug_filtered{ $post->{slug} } = $post; $posts_by_category_filtered{ $post->{category} } //= []; @@ -155,13 +156,12 @@ sub _GeneratePostCache ($self) { } } -sub Retrieve { - my $self = shift; +sub Retrieve($self, $filters = 1) { if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) { - return $self->_ReturnCacheFilter; + return $self->_ReturnCacheFilter($filters); } $self->_GeneratePostCache(); - return $self->_ReturnCacheFilter; + return $self->_ReturnCacheFilter($filters); } my $cache_all_post_categories = {};