diff --git a/README.md b/README.md index f2f836e..13efa40 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,6 @@ tests locally: ## TODOs -1. Do something if a search returns nothing 1. Hide ancient posts 1. Put a link to search_page somewhere 1. "All new posts flagged" mode (require approval for new posts) diff --git a/lib/PostText/Controller/Page.pm b/lib/PostText/Controller/Page.pm index e5acc8b..2b5dba1 100644 --- a/lib/PostText/Controller/Page.pm +++ b/lib/PostText/Controller/Page.pm @@ -59,13 +59,19 @@ sub search($self) { $v->required('q' )->size(1, 2_047); $v->optional('page'); - $search_query = $v->param('q'); - $this_page = $v->param('page') || 1; - $last_page = $self->page->last_page_for($search_query); - $base_path = $self->url_for->query(q => $search_query); - $search_results = $self->page->search($search_query, $this_page); + if ($v->has_error) { + $self->stash(status => 400) + } + else { + $search_query = $v->param('q'); + $this_page = $v->param('page') || 1; + $last_page = $self->page->last_page_for($search_query); + $base_path = $self->url_for->query(q => $search_query); + $search_results = $self->page->search($search_query, $this_page); - $self->stash(status => 400) if $v->has_error; + $self->stash(status => 404, error => 'No posts found. 🔎') + unless scalar @{$search_results}; + } } $self->stash( diff --git a/t/search.t b/t/search.t index d7ffa4a..4ebbef9 100644 --- a/t/search.t +++ b/t/search.t @@ -21,7 +21,10 @@ subtest 'Search after CAPTCHA', sub { $t->get_ok('/human/search')->status_is(200) ->text_like(h2 => qr/Search Posts/); - $t->get_ok('/human/search?q=test')->status_is(200) + $t->get_ok('/human/search?q=aaaaaaaaaa')->status_is(404) + ->text_like(p => qr/No posts found/); + + $t->get_ok('/human/search?q=lmao')->status_is(200) ->text_like(h3 => qr/Results/); $t->get_ok("/human/search?q=$invalid_query")->status_is(400)