diff --git a/Koha/ERM/Providers/EBSCO.pm b/Koha/ERM/Providers/EBSCO.pm index 13ec4c68ca..fbb6cfffce 100644 --- a/Koha/ERM/Providers/EBSCO.pm +++ b/Koha/ERM/Providers/EBSCO.pm @@ -4,7 +4,7 @@ use Modern::Perl; use HTTP::Request; use LWP::UserAgent; -use JSON qw( from_json decode_json ); +use JSON qw( from_json decode_json encode_json ); use List::Util qw( first ); use Koha::Exceptions; @@ -237,14 +237,16 @@ sub build_query { } sub request { - my ( $self, $method, $url, $params ) = @_; + my ( $self, $method, $url, $params, $payload ) = @_; $url = $self->build_query($url, $params) if $params; - warn $url; my $config = $self->config; my $base_url = 'https://api.ebsco.io/rm/rmaccounts/' . $config->{custid}; - my $request = HTTP::Request->new( $method => $base_url . $url); + my $request = HTTP::Request->new( + $method => $base_url . $url, + undef, ( $payload ? encode_json($payload) : undef ) + ); $request->header( 'x-api-key' => $config->{api_key} ); my $ua = LWP::UserAgent->new; my $response = $ua->simple_request($request); @@ -271,7 +273,10 @@ sub request { die sprintf "ERROR requesting EBSCO API\n%s\ncode %s: %s\n", $url, $response->code, $message; } + } elsif ( $response->code == 204 ) { # No content + return } + return decode_json( $response->decoded_content ); } diff --git a/Koha/REST/V1/ERM/EHoldings/Resources.pm b/Koha/REST/V1/ERM/EHoldings/Resources.pm index 707b10f7a9..76d6614527 100644 --- a/Koha/REST/V1/ERM/EHoldings/Resources.pm +++ b/Koha/REST/V1/ERM/EHoldings/Resources.pm @@ -61,4 +61,21 @@ sub get { } } +=head3 edit + +Controller function that handles editing a single Koha::ERM::EHoldings::Resource object + +=cut + +sub edit { + my $c = shift->openapi->valid_input or return; + + my $provider = $c->validation->param('provider'); + if ( $provider eq 'ebsco' ) { + return Koha::REST::V1::ERM::EHoldings::Resources::EBSCO::edit($c); + } else { + die "invalid action"; + } +} + 1; diff --git a/Koha/REST/V1/ERM/EHoldings/Resources/EBSCO.pm b/Koha/REST/V1/ERM/EHoldings/Resources/EBSCO.pm index c378ddb69a..6afe15d0f2 100644 --- a/Koha/REST/V1/ERM/EHoldings/Resources/EBSCO.pm +++ b/Koha/REST/V1/ERM/EHoldings/Resources/EBSCO.pm @@ -173,4 +173,74 @@ sub get { }; } +=head3 edit + +=cut + +sub edit { + my $c = shift->openapi->valid_input or return; + + return try { + my $body = $c->validation->param('body'); + my $is_selected = $body->{is_selected}; + my ( $vendor_id, $package_id, $resource_id ) = split '-', + $c->validation->param('resource_id'); + + my $ebsco = Koha::ERM::Providers::EBSCO->new; + my $t = try { + return $ebsco->request( GET => '/vendors/' + . $vendor_id + . '/packages/' + . $package_id + . '/titles/' + . $resource_id ); + + } + catch { + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) { + return $c->render( + status => 404, + openapi => { error => $_->error } + ); + + } + } + + $c->unhandled_exception($_); + }; + + unless ($t) { + return $c->render( + status => 404, + openapi => { error => "Resource not found" } + ); + } + + $ebsco->request( + PUT => '/vendors/' + . $vendor_id + . '/packages/' + . $package_id + . '/titles/' + . $resource_id, + undef, + { + isSelected => $is_selected, + titleName => $t->{titleName}, + pubType => $t->{pubType} + } + ); + + return $c->render( + status => 200, + openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated + ); + } + catch { + $c->unhandled_exception($_); + }; + +} + 1; diff --git a/api/v1/swagger/paths/erm_eholdings_resources.yaml b/api/v1/swagger/paths/erm_eholdings_resources.yaml index 1d299f9f12..676c4e0931 100644 --- a/api/v1/swagger/paths/erm_eholdings_resources.yaml +++ b/api/v1/swagger/paths/erm_eholdings_resources.yaml @@ -129,3 +129,64 @@ x-koha-embed: - resources - resources.package + patch: + x-mojo-to: ERM::EHoldings::Resources#edit + operationId: editErmEHoldingsResources + tags: + - eholdings + summary: Edit a resource + produces: + - application/json + parameters: + - description: Provider name + in: path + name: provider + required: true + type: string + - description: A JSON object containing the fields to edit + in: body + name: body + required: true + schema: + type: object + properties: + is_selected: + description: Add or remove this resource from remote holdings + type: boolean + additionalProperties: false + - $ref: "../swagger.yaml#/parameters/eholdings_resource_id_pp" + responses: + 200: + description: What has been modified + schema: + type: object + properties: + is_selected: + description: Add or remove this resource from remote holdings + type: boolean + additionalProperties: false + 401: + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + 403: + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + 404: + description: Ressource not found + schema: + $ref: "../swagger.yaml#/definitions/error" + 500: + description: |- + Internal server error. Possible `error_code` attribute values: + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + 503: + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + erm: 1 diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOResourcesShow.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOResourcesShow.vue index 83d1554413..e71cb28cb5 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOResourcesShow.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOResourcesShow.vue @@ -3,6 +3,23 @@