1 package Koha::REST::V1::ERM::EHoldings::Resources::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 );
37 my $c = shift->openapi->valid_input or return;
41 my $args = $c->validation->output;
42 my $ebsco = Koha::ERM::Providers::EBSCO->new;
44 # FIXME Do we need more validation here? Don't think so we have the API specs.
45 my ( $vendor_id, $package_id ) = split '-',
46 $c->validation->param('package_id') || q{};
47 my $title_id = $c->validation->param('title_id') || q{};
51 ? sprintf '/titles/%s', $title_id
52 : sprintf '/vendors/%s/packages/%s/titles', $vendor_id, $package_id;
55 '?orderby=titlename&offset=1&count=1&searchfield=titlename';
59 $ebsco->request( GET => $url . $params );
63 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
66 openapi => { error => $_->error }
72 $c->unhandled_exception($_);
75 my $base_total = $result->{totalResults};
77 my ( $per_page, $page ) = $ebsco->build_query_pagination($args);
79 my ( $search, $content_type, $selection_type );
80 my $additional_params = $ebsco->build_additional_params( $c->req->params->to_hash );
81 my $searchfield = 'titlename';
84 sprintf '?orderby=titlename&offset=%s&count=%s&searchfield=%s',
85 $page, $per_page, $searchfield;
87 $result = $ebsco->request(
88 GET => $url . $params,
93 for my $t ( @{ $result->{titles} } ) {
95 $t->{customerResourcesList}->[0]; # FIXME What about the others?
97 my $resource = $ebsco->build_resource($r);
99 $resource = $ebsco->embed( $resource, $t,
100 $c->req->headers->header('x-koha-embed') );
102 push @resources, $resource;
104 my $total = $result->{totalResults};
105 $total = 10000 if $total > 10000;
106 $c->add_pagination_headers(
108 base_total => $base_total,
113 return $c->render( status => 200, openapi => \@resources );
116 $c->unhandled_exception($_);
125 my $c = shift->openapi->valid_input or return;
128 my ( $vendor_id, $package_id, $resource_id ) = split '-',
129 $c->validation->param('resource_id');
130 my $ebsco = Koha::ERM::Providers::EBSCO->new;
132 return $ebsco->request( GET => '/vendors/'
142 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
145 openapi => { error => $_->error }
151 $c->unhandled_exception($_);
157 openapi => { error => "Resource not found" }
161 my $r = $t->{customerResourcesList}->[0]; # FIXME What about the others?
162 my $resource = $ebsco->build_resource($r);
164 $resource = $ebsco->embed( $resource, {%$t, %$r}, $c->req->headers->header('x-koha-embed') );
168 openapi => $resource,
172 $c->unhandled_exception($_);
181 my $c = shift->openapi->valid_input or return;
184 my $body = $c->validation->param('body');
185 my $is_selected = $body->{is_selected};
186 my ( $vendor_id, $package_id, $resource_id ) = split '-',
187 $c->validation->param('resource_id');
189 my $ebsco = Koha::ERM::Providers::EBSCO->new;
191 return $ebsco->request( GET => '/vendors/'
201 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
204 openapi => { error => $_->error }
210 $c->unhandled_exception($_);
216 openapi => { error => "Resource not found" }
229 isSelected => $is_selected,
230 titleName => $t->{titleName},
231 pubType => $t->{pubType}
237 openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated
241 $c->unhandled_exception($_);