# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Mojo::Base 'Mojolicious::Controller';
-use C4::Auth qw( haspermission );
-use Koha::City;
use Koha::Cities;
-use Try::Tiny;
+use Try::Tiny qw( catch try );
-sub list {
- my ( $c, $args, $cb ) = @_;
+=head1 API
- my $cities;
- my $filter;
- $args //= {};
+=head2 Methods
- for my $filter_param ( keys %$args ) {
- $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
- }
+=head3 list
+
+=cut
+
+sub list {
+ my $c = shift->openapi->valid_input or return;
return try {
- $cities = Koha::Cities->search($filter)->unblessed;
- return $c->$cb( $cities, 200 );
+ my $cities_set = Koha::Cities->new;
+ my $cities = $c->objects->search( $cities_set );
+ return $c->render( status => 200, openapi => $cities );
}
catch {
- if ( $_->isa('DBIx::Class::Exception') ) {
- return $c->$cb( { error => $_->{msg} }, 500 );
- }
- else {
- return $c->$cb(
- { error => "Something went wrong, check the logs." }, 500 );
- }
+ $c->unhandled_exception($_);
};
+
}
+=head3 get
+
+=cut
+
sub get {
- my ( $c, $args, $cb ) = @_;
+ my $c = shift->openapi->valid_input or return;
- my $city = Koha::Cities->find( $args->{cityid} );
- unless ($city) {
- return $c->$cb( { error => "City not found" }, 404 );
- }
+ return try {
+ my $city = Koha::Cities->find( $c->validation->param('city_id') );
+ unless ($city) {
+ return $c->render( status => 404,
+ openapi => { error => "City not found" } );
+ }
- return $c->$cb( $city->unblessed, 200 );
+ return $c->render( status => 200, openapi => $city->to_api );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ }
}
-sub add {
- my ( $c, $args, $cb ) = @_;
+=head3 add
+
+=cut
- my $city = Koha::City->new( $args->{body} );
+sub add {
+ my $c = shift->openapi->valid_input or return;
return try {
+ my $city = Koha::City->new_from_api( $c->validation->param('body') );
$city->store;
- return $c->$cb( $city->unblessed, 200 );
+ $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
+ return $c->render(
+ status => 201,
+ openapi => $city->to_api
+ );
}
catch {
- if ( $_->isa('DBIx::Class::Exception') ) {
- return $c->$cb( { error => $_->msg }, 500 );
- }
- else {
- return $c->$cb(
- { error => "Something went wrong, check the logs." }, 500 );
- }
+ $c->unhandled_exception($_);
};
}
+=head3 update
+
+=cut
+
sub update {
- my ( $c, $args, $cb ) = @_;
+ my $c = shift->openapi->valid_input or return;
- my $city;
+ my $city = Koha::Cities->find( $c->validation->param('city_id') );
+
+ if ( not defined $city ) {
+ return $c->render( status => 404,
+ openapi => { error => "Object not found" } );
+ }
return try {
- $city = Koha::Cities->find( $args->{cityid} );
- $city->set( $args->{body} );
+ $city->set_from_api( $c->validation->param('body') );
$city->store();
- return $c->$cb( $city->unblessed, 200 );
+ return $c->render( status => 200, openapi => $city->to_api );
}
catch {
- if ( not defined $city ) {
- return $c->$cb( { error => "Object not found" }, 404 );
- }
- elsif ( $_->isa('Koha::Exceptions::Object') ) {
- return $c->$cb( { error => $_->message }, 500 );
- }
- else {
- return $c->$cb(
- { error => "Something went wrong, check the logs." }, 500 );
- }
+ $c->unhandled_exception($_);
};
-
}
+=head3 delete
+
+=cut
+
sub delete {
- my ( $c, $args, $cb ) = @_;
+ my $c = shift->openapi->valid_input or return;
- my $city;
+ my $city = Koha::Cities->find( $c->validation->param('city_id') );
+ if ( not defined $city ) {
+ return $c->render( status => 404,
+ openapi => { error => "Object not found" } );
+ }
return try {
- $city = Koha::Cities->find( $args->{cityid} );
$city->delete;
- return $c->$cb( "", 200 );
+ return $c->render(
+ status => 204,
+ openapi => q{}
+ );
}
catch {
- if ( not defined $city ) {
- return $c->$cb( { error => "Object not found" }, 404 );
- }
- elsif ( $_->isa('DBIx::Class::Exception') ) {
- return $c->$cb( { error => $_->msg }, 500 );
- }
- else {
- return $c->$cb(
- { error => "Something went wrong, check the logs." }, 500 );
- }
+ $c->unhandled_exception($_);
};
-
}
1;