1 package Koha
::REST
::V1
::Cities
;
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';
35 my $c = shift->openapi->valid_input or return;
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 );
43 if ( $_->isa('DBIx::Class::Exception') ) {
44 return $c->render( status
=> 500,
45 openapi
=> { error
=> $_->{msg
} } );
48 return $c->render( status
=> 500,
49 openapi
=> { error
=> "Something went wrong, check the logs."} );
60 my $c = shift->openapi->valid_input or return;
62 my $city = Koha
::Cities
->find( $c->validation->param('city_id') );
64 return $c->render( status
=> 404,
65 openapi
=> { error
=> "City not found" } );
68 return $c->render( status
=> 200, openapi
=> $city->to_api );
76 my $c = shift->openapi->valid_input or return;
79 my $city = Koha
::City
->new_from_api( $c->validation->param('body') );
81 $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
84 openapi
=> $city->to_api
88 if ( $_->isa('DBIx::Class::Exception') ) {
91 openapi
=> { error
=> $_->{msg
} }
97 openapi
=> { error
=> "Something went wrong, check the logs." }
108 my $c = shift->openapi->valid_input or return;
110 my $city = Koha
::Cities
->find( $c->validation->param('city_id') );
112 if ( not defined $city ) {
113 return $c->render( status
=> 404,
114 openapi
=> { error
=> "Object not found" } );
118 $city->set_from_api( $c->validation->param('body') );
120 return $c->render( status
=> 200, openapi
=> $city->to_api );
123 if ( $_->isa('Koha::Exceptions::Object') ) {
124 return $c->render( status
=> 500,
125 openapi
=> { error
=> $_->message } );
128 return $c->render( status
=> 500,
129 openapi
=> { error
=> "Something went wrong, check the logs."} );
139 my $c = shift->openapi->valid_input or return;
141 my $city = Koha
::Cities
->find( $c->validation->param('city_id') );
142 if ( not defined $city ) {
143 return $c->render( status
=> 404,
144 openapi
=> { error
=> "Object not found" } );
149 return $c->render( status
=> 200, openapi
=> "" );
152 if ( $_->isa('DBIx::Class::Exception') ) {
153 return $c->render( status
=> 500,
154 openapi
=> { error
=> $_->{msg
} } );
157 return $c->render( status
=> 500,
158 openapi
=> { error
=> "Something went wrong, check the logs."} );
165 Helper function that maps a hashref of Koha::City attributes into REST api
174 foreach my $column ( keys %{ $Koha::REST
::V1
::Cities
::to_api_mapping
} ) {
175 my $mapped_column = $Koha::REST
::V1
::Cities
::to_api_mapping
->{$column};
176 if ( exists $city->{ $column }
177 && defined $mapped_column )
180 $city->{ $mapped_column } = delete $city->{ $column };
182 elsif ( exists $city->{ $column }
183 && !defined $mapped_column )
185 # key == undef => to be deleted
186 delete $city->{ $column };
195 Helper function that maps REST api objects into Koha::Cities
203 foreach my $attribute ( keys %{ $Koha::REST
::V1
::Cities
::to_model_mapping
} ) {
204 my $mapped_attribute = $Koha::REST
::V1
::Cities
::to_model_mapping
->{$attribute};
205 if ( exists $city->{ $attribute }
206 && defined $mapped_attribute )
209 $city->{ $mapped_attribute } = delete $city->{ $attribute };
211 elsif ( exists $city->{ $attribute }
212 && !defined $mapped_attribute )
214 # key == undef => to be deleted
215 delete $city->{ $attribute };
222 =head2 Global variables
224 =head3 $to_api_mapping
228 our $to_api_mapping = {
230 city_country
=> 'country',
232 city_state
=> 'state',
233 city_zipcode
=> 'postal_code'
236 =head3 $to_model_mapping
240 our $to_model_mapping = {
242 country
=> 'city_country',
244 postal_code
=> 'city_zipcode',
245 state => 'city_state'