Bug 13895: Adapt naming according to voted RFC
[koha.git] / Koha / REST / V1 / Cities.pm
blob5a579b4ee49e10a148cc1fcc6833c46faa12fe7e
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
8 # version.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Cities;
24 use Try::Tiny;
26 =head1 API
28 =head2 Class Methods
30 =head3 list
32 =cut
34 sub list {
35 my $c = shift->openapi->valid_input or return;
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 );
42 catch {
43 if ( $_->isa('DBIx::Class::Exception') ) {
44 return $c->render( status => 500,
45 openapi => { error => $_->{msg} } );
47 else {
48 return $c->render( status => 500,
49 openapi => { error => "Something went wrong, check the logs."} );
55 =head3 get
57 =cut
59 sub get {
60 my $c = shift->openapi->valid_input or return;
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" } );
68 return $c->render( status => 200, openapi => _to_api($city->TO_JSON) );
71 =head3 add
73 =cut
75 sub add {
76 my $c = shift->openapi->valid_input or return;
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) );
83 catch {
84 if ( $_->isa('DBIx::Class::Exception') ) {
85 return $c->render(
86 status => 500,
87 openapi => { error => $_->{msg} }
90 else {
91 return $c->render(
92 status => 500,
93 openapi => { error => "Something went wrong, check the logs." }
99 =head3 update
101 =cut
103 sub update {
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" } );
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) );
119 catch {
120 if ( $_->isa('Koha::Exceptions::Object') ) {
121 return $c->render( status => 500,
122 openapi => { error => $_->message } );
124 else {
125 return $c->render( status => 500,
126 openapi => { error => "Something went wrong, check the logs."} );
131 =head3 delete
133 =cut
135 sub delete {
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" } );
144 return try {
145 $city->delete;
146 return $c->render( status => 200, openapi => "" );
148 catch {
149 if ( $_->isa('DBIx::Class::Exception') ) {
150 return $c->render( status => 500,
151 openapi => { error => $_->{msg} } );
153 else {
154 return $c->render( status => 500,
155 openapi => { error => "Something went wrong, check the logs."} );
160 =head3 _to_api
162 Helper function that maps a hashref of Koha::City attributes into REST api
163 attribute names.
165 =cut
167 sub _to_api {
168 my $city = shift;
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 )
176 # key /= undef
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 };
187 return $city;
190 =head3 _to_model
192 Helper function that maps REST api objects into Koha::Cities
193 attribute names.
195 =cut
197 sub _to_model {
198 my $city = shift;
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 )
205 # key /= undef
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 };
216 return $city;
219 =head2 Global variables
221 =head3 $to_api_mapping
223 =cut
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'
233 =head3 $to_model_mapping
235 =cut
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'