Bug 20581: API provide status_alias embed
[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( status => 200, openapi => _to_api( $library->TO_JSON ) );
83 }
84
85 =head3 add
86
87 Controller function that handles adding a new Koha::Library object
88
89 =cut
90
91 sub add {
92     my $c = shift->openapi->valid_input or return;
93
94     return try {
95         my $library = Koha::Library->new( _to_model( $c->validation->param('body') ) );
96         $library->store;
97         $c->res->headers->location( $c->req->url->to_string . '/' . $library->branchcode );
98         return $c->render( status => 201, openapi => _to_api( $library->TO_JSON ) );
99     }
100     catch {
101         unless ( blessed $_ && $_->can('rethrow') ) {
102             return $c->render(
103                 status  => 500,
104                 openapi => { error => "Something went wrong, check Koha logs for details." }
105             );
106         }
107         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
108             return $c->render(
109                 status  => 409,
110                 openapi => { error => $_->error, conflict => $_->duplicate_id }
111             );
112         }
113         else {
114             return $c->render(
115                 status  => 500,
116                 openapi => { error => "$_" }
117             );
118         }
119     };
120 }
121
122 =head3 update
123
124 Controller function that handles updating a Koha::Library object
125
126 =cut
127
128 sub update {
129     my $c = shift->openapi->valid_input or return;
130
131     my $library = Koha::Libraries->find( $c->validation->param('library_id') );
132
133     if ( not defined $library ) {
134         return $c->render(
135             status  => 404,
136             openapi => { error => "Library not found" }
137         );
138     }
139
140     return try {
141         my $params = $c->req->json;
142         $library->set( _to_model($params) );
143         $library->store();
144         return $c->render( status => 200, openapi => _to_api($library->TO_JSON) );
145     }
146     catch {
147         unless ( blessed $_ && $_->can('rethrow') ) {
148             return $c->render(
149                 status  => 500,
150                 openapi => { error => "Something went wrong, check Koha logs for details." }
151             );
152         }
153
154         return $c->render(
155             status  => 500,
156             openapi => { error => "$_" }
157         );
158     };
159 }
160
161 =head3 delete
162
163 Controller function that handles deleting a Koha::Library object
164
165 =cut
166
167 sub delete {
168
169     my $c = shift->openapi->valid_input or return;
170
171     my $library = Koha::Libraries->find( $c->validation->param( 'library_id' ) );
172
173     if ( not defined $library ) {
174         return $c->render( status => 404, openapi => { error => "Library not found" } );
175     }
176
177     return try {
178         $library->delete;
179         return $c->render( status => 204, openapi => '');
180     }
181     catch {
182         unless ( blessed $_ && $_->can('rethrow') ) {
183             return $c->render(
184                 status  => 500,
185                 openapi => { error => "Something went wrong, check Koha logs for details." }
186             );
187         }
188
189         return $c->render(
190             status  => 500,
191             openapi => { error => "$_" }
192         );
193     };
194 }
195
196 =head3 _to_api
197
198 Helper function that maps a hashref of Koha::Library attributes into REST api
199 attribute names.
200
201 =cut
202
203 sub _to_api {
204     my $library = shift;
205
206     # Rename attributes
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 )
211         {
212             # key /= undef
213             $library->{ $mapped_column } = delete $library->{ $column };
214         }
215         elsif (    exists $library->{ $column }
216                 && !defined $mapped_column )
217         {
218             # key == undef => to be deleted
219             delete $library->{ $column };
220         }
221     }
222
223     return $library;
224 }
225
226 =head3 _to_model
227
228 Helper function that maps REST api objects into Koha::Library
229 attribute names.
230
231 =cut
232
233 sub _to_model {
234     my $library = shift;
235
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 )
240         {
241             # key /= undef
242             $library->{ $mapped_attribute } = delete $library->{ $attribute };
243         }
244         elsif (    exists $library->{ $attribute }
245                 && !defined $mapped_attribute )
246         {
247             # key == undef => to be deleted
248             delete $library->{ $attribute };
249         }
250     }
251
252     if ( exists $library->{pickup_location} ) {
253         $library->{pickup_location} = ( $library->{pickup_location} ) ? 1 : 0;
254     }
255
256     return $library;
257 }
258
259
260 =head2 Global variables
261
262 =head3 $to_api_mapping
263
264 =cut
265
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',
277     branchfax        => 'fax',
278     branchemail      => 'email',
279     branchreplyto    => 'reply_to_email',
280     branchreturnpath => 'return_path_email',
281     branchurl        => 'url',
282     issuing          => undef,
283     branchip         => 'ip',
284     branchprinter    => undef,
285     branchnotes      => 'notes',
286     marcorgcode      => 'marc_org_code',
287 };
288
289 =head3 $to_model_mapping
290
291 =cut
292
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',
304     fax               => 'branchfax',
305     email             => 'branchemail',
306     reply_to_email    => 'branchreplyto',
307     return_path_email => 'branchreturnpath',
308     url               => 'branchurl',
309     ip                => 'branchip',
310     notes             => 'branchnotes',
311     marc_org_code     => 'marcorgcode',
312 };
313
314 1;