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" } );
82 return $c->render( status => 200, openapi => _to_api( $library->TO_JSON ) );
87 Controller function that handles adding a new Koha::Library object
92 my $c = shift->openapi->valid_input or return;
95 my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
97 $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
98 return $c->render( status => 201, openapi => _to_api( $library->TO_JSON ) );
101 unless ( blessed $_ && $_->can('rethrow') ) {
104 openapi => { error => "Something went wrong, check Koha logs for details." }
107 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
110 openapi => { error => $_->error, conflict => $_->duplicate_id }
116 openapi => { error => "$_" }
124 Controller function that handles updating a Koha::Library object
129 my $c = shift->openapi->valid_input or return;
131 my $library = Koha::Libraries->find( $c->validation->param('library_id') );
133 if ( not defined $library ) {
136 openapi => { error => "Library not found" }
141 my $params = $c->req->json;
142 $library->set( _to_model($params) );
144 return $c->render( status => 200, openapi => _to_api($library->TO_JSON) );
147 unless ( blessed $_ && $_->can('rethrow') ) {
150 openapi => { error => "Something went wrong, check Koha logs for details." }
156 openapi => { error => "$_" }
163 Controller function that handles deleting a Koha::Library object
169 my $c = shift->openapi->valid_input or return;
171 my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
173 if ( not defined $library ) {
174 return $c->render( status => 404, openapi => { error => "Library not found" } );
179 return $c->render( status => 204, openapi => '');
182 unless ( blessed $_ && $_->can('rethrow') ) {
185 openapi => { error => "Something went wrong, check Koha logs for details." }
191 openapi => { error => "$_" }
198 Helper function that maps a hashref of Koha::Library attributes into REST api
207 foreach my $column ( keys %{ $Koha::REST::V1::Library::to_api_mapping } ) {
208 my $mapped_column = $Koha::REST::V1::Library::to_api_mapping->{$column};
209 if ( exists $library->{ $column }
210 && defined $mapped_column )
213 $library->{ $mapped_column } = delete $library->{ $column };
215 elsif ( exists $library->{ $column }
216 && !defined $mapped_column )
218 # key == undef => to be deleted
219 delete $library->{ $column };
228 Helper function that maps REST api objects into Koha::Library
236 foreach my $attribute ( keys %{ $Koha::REST::V1::Library::to_model_mapping } ) {
237 my $mapped_attribute = $Koha::REST::V1::Library::to_model_mapping->{$attribute};
238 if ( exists $library->{ $attribute }
239 && defined $mapped_attribute )
242 $library->{ $mapped_attribute } = delete $library->{ $attribute };
244 elsif ( exists $library->{ $attribute }
245 && !defined $mapped_attribute )
247 # key == undef => to be deleted
248 delete $library->{ $attribute };
252 if ( exists $library->{pickup_location} ) {
253 $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
260 =head2 Global variables
262 =head3 $to_api_mapping
266 our $to_api_mapping = {
267 branchcode => 'library_id',
268 branchname => 'name',
269 branchaddress1 => 'address1',
270 branchaddress2 => 'address2',
271 branchaddress3 => 'address3',
272 branchzip => 'postal_code',
273 branchcity => 'city',
274 branchstate => 'state',
275 branchcountry => 'country',
276 branchphone => 'phone',
278 branchemail => 'email',
279 branchreplyto => 'reply_to_email',
280 branchreturnpath => 'return_path_email',
284 branchprinter => undef,
285 branchnotes => 'notes',
286 marcorgcode => 'marc_org_code',
289 =head3 $to_model_mapping
293 our $to_model_mapping = {
294 library_id => 'branchcode',
295 name => 'branchname',
296 address1 => 'branchaddress1',
297 address2 => 'branchaddress2',
298 address3 => 'branchaddress3',
299 postal_code => 'branchzip',
300 city => 'branchcity',
301 state => 'branchstate',
302 country => 'branchcountry',
303 phone => 'branchphone',
305 email => 'branchemail',
306 reply_to_email => 'branchreplyto',
307 return_path_email => 'branchreturnpath',
310 notes => 'branchnotes',
311 marc_org_code => 'marcorgcode',