Bug 23805: Update 'Pay' to 'PAYMENT' for consistency
[koha.git] / Koha / REST / V1 / Library.pm
1 package Koha::REST::V1::Library;
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 use Koha::Libraries;
22
23 use Scalar::Util qw( blessed );
24
25 use Try::Tiny;
26
27 =head1 NAME
28
29 Koha::REST::V1::Library - Koha REST API for handling libraries (V1)
30
31 =head1 API
32
33 =head2 Methods
34
35 =cut
36
37 =head3 list
38
39 Controller function that handles listing Koha::Library objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
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 );
50     }
51     catch {
52         unless ( blessed $_ && $_->can('rethrow') ) {
53             return $c->render(
54                 status  => 500,
55                 openapi => { error => "Something went wrong, check Koha logs for details." }
56             );
57         }
58         return $c->render(
59             status  => 500,
60             openapi => { error => "$_" }
61         );
62     };
63 }
64
65 =head3 get
66
67 Controller function that handles retrieving a single Koha::Library
68
69 =cut
70
71 sub get {
72     my $c = shift->openapi->valid_input or return;
73
74     my $library_id = $c->validation->param('library_id');
75     my $library = Koha::Libraries->find( $library_id );
76
77     unless ($library) {
78         return $c->render( status  => 404,
79                            openapi => { error => "Library not found" } );
80     }
81
82     return $c->render(
83         status  => 200,
84         openapi => $library->to_api
85     );
86 }
87
88 =head3 add
89
90 Controller function that handles adding a new Koha::Library object
91
92 =cut
93
94 sub add {
95     my $c = shift->openapi->valid_input or return;
96
97     return try {
98         my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
99         $library->store;
100         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
101
102         return $c->render(
103             status  => 201,
104             openapi => $library->to_api
105         );
106     }
107     catch {
108         unless ( blessed $_ && $_->can('rethrow') ) {
109             return $c->render(
110                 status  => 500,
111                 openapi => { error => "Something went wrong, check Koha logs for details." }
112             );
113         }
114         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
115             return $c->render(
116                 status  => 409,
117                 openapi => { error => $_->error, conflict => $_->duplicate_id }
118             );
119         }
120         else {
121             return $c->render(
122                 status  => 500,
123                 openapi => { error => "$_" }
124             );
125         }
126     };
127 }
128
129 =head3 update
130
131 Controller function that handles updating a Koha::Library object
132
133 =cut
134
135 sub update {
136     my $c = shift->openapi->valid_input or return;
137
138     my $library = Koha::Libraries->find( $c->validation->param('library_id') );
139
140     if ( not defined $library ) {
141         return $c->render(
142             status  => 404,
143             openapi => { error => "Library not found" }
144         );
145     }
146
147     return try {
148         my $params = $c->req->json;
149         $library->set( _to_model($params) );
150         $library->store();
151         return $c->render(
152             status  => 200,
153             openapi => $library->to_api
154         );
155     }
156     catch {
157         unless ( blessed $_ && $_->can('rethrow') ) {
158             return $c->render(
159                 status  => 500,
160                 openapi => { error => "Something went wrong, check Koha logs for details." }
161             );
162         }
163
164         return $c->render(
165             status  => 500,
166             openapi => { error => "$_" }
167         );
168     };
169 }
170
171 =head3 delete
172
173 Controller function that handles deleting a Koha::Library object
174
175 =cut
176
177 sub delete {
178
179     my $c = shift->openapi->valid_input or return;
180
181     my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
182
183     if ( not defined $library ) {
184         return $c->render( status => 404, openapi => { error => "Library not found" } );
185     }
186
187     return try {
188         $library->delete;
189         return $c->render( status => 204, openapi => '');
190     }
191     catch {
192         unless ( blessed $_ && $_->can('rethrow') ) {
193             return $c->render(
194                 status  => 500,
195                 openapi => { error => "Something went wrong, check Koha logs for details." }
196             );
197         }
198
199         return $c->render(
200             status  => 500,
201             openapi => { error => "$_" }
202         );
203     };
204 }
205
206 =head3 _to_api
207
208 Helper function that maps a hashref of Koha::Library attributes into REST api
209 attribute names.
210
211 =cut
212
213 sub _to_api {
214     my $library = shift;
215
216     # Rename attributes
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 )
221         {
222             # key /= undef
223             $library->{ $mapped_column } = delete $library->{ $column };
224         }
225         elsif (    exists $library->{ $column }
226                 && !defined $mapped_column )
227         {
228             # key == undef => to be deleted
229             delete $library->{ $column };
230         }
231     }
232
233     return $library;
234 }
235
236 =head3 _to_model
237
238 Helper function that maps REST api objects into Koha::Library
239 attribute names.
240
241 =cut
242
243 sub _to_model {
244     my $library = shift;
245
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 )
250         {
251             # key /= undef
252             $library->{ $mapped_attribute } = delete $library->{ $attribute };
253         }
254         elsif (    exists $library->{ $attribute }
255                 && !defined $mapped_attribute )
256         {
257             # key == undef => to be deleted
258             delete $library->{ $attribute };
259         }
260     }
261
262     if ( exists $library->{pickup_location} ) {
263         $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
264     }
265
266     return $library;
267 }
268
269
270 =head2 Global variables
271
272 =head3 $to_api_mapping
273
274 =cut
275
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',
287     branchfax        => 'fax',
288     branchemail      => 'email',
289     branchreplyto    => 'reply_to_email',
290     branchreturnpath => 'return_path_email',
291     branchurl        => 'url',
292     issuing          => undef,
293     branchip         => 'ip',
294     branchprinter    => undef,
295     branchnotes      => 'notes',
296     marcorgcode      => 'marc_org_code',
297 };
298
299 =head3 $to_model_mapping
300
301 =cut
302
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',
314     fax               => 'branchfax',
315     email             => 'branchemail',
316     reply_to_email    => 'branchreplyto',
317     return_path_email => 'branchreturnpath',
318     url               => 'branchurl',
319     ip                => 'branchip',
320     notes             => 'branchnotes',
321     marc_org_code     => 'marcorgcode',
322 };
323
324 1;