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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 );
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."} );