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 => _to_api($city->TO_JSON) );
76 my $c = shift->openapi->valid_input or return;
79 my $city = Koha::City->new( _to_model( $c->validation->param('body') ) );
81 return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
84 if ( $_->isa('DBIx::Class::Exception') ) {
87 openapi => { error => $_->{msg} }
93 openapi => { error => "Something went wrong, check the logs." }
104 my $c = shift->openapi->valid_input or return;
106 my $city = Koha::Cities->find( $c->validation->param('city_id') );
108 if ( not defined $city ) {
109 return $c->render( status => 404,
110 openapi => { error => "Object not found" } );
114 my $params = $c->req->json;
115 $city->set( _to_model($params) );
117 return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
120 if ( $_->isa('Koha::Exceptions::Object') ) {
121 return $c->render( status => 500,
122 openapi => { error => $_->message } );
125 return $c->render( status => 500,
126 openapi => { error => "Something went wrong, check the logs."} );
136 my $c = shift->openapi->valid_input or return;
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" } );
146 return $c->render( status => 200, openapi => "" );
149 if ( $_->isa('DBIx::Class::Exception') ) {
150 return $c->render( status => 500,
151 openapi => { error => $_->{msg} } );
154 return $c->render( status => 500,
155 openapi => { error => "Something went wrong, check the logs."} );
162 Helper function that maps a hashref of Koha::City attributes into REST api
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 )
177 $city->{ $mapped_column } = delete $city->{ $column };
179 elsif ( exists $city->{ $column }
180 && !defined $mapped_column )
182 # key == undef => to be deleted
183 delete $city->{ $column };
192 Helper function that maps REST api objects into Koha::Cities
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 )
206 $city->{ $mapped_attribute } = delete $city->{ $attribute };
208 elsif ( exists $city->{ $attribute }
209 && !defined $mapped_attribute )
211 # key == undef => to be deleted
212 delete $city->{ $attribute };
219 =head2 Global variables
221 =head3 $to_api_mapping
225 our $to_api_mapping = {
227 city_country => 'country',
229 city_state => 'state',
230 city_zipcode => 'postal_code'
233 =head3 $to_model_mapping
237 our $to_model_mapping = {
239 country => 'city_country',
241 postal_code => 'city_zipcode',
242 state => 'city_state'