Commit 3b75fe81 authored by Keith Jolley's avatar Keith Jolley
Browse files

Add routes to loci.

Make sure all statement handles are finished before dropping connection.
Refactor paging.
parent 9d44adbb
......@@ -27,6 +27,7 @@ use Log::Log4perl qw(get_logger);
my $logger = get_logger('BIGSdb.Application_Initiate');
use BIGSdb::Utils;
use BIGSdb::REST::Routes::Isolates;
use BIGSdb::REST::Routes::Loci;
use BIGSdb::REST::Routes::Resources;
use BIGSdb::REST::Routes::Users;
use constant PAGE_SIZE => 100;
......@@ -117,7 +118,6 @@ hook after => sub {
my $self = setting('self');
$self->{'dataConnector'}->drop_connection( { host => $self->{'system'}->{'host'}, dbase_name => $self->{'system'}->{'db'} } );
};
sub get_set_id {
my ($self) = @_;
if ( ( $self->{'system'}->{'sets'} // '' ) eq 'yes' ) {
......@@ -130,7 +130,7 @@ sub get_set_id {
#Set view if defined in set.
sub _initiate_view {
my ($self) = @_;
return if $self->{'system'}->{'dbtype'} ne 'isolates';
return if ($self->{'system'}->{'dbtype'} // '') ne 'isolates';
my $set_id = $self->get_set_id;
if ( defined $self->{'system'}->{'view'} && $set_id ) {
if ( $self->{'system'}->{'views'} && BIGSdb::Utils::is_int($set_id) ) {
......@@ -148,4 +148,36 @@ sub get_resources {
$self->{'datastore'}->run_query( "SELECT * FROM resources ORDER BY name", undef, { fetch => 'all_arrayref', slice => {} } );
return $dbases;
}
sub get_paging {
my ( $self, $route, $pages, $page ) = @_;
my $paging = [];
if ( $page > 1 ) {
push @$paging, { first => request->uri_base . "$route?page=1&page_size=$self->{'page_size'}" };
push @$paging, { previous => request->uri_base . "$route?page=" . ( $page - 1 ) . "&page_size=$self->{'page_size'}" };
}
if ( $page < $pages ) {
push @$paging, { next => request->uri_base . "$route?page=" . ( $page + 1 ) . "&page_size=$self->{'page_size'}" };
}
if ( $page != $pages ) {
push @$paging, { last => request->uri_base . "$route?page=$pages&page_size=$self->{'page_size'}" };
}
return $paging;
}
sub clean_locus {
my ( $self, $locus ) = @_;
return if !defined $locus;
my $locus_info = $self->{'datastore'}->get_locus_info($locus);
my $set_id = $self->get_set_id;
if ($set_id) {
my $set_name = $self->{'datastore'}->run_query(
"SELECT set_name FROM set_loci WHERE set_id=? AND locus=?",
[ $set_id, $locus ],
{ fetch => 'row_array', cache => 'clean_locus' }
);
return $set_name if $set_name;
}
return $locus;
}
1;
......@@ -21,9 +21,7 @@ use strict;
use warnings;
use 5.010;
use Dancer2 appname => 'BIGSdb::REST::Interface';
use Log::Log4perl qw(get_logger);
use BIGSdb::Utils;
my $logger = get_logger('BIGSdb.Rest');
#Isolate database routes
get '/db/:db/isolates' => sub {
......@@ -39,17 +37,7 @@ get '/db/:db/isolates' => sub {
my $values = [];
if (@$ids) {
my $paging = [];
if ( $page > 1 ) {
push @$paging, { first => request->uri_base . "/db/$db/isolates?page=1&page_size=$self->{'page_size'}" };
push @$paging, { previous => request->uri_base . "/db/$db/isolates?page=" . ( $page - 1 ) . "&page_size=$self->{'page_size'}" };
}
if ( $page < $pages ) {
push @$paging, { next => request->uri_base . "/db/$db/isolates?page=" . ( $page + 1 ) . "&page_size=$self->{'page_size'}" };
}
if ( $page != $pages ) {
push @$paging, { last => request->uri_base . "/db/$db/isolates?page=$pages&page_size=$self->{'page_size'}" };
}
my $paging = $self->get_paging( "/db/$db/isolates", $pages, $page );
push @$values, { paging => $paging } if $pages > 1;
my @links;
push @links, request->uri_for("/db/$db/isolates/$_")->as_string foreach @$ids;
......
#Written by Keith Jolley
#Copyright (c) 2014, University of Oxford
#E-mail: keith.jolley@zoo.ox.ac.uk
#
#This file is part of Bacterial Isolate Genome Sequence Database (BIGSdb).
#
#BIGSdb is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#BIGSdb is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with BIGSdb. If not, see <http://www.gnu.org/licenses/>.
package BIGSdb::REST::Routes::Loci;
use strict;
use warnings;
use 5.010;
use Dancer2 appname => 'BIGSdb::REST::Interface';
#Locus routes
get '/db/:db/loci' => sub {
my $self = setting('self');
my ($db) = params->{'db'};
my $page = ( BIGSdb::Utils::is_int( param('page') ) && param('page') > 0 ) ? param('page') : 1;
my $set_id = $self->get_set_id;
my $set_clause =
$set_id
? " WHERE (id IN (SELECT locus FROM scheme_members WHERE scheme_id IN (SELECT scheme_id FROM set_schemes "
. "WHERE set_id=$set_id)) OR id IN (SELECT locus FROM set_loci WHERE set_id=$set_id))"
: '';
my $locus_count = $self->{'datastore'}->run_query("SELECT COUNT(*) FROM loci$set_clause");
my $pages = int( $locus_count / $self->{'page_size'} ) + 1;
my $offset = ( $page - 1 ) * $self->{'page_size'};
my $loci = $self->{'datastore'}->run_query( "SELECT id FROM loci$set_clause ORDER BY id OFFSET $offset LIMIT $self->{'page_size'}",
undef, { fetch => 'col_arrayref' } );
my $values = [];
if (@$loci) {
my $paging = $self->get_paging( "/db/$db/loci", $pages, $page );
push @$values, { paging => $paging } if $pages > 1;
my @links;
foreach my $locus (@$loci) {
my $cleaned_locus = $self->clean_locus($locus);
push @links, request->uri_for("/db/$db/loci/$cleaned_locus")->as_string;
}
push @$values, { loci => \@links };
}
return $values;
};
1;
......@@ -21,8 +21,6 @@ use strict;
use warnings;
use 5.010;
use Dancer2 appname => 'BIGSdb::REST::Interface';
use Log::Log4perl qw(get_logger);
my $logger = get_logger('BIGSdb.Rest');
#Resource description routes
get '/' => sub {
......
......@@ -21,9 +21,7 @@ use strict;
use warnings;
use 5.010;
use Dancer2 appname => 'BIGSdb::REST::Interface';
use Log::Log4perl qw(get_logger);
use BIGSdb::Utils;
my $logger = get_logger('BIGSdb.Rest');
#User routes
get '/db/:db/users/:user' => sub {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment