1 package Koha::REST::V1::Library;
3 # This file is part of Koha.
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
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.
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.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Scalar::Util qw( blessed );
29 Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
39 Controller function that handles listing Koha::Library objects
44 my $c = shift->openapi->valid_input or return;
47 my $libraries_set = Koha::Libraries->new;
48 my $libraries = $c->objects->search( $libraries_set, \&_to_model, \&_to_api );
49 return $c->render( status => 200, openapi => $libraries );
52 unless ( blessed $_ && $_->can('rethrow') ) {
55 openapi => { error => "Something went wrong, check Koha logs for details." }
60 openapi => { error => "$_" }
67 Controller function that handles retrieving a single Koha::Library
72 my $c = shift->openapi->valid_input or return;
74 my $library_id = $c->validation->param('library_id');
75 my $library = Koha::Libraries->find( $library_id );
78 return $c->render( status => 404,
79 openapi => { error => "Library not found" } );
84 openapi => $library->to_api
90 Controller function that handles adding a new Koha::Library object
95 my $c = shift->openapi->valid_input or return;
98 my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
100 $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
104 openapi => $library->to_api
108 unless ( blessed $_ && $_->can('rethrow') ) {
111 openapi => { error => "Something went wrong, check Koha logs for details." }
114 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
117 openapi => { error => $_->error, conflict => $_->duplicate_id }
123 openapi => { error => "$_" }
131 Controller function that handles updating a Koha::Library object
136 my $c = shift->openapi->valid_input or return;
138 my $library = Koha::Libraries->find( $c->validation->param('library_id') );
140 if ( not defined $library ) {
143 openapi => { error => "Library not found" }
148 my $params = $c->req->json;
149 $library->set( _to_model($params) );
153 openapi => $library->to_api
157 unless ( blessed $_ && $_->can('rethrow') ) {
160 openapi => { error => "Something went wrong, check Koha logs for details." }
166 openapi => { error => "$_" }
173 Controller function that handles deleting a Koha::Library object
179 my $c = shift->openapi->valid_input or return;
181 my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
183 if ( not defined $library ) {
184 return $c->render( status => 404, openapi => { error => "Library not found" } );
189 return $c->render( status => 204, openapi => '');
192 unless ( blessed $_ && $_->can('rethrow') ) {
195 openapi => { error => "Something went wrong, check Koha logs for details." }
201 openapi => { error => "$_" }
208 Helper function that maps a hashref of Koha::Library attributes into REST api
217 foreach my $column ( keys %{ $Koha::REST::V1::Library::to_api_mapping } ) {
218 my $mapped_column = $Koha::REST::V1::Library::to_api_mapping->{$column};
219 if ( exists $library->{ $column }
220 && defined $mapped_column )
223 $library->{ $mapped_column } = delete $library->{ $column };
225 elsif ( exists $library->{ $column }
226 && !defined $mapped_column )
228 # key == undef => to be deleted
229 delete $library->{ $column };
238 Helper function that maps REST api objects into Koha::Library
246 foreach my $attribute ( keys %{ $Koha::REST::V1::Library::to_model_mapping } ) {
247 my $mapped_attribute = $Koha::REST::V1::Library::to_model_mapping->{$attribute};
248 if ( exists $library->{ $attribute }
249 && defined $mapped_attribute )
252 $library->{ $mapped_attribute } = delete $library->{ $attribute };
254 elsif ( exists $library->{ $attribute }
255 && !defined $mapped_attribute )
257 # key == undef => to be deleted
258 delete $library->{ $attribute };
262 if ( exists $library->{pickup_location} ) {
263 $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
270 =head2 Global variables
272 =head3 $to_api_mapping
276 our $to_api_mapping = {
277 branchcode => 'library_id',
278 branchname => 'name',
279 branchaddress1 => 'address1',
280 branchaddress2 => 'address2',
281 branchaddress3 => 'address3',
282 branchzip => 'postal_code',
283 branchcity => 'city',
284 branchstate => 'state',
285 branchcountry => 'country',
286 branchphone => 'phone',
288 branchemail => 'email',
289 branchreplyto => 'reply_to_email',
290 branchreturnpath => 'return_path_email',
294 branchprinter => undef,
295 branchnotes => 'notes',
296 marcorgcode => 'marc_org_code',
299 =head3 $to_model_mapping
303 our $to_model_mapping = {
304 library_id => 'branchcode',
305 name => 'branchname',
306 address1 => 'branchaddress1',
307 address2 => 'branchaddress2',
308 address3 => 'branchaddress3',
309 postal_code => 'branchzip',
310 city => 'branchcity',
311 state => 'branchstate',
312 country => 'branchcountry',
313 phone => 'branchphone',
315 email => 'branchemail',
316 reply_to_email => 'branchreplyto',
317 return_path_email => 'branchreturnpath',
320 notes => 'branchnotes',
321 marc_org_code => 'marcorgcode',