1 package Koha::REST::V1::ERM::EHoldings::Packages::EBSCO;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use JSON qw( decode_json );
23 use Koha::ERM::Providers::EBSCO;
25 use Scalar::Util qw( blessed );
26 use Try::Tiny qw( catch try );
29 my $c = shift or return;
33 my $params = '?orderby=packagename&offset=1&count=1';
34 my $ebsco = Koha::ERM::Providers::EBSCO->new;
35 my $result = $ebsco->request( GET => '/packages' . $params );
36 my $base_total = $result->{totalResults};
38 my ( $per_page, $page ) = $ebsco->build_query_pagination(
40 per_page => $c->stash('koha.pagination.per_page'),
41 page => $c->stash('koha.pagination.page'),
44 my $additional_params =
45 $ebsco->build_additional_params( $c->req->params->to_hash );
47 my $orderby = $additional_params->{name} ? 'relevance' : 'packagename';
48 $params = sprintf '?orderby=%s&offset=%s&count=%s', $orderby, $page, $per_page;
50 $result = $ebsco->request(
51 GET => '/packages' . $params,
56 for my $p ( @{ $result->{packagesList} } ) {
57 my $package = $ebsco->build_package($p);
59 $ebsco->embed( $package, $p, $c->req->headers->header('x-koha-embed') );
60 push @packages, $package;
62 my $total = $result->{totalResults};
63 $total = 10000 if $total > 10000;
65 $c->add_pagination_headers(
67 base_total => $base_total,
69 per_page => $per_page,
73 return $c->render( status => 200, openapi => \@packages );
77 if ( $_->isa('Koha::Exceptions::Authorization::Unauthorized') ) {
83 message => "Check your ERMProviderEbscoApiKey/ERMProviderEbscoCustomerID system preferences."
90 $c->unhandled_exception($_);
95 my $c = shift or return;
98 my ( $vendor_id, $package_id ) = split '-',
99 $c->param('package_id');
100 my $ebsco = Koha::ERM::Providers::EBSCO->new;
101 my $p = $ebsco->request(
102 GET => '/vendors/' . $vendor_id . '/packages/' . $package_id );
106 openapi => { error => "Package not found" }
110 my $package = $ebsco->build_package($p);
113 $ebsco->embed( $package, $p,
114 $c->req->headers->header('x-koha-embed') );
122 return $c->unhandled_exception($_);
127 my $c = shift or return;
130 my $body = $c->req->json;
131 my $is_selected = $body->{is_selected};
132 my ( $vendor_id, $package_id ) = split '-',
133 $c->param('package_id');
135 my $ebsco = Koha::ERM::Providers::EBSCO->new;
138 PUT => '/vendors/' . $vendor_id . '/packages/' . $package_id,
141 isSelected => $is_selected,
147 openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated
152 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
155 openapi => { error => $_->error }
161 return $c->unhandled_exception($_);
165 return $c->unhandled_exception($_);