From ff61ab78a9bdf632aa6ac7e90889a0af0ac2f8a0 Mon Sep 17 00:00:00 2001 From: barnold Date: Thu, 22 Sep 2022 12:39:06 +0100 Subject: [PATCH] Add minimum book count as authors search criterion. --- lib/MyApp/Controller/Book.pm | 1 + lib/MyModel/Author.pm | 9 ++++++--- templates/book/authors.html.ep | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/MyApp/Controller/Book.pm b/lib/MyApp/Controller/Book.pm index fe482a2..ca90f01 100644 --- a/lib/MyApp/Controller/Book.pm +++ b/lib/MyApp/Controller/Book.pm @@ -36,6 +36,7 @@ sub authors ($self) { my $author_page = $author_model->get_page( $page_number, $self->param('name_like'), + $self->param('minimum_book_count') + 0, # cast blank to 0 ); my $pager = $author_page->pager; if ($page_number > $pager->last_page) { diff --git a/lib/MyModel/Author.pm b/lib/MyModel/Author.pm index b584723..4961271 100644 --- a/lib/MyModel/Author.pm +++ b/lib/MyModel/Author.pm @@ -4,14 +4,17 @@ use Moose; extends 'MyModel'; no warnings qw( experimental::signatures ); -sub get_page ($self, $page_number = 1, $name_like = undef) { +sub get_page ($self, $page_number = 1, $name_like = undef, $count_min = 0) { my $search_condition = (defined $name_like) ? { "me.name" => { ilike => "\%$name_like\%" } } : undef; my $rs = $self->schema->resultset('Author')->search_rs( $search_condition, - { prefetch => [ "books" ], - order_by => [ "me.name " ], + { join => 'books', + select => [ 'me.id', 'me.name', { count => 'books.id', -as => 'bcount' } ], + order_by => [ 'me.name' ], + group_by => [ 'me.id', 'me.name' ], + having => \[ 'count(books.id) >= ?', $count_min ], page => $page_number, rows => $self->rows_per_page, }, diff --git a/templates/book/authors.html.ep b/templates/book/authors.html.ep index 66088ab..cb7c303 100644 --- a/templates/book/authors.html.ep +++ b/templates/book/authors.html.ep @@ -6,6 +6,9 @@ %= form_for authors => { page_number => 1 } => begin %= label_for name_like => 'Name like' %= text_field name_like => flash('name_like') +%= label_for minimum_book_count => 'Wrote at least' +<%= number_field minimum_book_count => 0, + min => 0, maxlength => 4, size => 4 %> books. %= submit_button "Search" %= end