Bug 23893: Use in /cities
[koha.git] / Koha / REST / V1 / Cities.pm
blobbbc54defcbaca6a136756740727c3d0abb3049c0
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 => $city->to_api );
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_from_api( $c->validation->param('body') );
80 $city->store;
81 $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
82 return $c->render(
83 status => 201,
84 openapi => $city->to_api
87 catch {
88 if ( $_->isa('DBIx::Class::Exception') ) {
89 return $c->render(
90 status => 500,
91 openapi => { error => $_->{msg} }
94 else {
95 return $c->render(
96 status => 500,
97 openapi => { error => "Something went wrong, check the logs." }
103 =head3 update
105 =cut
107 sub update {
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" } );
117 return try {
118 $city->set_from_api( $c->validation->param('body') );
119 $city->store();
120 return $c->render( status => 200, openapi => $city->to_api );
122 catch {
123 if ( $_->isa('Koha::Exceptions::Object') ) {
124 return $c->render( status => 500,
125 openapi => { error => $_->message } );
127 else {
128 return $c->render( status => 500,
129 openapi => { error => "Something went wrong, check the logs."} );
134 =head3 delete
136 =cut
138 sub delete {
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" } );
147 return try {
148 $city->delete;
149 return $c->render( status => 200, openapi => "" );
151 catch {
152 if ( $_->isa('DBIx::Class::Exception') ) {
153 return $c->render( status => 500,
154 openapi => { error => $_->{msg} } );
156 else {
157 return $c->render( status => 500,
158 openapi => { error => "Something went wrong, check the logs."} );
163 =head3 _to_api
165 Helper function that maps a hashref of Koha::City attributes into REST api
166 attribute names.
168 =cut
170 sub _to_api {
171 my $city = shift;
173 # Rename attributes
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 )
179 # key /= undef
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 };
190 return $city;
193 =head3 _to_model
195 Helper function that maps REST api objects into Koha::Cities
196 attribute names.
198 =cut
200 sub _to_model {
201 my $city = shift;
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 )
208 # key /= undef
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 };
219 return $city;
222 =head2 Global variables
224 =head3 $to_api_mapping
226 =cut
228 our $to_api_mapping = {
229 cityid => 'city_id',
230 city_country => 'country',
231 city_name => 'name',
232 city_state => 'state',
233 city_zipcode => 'postal_code'
236 =head3 $to_model_mapping
238 =cut
240 our $to_model_mapping = {
241 city_id => 'cityid',
242 country => 'city_country',
243 name => 'city_name',
244 postal_code => 'city_zipcode',
245 state => 'city_state'