Bug 22483: Explicitly ban 'undef' as a valid $flagsrequired
[koha.git] / Koha / REST / V1 / Cities.pm
1 package Koha::REST::V1::Cities;
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::Cities;
23
24 use Try::Tiny;
25
26 =head1 API
27
28 =head2 Class Methods
29
30 =head3 list
31
32 =cut
33
34 sub list {
35     my $c = shift->openapi->valid_input or return;
36
37     return try {
38         my $cities_set = Koha::Cities->new;
39         my $cities = $c->objects->search( $cities_set, \&_to_model, \&_to_api );
40         return $c->render( status => 200, openapi => $cities );
41     }
42     catch {
43         if ( $_->isa('DBIx::Class::Exception') ) {
44             return $c->render( status  => 500,
45                                openapi => { error => $_->{msg} } );
46         }
47         else {
48             return $c->render( status => 500,
49                 openapi => { error => "Something went wrong, check the logs."} );
50         }
51     };
52
53 }
54
55 =head3 get
56
57 =cut
58
59 sub get {
60     my $c = shift->openapi->valid_input or return;
61
62     my $city = Koha::Cities->find( $c->validation->param('city_id') );
63     unless ($city) {
64         return $c->render( status  => 404,
65                            openapi => { error => "City not found" } );
66     }
67
68     return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
69 }
70
71 =head3 add
72
73 =cut
74
75 sub add {
76     my $c = shift->openapi->valid_input or return;
77
78     return try {
79         my $city = Koha::City->new( _to_model( $c->validation->param('body') ) );
80         $city->store;
81         return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
82     }
83     catch {
84         if ( $_->isa('DBIx::Class::Exception') ) {
85             return $c->render(
86                 status  => 500,
87                 openapi => { error => $_->{msg} }
88             );
89         }
90         else {
91             return $c->render(
92                 status  => 500,
93                 openapi => { error => "Something went wrong, check the logs." }
94             );
95         }
96     };
97 }
98
99 =head3 update
100
101 =cut
102
103 sub update {
104     my $c = shift->openapi->valid_input or return;
105
106     my $city = Koha::Cities->find( $c->validation->param('city_id') );
107
108     if ( not defined $city ) {
109         return $c->render( status  => 404,
110                            openapi => { error => "Object not found" } );
111     }
112
113     return try {
114         my $params = $c->req->json;
115         $city->set( _to_model($params) );
116         $city->store();
117         return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
118     }
119     catch {
120         if ( $_->isa('Koha::Exceptions::Object') ) {
121             return $c->render( status  => 500,
122                                openapi => { error => $_->message } );
123         }
124         else {
125             return $c->render( status => 500,
126                 openapi => { error => "Something went wrong, check the logs."} );
127         }
128     };
129 }
130
131 =head3 delete
132
133 =cut
134
135 sub delete {
136     my $c = shift->openapi->valid_input or return;
137
138     my $city = Koha::Cities->find( $c->validation->param('city_id') );
139     if ( not defined $city ) {
140         return $c->render( status  => 404,
141                            openapi => { error => "Object not found" } );
142     }
143
144     return try {
145         $city->delete;
146         return $c->render( status => 200, openapi => "" );
147     }
148     catch {
149         if ( $_->isa('DBIx::Class::Exception') ) {
150             return $c->render( status  => 500,
151                                openapi => { error => $_->{msg} } );
152         }
153         else {
154             return $c->render( status => 500,
155                 openapi => { error => "Something went wrong, check the logs."} );
156         }
157     };
158 }
159
160 =head3 _to_api
161
162 Helper function that maps a hashref of Koha::City attributes into REST api
163 attribute names.
164
165 =cut
166
167 sub _to_api {
168     my $city    = shift;
169
170     # Rename attributes
171     foreach my $column ( keys %{ $Koha::REST::V1::Cities::to_api_mapping } ) {
172         my $mapped_column = $Koha::REST::V1::Cities::to_api_mapping->{$column};
173         if (    exists $city->{ $column }
174              && defined $mapped_column )
175         {
176             # key /= undef
177             $city->{ $mapped_column } = delete $city->{ $column };
178         }
179         elsif (    exists $city->{ $column }
180                 && !defined $mapped_column )
181         {
182             # key == undef => to be deleted
183             delete $city->{ $column };
184         }
185     }
186
187     return $city;
188 }
189
190 =head3 _to_model
191
192 Helper function that maps REST api objects into Koha::Cities
193 attribute names.
194
195 =cut
196
197 sub _to_model {
198     my $city = shift;
199
200     foreach my $attribute ( keys %{ $Koha::REST::V1::Cities::to_model_mapping } ) {
201         my $mapped_attribute = $Koha::REST::V1::Cities::to_model_mapping->{$attribute};
202         if (    exists $city->{ $attribute }
203              && defined $mapped_attribute )
204         {
205             # key /= undef
206             $city->{ $mapped_attribute } = delete $city->{ $attribute };
207         }
208         elsif (    exists $city->{ $attribute }
209                 && !defined $mapped_attribute )
210         {
211             # key == undef => to be deleted
212             delete $city->{ $attribute };
213         }
214     }
215
216     return $city;
217 }
218
219 =head2 Global variables
220
221 =head3 $to_api_mapping
222
223 =cut
224
225 our $to_api_mapping = {
226     cityid       => 'city_id',
227     city_country => 'country',
228     city_name    => 'name',
229     city_state   => 'state',
230     city_zipcode => 'postal_code'
231 };
232
233 =head3 $to_model_mapping
234
235 =cut
236
237 our $to_model_mapping = {
238     city_id     => 'cityid',
239     country     => 'city_country',
240     name        => 'city_name',
241     postal_code => 'city_zipcode',
242     state       => 'city_state'
243 };
244
245 1;