Bug 32030: Add/remove resources to/from EBSCO's holdings
[koha.git] / Koha / REST / V1 / ERM / EHoldings / Resources / EBSCO.pm
1 package Koha::REST::V1::ERM::EHoldings::Resources::EBSCO;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use JSON qw( decode_json );
23 use Koha::ERM::Providers::EBSCO;
24
25 use Scalar::Util qw( blessed );
26 use Try::Tiny;
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 list
33
34 =cut
35
36 sub list {
37     my $c = shift->openapi->valid_input or return;
38
39     return try {
40
41         my $args = $c->validation->output;
42         my $ebsco      = Koha::ERM::Providers::EBSCO->new;
43
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{};
48
49         my $url =
50           $title_id
51           ? sprintf '/titles/%s', $title_id
52           : sprintf '/vendors/%s/packages/%s/titles', $vendor_id, $package_id;
53
54         my $params =
55           '?orderby=titlename&offset=1&count=1&searchfield=titlename';
56         my $result;
57         try {
58             $result =
59               $ebsco->request( GET => $url . $params );
60         }
61         catch {
62             if ( blessed $_ ) {
63                 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
64                     return $c->render(
65                         status  => 404,
66                         openapi => { error => $_->error }
67                     );
68
69                 }
70             }
71
72             $c->unhandled_exception($_);
73         };
74
75         my $base_total = $result->{totalResults};
76
77         my ( $per_page, $page ) = $ebsco->build_query_pagination($args);
78
79         my ( $search, $content_type, $selection_type );
80         my $additional_params = $ebsco->build_additional_params( $c->req->params->to_hash );
81         my $searchfield = 'titlename';
82
83         $params =
84           sprintf '?orderby=titlename&offset=%s&count=%s&searchfield=%s',
85           $page, $per_page, $searchfield;
86
87         $result = $ebsco->request(
88             GET => $url . $params,
89             $additional_params
90         );
91
92         my @resources;
93         for my $t ( @{ $result->{titles} } ) {
94             my $r =
95               $t->{customerResourcesList}->[0];   # FIXME What about the others?
96
97             my $resource = $ebsco->build_resource($r);
98
99             $resource = $ebsco->embed( $resource, $t,
100                 $c->req->headers->header('x-koha-embed') );
101
102             push @resources, $resource;
103         }
104         my $total = $result->{totalResults};
105         $total = 10000 if $total > 10000;
106         $c->add_pagination_headers(
107             {
108                 base_total => $base_total,
109                 total      => $total,
110                 params     => $args,
111             }
112         );
113         return $c->render( status => 200, openapi => \@resources );
114     }
115     catch {
116         $c->unhandled_exception($_);
117     };
118 }
119
120 =head3 get
121
122 =cut
123
124 sub get {
125     my $c = shift->openapi->valid_input or return;
126
127     return try {
128         my ( $vendor_id, $package_id, $resource_id ) = split '-',
129           $c->validation->param('resource_id');
130         my $ebsco      = Koha::ERM::Providers::EBSCO->new;
131         my $t = try {
132               return $ebsco->request( GET => '/vendors/'
133                   . $vendor_id
134                   . '/packages/'
135                   . $package_id
136                   . '/titles/'
137                   . $resource_id );
138
139         }
140         catch {
141             if ( blessed $_ ) {
142                 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
143                     return $c->render(
144                         status  => 404,
145                         openapi => { error => $_->error }
146                     );
147
148                 }
149             }
150
151             $c->unhandled_exception($_);
152         };
153
154         unless ($t) {
155             return $c->render(
156                 status  => 404,
157                 openapi => { error => "Resource not found" }
158             );
159         }
160
161         my $r = $t->{customerResourcesList}->[0]; # FIXME What about the others?
162         my $resource = $ebsco->build_resource($r);
163
164         $resource = $ebsco->embed( $resource, {%$t, %$r}, $c->req->headers->header('x-koha-embed') );
165
166         return $c->render(
167             status  => 200,
168             openapi => $resource,
169         );
170     }
171     catch {
172         $c->unhandled_exception($_);
173     };
174 }
175
176 =head3 edit
177
178 =cut
179
180 sub edit {
181     my $c = shift->openapi->valid_input or return;
182
183     return try {
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');
188
189         my $ebsco = Koha::ERM::Providers::EBSCO->new;
190         my $t     = try {
191             return $ebsco->request( GET => '/vendors/'
192                   . $vendor_id
193                   . '/packages/'
194                   . $package_id
195                   . '/titles/'
196                   . $resource_id );
197
198         }
199         catch {
200             if ( blessed $_ ) {
201                 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
202                     return $c->render(
203                         status  => 404,
204                         openapi => { error => $_->error }
205                     );
206
207                 }
208             }
209
210             $c->unhandled_exception($_);
211         };
212
213         unless ($t) {
214             return $c->render(
215                 status  => 404,
216                 openapi => { error => "Resource not found" }
217             );
218         }
219
220         $ebsco->request(
221             PUT => '/vendors/'
222               . $vendor_id
223               . '/packages/'
224               . $package_id
225               . '/titles/'
226               . $resource_id,
227             undef,
228             {
229                 isSelected => $is_selected,
230                 titleName  => $t->{titleName},
231                 pubType    => $t->{pubType}
232             }
233         );
234
235         return $c->render(
236             status  => 200,
237             openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated
238         );
239     }
240     catch {
241         $c->unhandled_exception($_);
242     };
243
244 }
245
246 1;