|
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 = Koha::Biblio::Volumes->find($c->validation->param('volume_id')); |
| 82 |
my $embed = $c->stash('koha.embed'); |
| 83 |
|
| 84 |
if ( $volume ) { |
| 85 |
return $c->render( |
| 86 |
status => 200, |
| 87 |
openapi => $volume->to_api({ embed => $embed }) |
| 88 |
); |
| 89 |
} |
| 90 |
else { |
| 91 |
return $c->render( |
| 92 |
status => 404, |
| 93 |
openapi => { |
| 94 |
error => 'Volume not found' |
| 95 |
} |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
catch { |
| 100 |
return $c->render( |
| 101 |
status => 500, |
| 102 |
openapi => { |
| 103 |
error => "Something went wrong, check the logs ($_)." |
| 104 |
} |
| 105 |
); |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
=head3 add |
| 110 |
|
| 111 |
Controller function to handle adding a Koha::Biblio::Volume object |
| 112 |
|
| 113 |
=cut |
| 114 |
|
| 115 |
sub add { |
| 116 |
my $c = shift->openapi->valid_input or return; |
| 117 |
|
| 118 |
return try { |
| 119 |
my $volume_data = $c->validation->param('body'); |
| 120 |
# biblio_id comes from the path |
| 121 |
$volume_data->{biblio_id} = $c->validation->param('biblio_id'); |
| 122 |
|
| 123 |
my $volume = Koha::Biblio::Volume->new_from_api($volume_data); |
| 124 |
$volume->store->discard_changes(); |
| 125 |
|
| 126 |
$c->res->headers->location( $c->req->url->to_string . '/' . $volume->id ); |
| 127 |
|
| 128 |
return $c->render( |
| 129 |
status => 201, |
| 130 |
openapi => $volume->to_api |
| 131 |
); |
| 132 |
} |
| 133 |
catch { |
| 134 |
if ( blessed($_) ) { |
| 135 |
my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping; |
| 136 |
|
| 137 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
| 138 |
return $c->render( |
| 139 |
status => 409, |
| 140 |
openapi => { |
| 141 |
error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" |
| 142 |
} |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $c->render( |
| 148 |
status => 500, |
| 149 |
openapi => { |
| 150 |
error => "Unhandled exception ($_)" |
| 151 |
} |
| 152 |
); |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
=head3 update |
| 157 |
|
| 158 |
Controller function to handle updating a Koha::Biblio::Volume object |
| 159 |
|
| 160 |
=cut |
| 161 |
|
| 162 |
sub update { |
| 163 |
my $c = shift->openapi->valid_input or return; |
| 164 |
|
| 165 |
return try { |
| 166 |
my $volume_id = $c->validation->param('volume_id'); |
| 167 |
my $volume = Koha::Biblio::Volumes->find( $volume_id ); |
| 168 |
|
| 169 |
unless ( $volume ) { |
| 170 |
return $c->render( |
| 171 |
status => 404, |
| 172 |
openapi => { |
| 173 |
error => 'Volume not found' |
| 174 |
} |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
my $volume_data = $c->validation->param('body'); |
| 179 |
$volume->set_from_api( $volume_data )->store->discard_changes(); |
| 180 |
|
| 181 |
return $c->render( |
| 182 |
status => 200, |
| 183 |
openapi => $volume->to_api |
| 184 |
); |
| 185 |
} |
| 186 |
catch { |
| 187 |
if ( blessed($_) ) { |
| 188 |
my $to_api_mapping = Koha::Biblio::Volume->new->to_api_mapping; |
| 189 |
|
| 190 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
| 191 |
return $c->render( |
| 192 |
status => 409, |
| 193 |
openapi => { |
| 194 |
error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" |
| 195 |
} |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return $c->render( |
| 201 |
status => 500, |
| 202 |
openapi => { |
| 203 |
error => "Unhandled exception ($_)" |
| 204 |
} |
| 205 |
); |
| 206 |
}; |
| 207 |
} |
| 208 |
|
| 209 |
=head3 delete |
| 210 |
|
| 211 |
Controller function that handles deleting a Koha::Biblio::Volume object |
| 212 |
|
| 213 |
=cut |
| 214 |
|
| 215 |
sub delete { |
| 216 |
|
| 217 |
my $c = shift->openapi->valid_input or return; |
| 218 |
|
| 219 |
my $volume = Koha::Biblio::Volumes->find( $c->validation->param( 'volume_id' ) ); |
| 220 |
|
| 221 |
if ( not defined $volume ) { |
| 222 |
return $c->render( status => 404, openapi => { error => "Volume not found" } ); |
| 223 |
} |
| 224 |
|
| 225 |
return try { |
| 226 |
$volume->delete; |
| 227 |
return $c->render( status => 204, openapi => ''); |
| 228 |
} |
| 229 |
catch { |
| 230 |
unless ( blessed $_ && $_->can('rethrow') ) { |
| 231 |
return $c->render( |
| 232 |
status => 500, |
| 233 |
openapi => { error => "Something went wrong, check Koha logs for details." } |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
return $c->render( |
| 238 |
status => 500, |
| 239 |
openapi => { error => "$_" } |
| 240 |
); |
| 241 |
}; |
| 242 |
} |
| 243 |
|
| 244 |
1; |