Break out utility functions into separate file, to allow other api portions to use
This commit is contained in:
@@ -10,55 +10,7 @@ use Dancer::Exception qw(:all);
|
||||
|
||||
use App::Netdisco::Web::Plugin;
|
||||
|
||||
sub parse_search_params {
|
||||
my $params = shift;
|
||||
my $search = {};
|
||||
my $partial = $params->{partial} || 0;
|
||||
foreach my $param (keys %{$params}) {
|
||||
if ($param ne 'return_url' and $param ne 'partial') {
|
||||
if ($partial == 1) {
|
||||
$search->{"text(".$param.")"} = { like => '%'.$params->{$param}.'%'};
|
||||
}
|
||||
else {
|
||||
$search->{$param} = $params->{$param};
|
||||
}
|
||||
}
|
||||
}
|
||||
return $search;
|
||||
}
|
||||
sub format_data {
|
||||
my $items = shift;
|
||||
|
||||
header( 'Content-Type' => 'application/json');
|
||||
my $results = {};
|
||||
if (ref($items) =~ m/ResultSet/) {
|
||||
my @hashes;
|
||||
foreach my $item ($items->all) {
|
||||
my $c = {};
|
||||
my $columns = $item->{_column_data};
|
||||
foreach my $col (keys %{$columns}) {
|
||||
$c->{$col} = $columns->{$col};
|
||||
}
|
||||
push @hashes, $c;
|
||||
}
|
||||
$results->{data} = \@hashes;
|
||||
}
|
||||
elsif (ref($items) =~ m/Result/) {
|
||||
$results->{data} = $items->{_column_data};
|
||||
}
|
||||
else {
|
||||
$results->{data} = $items;
|
||||
}
|
||||
return to_json $results;
|
||||
};
|
||||
|
||||
sub format_error {
|
||||
my $status = shift;
|
||||
my $message = shift;
|
||||
header( 'Content-Type' => 'application/json');
|
||||
status $status;
|
||||
return to_json { error => $message };
|
||||
}
|
||||
use App::Netdisco::Web::Plugin::API::Util;
|
||||
|
||||
get '/api/device/all' => sub {
|
||||
my $devices=schema('netdisco')->resultset('Device')->all;
|
||||
|
||||
Reference in New Issue
Block a user