Created
September 19, 2012 13:49
-
-
Save fgabolde/3749783 to your computer and use it in GitHub Desktop.
Demonstrate incremental building of DBIC queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/routers' => sub { | |
my $routers = schema->resultset('Router')->search({}, | |
{ join => [ qw/network device_type/ ], | |
prefetch => [ qw/network device_type/ ], | |
order_by => 'me.name' }); | |
my $networks = schema->resultset('Network')->search({}, | |
{ order_by => 'name' }); | |
my $device_types = schema->resultset('DeviceType')->search({}, | |
{ order_by => 'name' }); | |
# handle search parameters by filtering the resultset incrementally | |
my $router_name = params->{'router-name'}; | |
my $network_id = params->{'network-id'}; | |
my $device_type_id = params->{'device-type-id'}; | |
my $current_page = params->{'page'} || 1; | |
my $page_size = params->{'page-size'} || 100; | |
if (defined $router_name and $router_name ne '') { | |
$routers = $routers->search({ 'me.name' => { LIKE => $router_name } }); | |
} | |
if (defined $network_id and $network_id ne '') { | |
$routers = $routers->search({ 'network.id' => $network_id }); | |
} | |
if (defined $device_type_id and $device_type_id ne '') { | |
$routers = $routers->search({ 'device_type.id' => $device_type_id }); | |
} | |
# DON'T ADD SEARCH PARAMETERS AFTER THIS | |
my $count_before_paging = $routers->count; | |
# DON'T ADD PAGING PARAMETERS BEFORE THIS | |
$routers = $routers->search(undef, | |
{ rows => $page_size, | |
page => $current_page }); | |
template 'router-list', { routers => [ $routers->all ], | |
router_total_count => $count_before_paging, | |
router_paged_count => $routers->count, | |
networks => [ $networks->all ], | |
device_types => [ $device_types->all ], | |
current_page => $current_page, | |
max_page => ceil($count_before_paging / $page_size) }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment