diff --git a/lib/NGCP/Panel/Controller/API/Customers.pm b/lib/NGCP/Panel/Controller/API/Customers.pm index 85d02459dc..507cc03646 100644 --- a/lib/NGCP/Panel/Controller/API/Customers.pm +++ b/lib/NGCP/Panel/Controller/API/Customers.pm @@ -20,7 +20,7 @@ class_has 'api_description' => ( is => 'ro', isa => 'Str', default => - 'Defines a billing container for end customers. Customers usually have one or more Subscribers. A Billing Profile is assigned to a customer, and it has Contract Balances indicating the saldo of the customer for current and past billing intervals.' + 'Defines a billing container for end customers. Customers usually have one or more Subscribers. A Billing Profile is assigned to a customer, and it has Contract Balances indicating the saldo of the customer for current and past billing intervals.', ); class_has 'query_params' => ( @@ -80,7 +80,7 @@ __PACKAGE__->config( Does => [qw(ACL CheckTrailingSlash RequireSSL)], Method => $_, Path => __PACKAGE__->dispatch_path, - } } @{ __PACKAGE__->allowed_methods } + } } @{ __PACKAGE__->allowed_methods }, }, action_roles => [qw(HTTPMethods)], ); @@ -90,6 +90,7 @@ sub auto :Private { $self->set_body($c); $self->log_request($c); + return 1; } sub GET :Allow { @@ -115,15 +116,9 @@ sub GET :Allow { name => 'ngcp', templated => true, ), - Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s', $c->request->path, $page, $rows)); + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'); - if(($total_count / $rows) > $page ) { - push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows)); - } - if($page > 1) { - push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows)); - } + push @links, $self->collection_nav_links($page, $rows, $total_count, $c->request->path, $c->request->query_params); my $hal = Data::HAL->new( embedded => [@embedded], @@ -272,6 +267,7 @@ sub end : Private { my ($self, $c) = @_; $self->log_response($c); + return; } # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index d324028251..0316e475de 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -409,6 +409,25 @@ sub paginate_order_collection { return ($total_count, $item_rs); } +sub collection_nav_links { + my ($self, $page, $rows, $total_count, $path, $params) = @_; + + $params = { %{ $params } }; #copy + delete @{$params}{'page', 'rows'}; + my $rest_params = join( '&', map {"$_=".$params->{$_}} keys %{$params}); + $rest_params = $rest_params ? "&$rest_params" : ""; + + my @links = (Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s%s', $path, $page, $rows, $rest_params))); + + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d%s', $path, $page + 1, $rows, $rest_params)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d%s', $path, $page - 1, $rows, $rest_params)); + } + return @links; +} + sub apply_patch { my ($self, $c, $entity, $json) = @_; my $patch = JSON::decode_json($json); diff --git a/t/api-customers.t b/t/api-customers.t index 823ccde127..3312df4c62 100644 --- a/t/api-customers.t +++ b/t/api-customers.t @@ -192,7 +192,7 @@ my @allcustomers = (); ok($err->{message} =~ /field='max_subscribers'/, "check error message in body"); # iterate over customers collection to check next/prev links and status - my $nexturi = $uri.'/api/customers/?page=1&rows=5'; + my $nexturi = $uri.'/api/customers/?page=1&rows=5&status=active'; do { $res = $ua->get($nexturi); is($res->code, 200, "fetch contacts page"); @@ -408,18 +408,6 @@ my @allcustomers = (); $pc = JSON::from_json($res->decoded_content); is($pc->{status}, "terminated", "check termination status of customer"); } - - # check if we can still get the terminated customer - $req = HTTP::Request->new('GET', $uri.'/'.$pc->{_links}->{self}->{href}); - $res = $ua->request($req); - is($res->code, 404, "check fetching of terminated customer"); - - # check if deletion of contact is now ok - # TODO: are we supposed to be able to delete a contact for a terminated - # customer? there are still DB contstraints in the way! - #$req = HTTP::Request->new('DELETE', $uri.'/'.$custcontact->{_links}->{self}->{href}); - #$res = $ua->request($req); - #is($res->code, 204, "check deletion of unused contact"); } done_testing;