diff --git a/lib/App/Netdisco/Web/Plugin/API/Util.pm b/lib/App/Netdisco/Web/Plugin/API/Util.pm index 5e36a9e4..3884dec0 100644 --- a/lib/App/Netdisco/Web/Plugin/API/Util.pm +++ b/lib/App/Netdisco/Web/Plugin/API/Util.pm @@ -1,15 +1,19 @@ -package App::Netdisco::Web::Plugin::API::Util; -#Collection of API utilities +package App::Netdisco::Util::API; use Dancer ':syntax'; use base 'Exporter'; -our @EXPORT = qw/parse_search_params format_data format_error/; +our @EXPORT = qw/ + parse_search_params + format_data + format_error +/; sub parse_search_params { my $params = shift; my $search = {}; - my $partial = $params->{partial} || 0; + my $partial = $params->{partial} || 0; + foreach my $param (keys %{$params}) { if ($param ne 'return_url' and $param ne 'partial') { if ($partial == 1) { @@ -20,23 +24,28 @@ sub parse_search_params { } } } + 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/) { @@ -45,14 +54,17 @@ sub format_data { else { $results->{data} = $items; } + + header('Content-Type' => 'application/json'); return to_json $results; }; sub format_error { my $status = shift; my $message = shift; - header( 'Content-Type' => 'application/json'); + status $status; + header('Content-Type' => 'application/json'); return to_json { error => $message }; }