Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Biblios::Volumes; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
|
22 |
use Koha::Biblio::Volumes; |
23 |
|
24 |
use Scalar::Util qw(blessed); |
25 |
use Try::Tiny; |
26 |
|
27 |
=head1 NAME |
28 |
|
29 |
Koha::REST::V1::Biblios::Volumes - Koha REST API for handling volumes (V1) |
30 |
|
31 |
=head1 API |
32 |
|
33 |
=head2 Methods |
34 |
|
35 |
=cut |
36 |
|
37 |
=head3 list |
38 |
|
39 |
Controller function that handles listing Koha::Biblio::Volume objects |
40 |
|
41 |
=cut |
42 |
|
43 |
sub list { |
44 |
my $c = shift->openapi->valid_input or return; |
45 |
|
46 |
return try { |
47 |
my $volumes_set = Koha::Biblio::Volumes->new; |
48 |
my $volumes = $c->objects->search( $volumes_set ); |
49 |
return $c->render( |
50 |
status => 200, |
51 |
openapi => $volumes |
52 |
); |
53 |
} |
54 |
catch { |
55 |
unless ( blessed $_ && $_->can('rethrow') ) { |
56 |
return $c->render( |
57 |
status => 500, |
58 |
openapi => { |
59 |
error => |
60 |
"Something went wrong, check Koha logs for details." |
61 |
} |
62 |
); |
63 |
} |
64 |
return $c->render( |
65 |
status => 500, |
66 |
openapi => { error => "$_" } |
67 |
); |
68 |
}; |
69 |
} |
70 |
|
71 |
=head3 get |
72 |
|
73 |
Controller function that handles retrieving a single Koha::Biblio::Volume |
74 |
|
75 |
=cut |
76 |
|
77 |
sub get { |
78 |
my $c = shift->openapi->valid_input or return; |
79 |
|
80 |
try { |
81 |
my $volume_id = $c->validation->param('volume_id'); |
82 |
my $biblio_id = $c->validation->param('biblio_id'); |
83 |
|
84 |
my $volume = Koha::Biblio::Volumes->find( $volume_id ); |
85 |
my $embed = $c->stash('koha.embed'); |
86 |
|
87 |
if ( $volume && $volume->biblionumber eq $biblio_id ) { |
88 |
return $c->render( |
89 |
status => 200, |
90 |
openapi => $volume->to_api({ embed => $embed }) |
91 |
); |
92 |
} |
93 |
else { |
94 |
return $c->render( |
95 |
status => 404, |
96 |
openapi => { |
97 |
error => 'Volume not found' |
98 |
} |
99 |
); |
100 |
} |
101 |
} |
102 |
catch { |
103 |
return $c->render( |
104 |
status => 500, |
105 |
openapi => { |
106 |
error => "Something went wrong, check the logs ($_)." |
107 |
} |
108 |
); |
109 |
}; |
110 |
} |
111 |
|
112 |
=head3 add |
113 |
|
114 |
Controller function to handle adding a Koha::Biblio::Volume object |
115 |
|
116 |
=cut |
117 |
|
118 |
sub add { |
119 |
my $c = shift->openapi->valid_input or return; |
120 |
|
121 |
return try { |
122 |
my $volume_data = $c->validation->param('body'); |
123 |
# biblio_id comes from the path |
124 |
$volume_data->{biblio_id} = $c->validation->param('biblio_id'); |
125 |
|
126 |
my $volume = Koha::Biblio::Volume->new_from_api($volume_data); |
127 |
$volume->store->discard_changes(); |
128 |
|
129 |
$c->res->headers->location( $c->req->url->to_string . '/' . $volume->id ); |
130 |
|
131 |
return $c->render( |
132 |
status => 201, |
133 |
openapi => $volume->to_api |
134 |
); |
135 |
} |
136 |
catch { |
137 |
if ( blessed($_) ) { |
138 |
my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping; |
139 |
|
140 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
141 |
return $c->render( |
142 |
status => 409, |
143 |
openapi => { |
144 |
error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" |
145 |
} |
146 |
); |
147 |
} |
148 |
} |
149 |
|
150 |
return $c->render( |
151 |
status => 500, |
152 |
openapi => { |
153 |
error => "Unhandled exception ($_)" |
154 |
} |
155 |
); |
156 |
}; |
157 |
} |
158 |
|
159 |
=head3 update |
160 |
|
161 |
Controller function to handle updating a Koha::Biblio::Volume object |
162 |
|
163 |
=cut |
164 |
|
165 |
sub update { |
166 |
my $c = shift->openapi->valid_input or return; |
167 |
|
168 |
return try { |
169 |
my $volume_id = $c->validation->param('volume_id'); |
170 |
my $biblio_id = $c->validation->param('biblio_id'); |
171 |
|
172 |
my $volume = Koha::Biblio::Volumes->find( $volume_id ); |
173 |
|
174 |
unless ( $volume && $volume->biblionumber eq $biblio_id ) { |
175 |
return $c->render( |
176 |
status => 404, |
177 |
openapi => { |
178 |
error => 'Volume not found' |
179 |
} |
180 |
); |
181 |
} |
182 |
|
183 |
my $volume_data = $c->validation->param('body'); |
184 |
$volume->set_from_api( $volume_data )->store->discard_changes(); |
185 |
|
186 |
return $c->render( |
187 |
status => 200, |
188 |
openapi => $volume->to_api |
189 |
); |
190 |
} |
191 |
catch { |
192 |
if ( blessed($_) ) { |
193 |
my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping; |
194 |
|
195 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
196 |
return $c->render( |
197 |
status => 409, |
198 |
openapi => { |
199 |
error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" |
200 |
} |
201 |
); |
202 |
} |
203 |
} |
204 |
|
205 |
return $c->render( |
206 |
status => 500, |
207 |
openapi => { |
208 |
error => "Unhandled exception ($_)" |
209 |
} |
210 |
); |
211 |
}; |
212 |
} |
213 |
|
214 |
=head3 delete |
215 |
|
216 |
Controller function that handles deleting a Koha::Biblio::Volume object |
217 |
|
218 |
=cut |
219 |
|
220 |
sub delete { |
221 |
|
222 |
my $c = shift->openapi->valid_input or return; |
223 |
|
224 |
my $volume_id = $c->validation->param( 'volume_id' ); |
225 |
my $biblio_id = $c->validation->param( 'biblio_id' ); |
226 |
|
227 |
my $volume = Koha::Biblio::Volumes->find({ id => $volume_id, biblionumber => $biblio_id }); |
228 |
|
229 |
if ( not defined $volume ) { |
230 |
return $c->render( status => 404, openapi => { error => "Volume not found" } ); |
231 |
} |
232 |
|
233 |
return try { |
234 |
$volume->delete; |
235 |
return $c->render( status => 204, openapi => ''); |
236 |
} |
237 |
catch { |
238 |
unless ( blessed $_ && $_->can('rethrow') ) { |
239 |
return $c->render( |
240 |
status => 500, |
241 |
openapi => { error => "Something went wrong, check Koha logs for details." } |
242 |
); |
243 |
} |
244 |
|
245 |
return $c->render( |
246 |
status => 500, |
247 |
openapi => { error => "$_" } |
248 |
); |
249 |
}; |
250 |
} |
251 |
|
252 |
1; |