tidy util code
This commit is contained in:
@@ -15,30 +15,35 @@ __PACKAGE__->add_columns(
|
||||
"mac",
|
||||
{ data_type => "macaddr", is_nullable => 0,
|
||||
extra => { descr => 'MAC address' } },
|
||||
|
||||
"ip",
|
||||
{ data_type => "inet", is_nullable => 0,
|
||||
extra => { descr => 'IP address' } },
|
||||
|
||||
"dns",
|
||||
{ data_type => "text", is_nullable => 1,
|
||||
extra => { descr => 'FQDN of the node' } },
|
||||
|
||||
"active",
|
||||
{ data_type => "boolean", is_nullable => 1,
|
||||
extra => { descr => 'Whether the entry is still "fresh"' } },
|
||||
|
||||
"time_first",
|
||||
{
|
||||
data_type => "timestamp",
|
||||
default_value => \"current_timestamp",
|
||||
is_nullable => 1,
|
||||
original => { default_value => \"now()" },
|
||||
extra => { hide_from_api => 1 },
|
||||
extra => { descr => 'When first seen on the network' },
|
||||
},
|
||||
|
||||
"time_last",
|
||||
{
|
||||
data_type => "timestamp",
|
||||
default_value => \"current_timestamp",
|
||||
is_nullable => 1,
|
||||
original => { default_value => \"now()" },
|
||||
extra => { hide_from_api => 1 },
|
||||
extra => { descr => 'When last seen on the network' },
|
||||
},
|
||||
);
|
||||
__PACKAGE__->set_primary_key("mac", "ip");
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package App::Netdisco::Util::API;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Dancer ':syntax';
|
||||
use Dancer::Plugin::DBIC 'schema';
|
||||
|
||||
@@ -12,8 +10,6 @@ our @EXPORT = ();
|
||||
our @EXPORT_OK = qw/
|
||||
resultsource_to_openapi_params
|
||||
parse_search_params
|
||||
format_data
|
||||
format_error
|
||||
/;
|
||||
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
||||
|
||||
@@ -26,13 +22,15 @@ sub resultsource_to_openapi_params {
|
||||
|
||||
foreach my $col ($rs->primary_columns,
|
||||
(singleton ($rs->primary_columns, keys %{ $columns }))) {
|
||||
|
||||
my $data = $columns->{$col};
|
||||
next if $data->{extra}->{hide_from_api};
|
||||
|
||||
push @params, (
|
||||
$col => {
|
||||
description => $data->{extra}->{descr},
|
||||
type => ($data->{data_type} =~ m/int/ ? 'integer'
|
||||
: $data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
|
||||
type => ($data->{data_type} =~ m/int/ ? 'integer' :
|
||||
$data->{data_type} eq 'boolean' ? 'boolean' : 'string'),
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -41,62 +39,26 @@ sub resultsource_to_openapi_params {
|
||||
}
|
||||
|
||||
sub parse_search_params {
|
||||
my $params = shift;
|
||||
my $search = {};
|
||||
my $partial = $params->{partial} || false;
|
||||
my $sourcename = shift or return {};
|
||||
my $params = shift or return {};
|
||||
my @pspec = resultsource_to_openapi_params($sourcename) or return {};
|
||||
|
||||
foreach my $param (keys %{$params}) {
|
||||
if ($param ne 'return_url' and $param ne 'partial') {
|
||||
if ($partial eq 'true') {
|
||||
$search->{"text(".$param.")"} = { -ilike => '%'.$params->{$param}.'%'};
|
||||
}
|
||||
else {
|
||||
$search->{$param} = $params->{$param};
|
||||
}
|
||||
}
|
||||
}
|
||||
my $partial = $params->{partial} || false;
|
||||
my $search = {};
|
||||
|
||||
return $search;
|
||||
}
|
||||
foreach my $param (@pspec) {
|
||||
next unless exists $params->{$param};
|
||||
|
||||
sub format_data {
|
||||
my $items = shift;
|
||||
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};
|
||||
if ($partial) {
|
||||
$search->{'text('. quotemeta($param) .')'}
|
||||
= { -ilike => '%'. $params->{$param} .'%'};
|
||||
}
|
||||
else {
|
||||
$results->{data} = $items;
|
||||
$search->{$param} = $params->{$param};
|
||||
}
|
||||
}
|
||||
|
||||
header('Content-Type' => 'application/json');
|
||||
return to_json $results;
|
||||
};
|
||||
|
||||
sub format_error {
|
||||
my $status = shift;
|
||||
my $message = shift;
|
||||
|
||||
status $status;
|
||||
header('Content-Type' => 'application/json');
|
||||
return to_json { error => $message };
|
||||
return $search;
|
||||
}
|
||||
|
||||
true;
|
||||
|
||||
Reference in New Issue
Block a user