From bc926d6ae90323facd4ae3ccd591c8e38dbfbef9 Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Thu, 27 Sep 2018 10:39:06 +0200 Subject: [PATCH] TT#40453 Remove obsolete code from /api/callrecordings/ Change-Id: I4eb14b3f33abe63d8d26ad4513514d1b196661f6 --- .../Panel/Controller/API/CallRecordings.pm | 84 ++----------------- .../Controller/API/CallRecordingsItem.pm | 71 ++-------------- lib/NGCP/Panel/Role/API/CallRecordings.pm | 65 +++++++------- 3 files changed, 42 insertions(+), 178 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/CallRecordings.pm b/lib/NGCP/Panel/Controller/API/CallRecordings.pm index cf29f45566..c73947df10 100644 --- a/lib/NGCP/Panel/Controller/API/CallRecordings.pm +++ b/lib/NGCP/Panel/Controller/API/CallRecordings.pm @@ -3,13 +3,11 @@ use NGCP::Panel::Utils::Generic qw(:all); use Sipwise::Base; -use boolean qw(true); -use Data::HAL qw(); -use Data::HAL::Link qw(); -use HTTP::Headers qw(); -use HTTP::Status qw(:constants); +use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::CallRecordings/; -use DateTime::TimeZone; +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller subscriberadmin subscriber/], +}); sub allowed_methods{ return [qw/GET OPTIONS HEAD/]; @@ -37,13 +35,7 @@ sub query_params { { param => 'status', description => 'Filter for callrecordings with a specific status', - query => { - first => sub { - my $q = shift; - { 'status' => $q }; - }, - second => sub {}, - }, + query_type => 'string_eq', }, { # we handle that separately/manually in the role @@ -58,72 +50,6 @@ sub query_params { ]; } -use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::CallRecordings/; - -sub resource_name{ - return 'callrecordings'; -} - -sub dispatch_path{ - return '/api/callrecordings/'; -} - -sub relation{ - return 'http://purl.org/sipwise/ngcp-api/#rel-callrecordings'; -} - -__PACKAGE__->set_config({ - allowed_roles => [qw/admin reseller subscriberadmin subscriber/], -}); - -sub GET :Allow { - my ($self, $c) = @_; - my $page = $c->request->params->{page} // 1; - my $rows = $c->request->params->{rows} // 10; - { - if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone"); - return; - } - my $callrecordings = $self->item_rs($c); - (my $total_count, $callrecordings) = $self->paginate_order_collection($c, $callrecordings); - my (@embedded, @links); - my $form = $self->get_form($c); - for my $domain ($callrecordings->all) { - push @embedded, $self->hal_from_item($c, $domain, $form); - push @links, Data::HAL::Link->new( - relation => 'ngcp:'.$self->resource_name, - href => sprintf('/%s%d', $c->request->path, $domain->id), - ); - } - push @links, - Data::HAL::Link->new( - relation => 'curies', - href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', - name => 'ngcp', - templated => true, - ), - Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - $self->collection_nav_links($c, $page, $rows, $total_count, $c->request->path, $c->request->query_params); - - my $hal = Data::HAL->new( - embedded => [@embedded], - links => [@links], - ); - $hal->resource({ - total_count => $total_count, - }); - my $rname = $self->resource_name; - - my $response = HTTP::Response->new(HTTP_OK, undef, - HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); - $c->response->headers($response->headers); - $c->response->body($response->content); - return; - } - return; -} - 1; # vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/CallRecordingsItem.pm b/lib/NGCP/Panel/Controller/API/CallRecordingsItem.pm index ca4591388a..5a21c8cc99 100644 --- a/lib/NGCP/Panel/Controller/API/CallRecordingsItem.pm +++ b/lib/NGCP/Panel/Controller/API/CallRecordingsItem.pm @@ -3,82 +3,21 @@ use NGCP::Panel::Utils::Generic qw(:all); use Sipwise::Base; -use HTTP::Headers qw(); -use HTTP::Status qw(:constants); - -use NGCP::Panel::Utils::ValidateJSON qw(); -use NGCP::Panel::Utils::Subscriber; -require Catalyst::ActionRole::ACL; -require NGCP::Panel::Role::HTTPMethods; -require Catalyst::ActionRole::RequireSSL; - -sub allowed_methods{ - return [qw/GET OPTIONS HEAD DELETE/]; -} - use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::CallRecordings/; -sub resource_name{ - return 'callrecordings'; -} - -sub dispatch_path{ - return '/api/callrecordings/'; -} - -sub relation{ - return 'http://purl.org/sipwise/ngcp-api/#rel-callrecordings'; -} use NGCP::Panel::Utils::Subscriber; __PACKAGE__->set_config({ allowed_roles => [qw/admin reseller subscriberadmin subscriber/], }); -sub GET :Allow { - my ($self, $c, $id) = @_; - { - last unless $self->valid_id($c, $id); - my $item = $self->item_by_id($c, $id); - last unless $self->resource_exists($c, callrecording => $item); - - my $hal = $self->hal_from_item($c, $item); - - my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( - (map { # XXX Data::HAL must be able to generate links with multiple relations - s|rel="(http://purl.org/sipwise/ngcp-api/#rel-\w+)"|rel="item $1"|r =~ - s/rel=self/rel="item self"/r; - } $hal->http_headers), - ), $hal->as_json); - $c->response->headers($response->headers); - $c->response->body($response->content); - return; - } - return; +sub allowed_methods{ + return [qw/GET OPTIONS HEAD DELETE/]; } -sub DELETE :Allow { - my ($self, $c, $id) = @_; - - my $guard = $c->model('DB')->txn_scope_guard; - { - my $item = $self->item_by_id($c, $id); - last unless $self->resource_exists($c, callrecording => $item); - - try { - NGCP::Panel::Utils::Subscriber::delete_callrecording( c => $c, recording => $item ); - } catch($e) { - $c->log->error("Failed to delete recording: $e"); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to delete recording."); - last; - } - - - $guard->commit; - - $c->response->status(HTTP_NO_CONTENT); - $c->response->body(q()); - } +sub delete_item { + my ($self, $c, $item) = @_; + NGCP::Panel::Utils::Subscriber::delete_callrecording( c => $c, recording => $item ); return; } diff --git a/lib/NGCP/Panel/Role/API/CallRecordings.pm b/lib/NGCP/Panel/Role/API/CallRecordings.pm index 37538cb390..3b07c465d1 100644 --- a/lib/NGCP/Panel/Role/API/CallRecordings.pm +++ b/lib/NGCP/Panel/Role/API/CallRecordings.pm @@ -11,6 +11,32 @@ use Data::HAL::Link qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::Subscriber; +sub item_name { + return 'callrecording'; +} + +sub resource_name { + return 'callrecordings'; +} + +sub get_form { + my ($self, $c) = @_; + return NGCP::Panel::Form::get("NGCP::Panel::Form::CallRecording::Recording", $c); +} + +#Todo: maybe put it into Entities as common checking for all collections? +sub validate_request { + my($self, $c) = @_; + my $method = uc($c->request->method); + if ($method eq 'GET') { + if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone"); + return; + } + } + return 1; +} + sub _item_rs { my ($self, $c) = @_; @@ -68,13 +94,8 @@ sub _item_rs { return $item_rs; } -sub get_form { - my ($self, $c) = @_; - return NGCP::Panel::Form::get("NGCP::Panel::Form::CallRecording::Recording", $c); -} - -sub hal_from_item { - my ($self, $c, $item, $form) = @_; +sub hal_links { + my($self, $c, $item, $resource, $form) = @_; my $res_rs = $item->recording_metakeys->search({ key => 'uuid' @@ -83,26 +104,10 @@ sub hal_from_item { uuid => { -in => $res_rs->get_column('value')->as_query } })->get_column('id')->all; - my $hal = Data::HAL->new( - links => [ - Data::HAL::Link->new( - relation => 'curies', - href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', - name => 'ngcp', - templated => true, - ), - Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)), - Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), - (map { Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $_)) } @sub_ids), - Data::HAL::Link->new(relation => 'ngcp:callrecordingstreams', href => sprintf("/api/callrecordingstreams/?recording_id=%d", $item->id)), - ], - relation => 'ngcp:'.$self->resource_name, - ); - - my $resource = $self->resource_from_item($c, $item, $form); - $hal->resource($resource); - return $hal; + return [ + (map { Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $_)) } @sub_ids), + Data::HAL::Link->new(relation => 'ngcp:callrecordingstreams', href => sprintf("/api/callrecordingstreams/?recording_id=%d", $item->id)), + ]; } sub resource_from_item { @@ -145,11 +150,5 @@ sub resource_from_item { return \%resource; } -sub item_by_id { - my ($self, $c, $id) = @_; - my $item_rs = $self->item_rs($c); - return $item_rs->find($id); -} - 1; # vim: set tabstop=4 expandtab: