Bug 34339: Avoid relying on $c->validation - ERM
[koha.git] / Koha / REST / V1 / ERM / EHoldings / Packages / EBSCO.pm
1 package Koha::REST::V1::ERM::EHoldings::Packages::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 qw( catch try );
27
28 sub list {
29     my $c = shift or return;
30
31     return try {
32
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};
37
38         my ( $per_page, $page ) = $ebsco->build_query_pagination(
39             {
40                 per_page => $c->stash('koha.pagination.per_page'),
41                 page     => $c->stash('koha.pagination.page'),
42             }
43         );
44         my $additional_params =
45           $ebsco->build_additional_params( $c->req->params->to_hash );
46
47         my $orderby = $additional_params->{name} ? 'relevance' : 'packagename';
48         $params = sprintf '?orderby=%s&offset=%s&count=%s', $orderby, $page, $per_page;
49
50         $result = $ebsco->request(
51             GET => '/packages' . $params,
52             $additional_params
53         );
54
55         my @packages;
56         for my $p ( @{ $result->{packagesList} } ) {
57             my $package = $ebsco->build_package($p);
58             $package =
59               $ebsco->embed( $package, $p, $c->req->headers->header('x-koha-embed') );
60               push @packages, $package;
61         }
62         my $total = $result->{totalResults};
63         $total = 10000 if $total > 10000;
64
65         $c->add_pagination_headers(
66             {
67                 base_total   => $base_total,
68                 page         => $page,
69                 per_page     => $per_page,
70                 total        => $total,
71             }
72         );
73         return $c->render( status => 200, openapi => \@packages );
74     }
75     catch {
76         if ( blessed $_ ) {
77             if ( $_->isa('Koha::Exceptions::Authorization::Unauthorized') ) {
78                 return $c->render(
79                     status  => 401,
80                     openapi => {
81                         errors => [
82                             {
83                                 message => "Check your ERMProviderEbscoApiKey/ERMProviderEbscoCustomerID system preferences."
84                             }
85                         ]
86                     }
87                 );
88             }
89         }
90         $c->unhandled_exception($_);
91     };
92 }
93
94 sub get {
95     my $c = shift or return;
96
97     return try {
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 );
103         unless ($p) {
104             return $c->render(
105                 status  => 404,
106                 openapi => { error => "Package not found" }
107             );
108         }
109
110         my $package = $ebsco->build_package($p);
111
112         $package =
113           $ebsco->embed( $package, $p,
114             $c->req->headers->header('x-koha-embed') );
115
116           return $c->render(
117             status  => 200,
118             openapi => $package
119           );
120     }
121     catch {
122         return $c->unhandled_exception($_);
123     };
124 }
125
126 sub edit {
127     my $c = shift or return;
128
129     return try {
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');
134
135         my $ebsco = Koha::ERM::Providers::EBSCO->new;
136         my $t     = try {
137             $ebsco->request(
138                 PUT => '/vendors/' . $vendor_id . '/packages/' . $package_id,
139                 undef,
140                 {
141                     isSelected => $is_selected,
142                 }
143             );
144
145             return $c->render(
146                 status  => 200,
147                 openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated
148             );
149         }
150         catch {
151             if ( blessed $_ ) {
152                 if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) {
153                     return $c->render(
154                         status  => 404,
155                         openapi => { error => $_->error }
156                     );
157
158                 }
159             }
160
161             return $c->unhandled_exception($_);
162         };
163     }
164     catch {
165         return $c->unhandled_exception($_);
166     };
167 }
168
169 1;