X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;ds=sidebyside;f=Koha%2FREST%2FV1%2FCities.pm;h=3dfc91d56ffd90e74c444a9ccb255706dde445c5;hb=9d6d641d1f8b77271800f43bc027b651f9aea52b;hp=e8ababe10aa090daca0b32f5695bde6a15d1d364;hpb=c15db78d3c26903c557b00a35bba329a4022b569;p=srvgit diff --git a/Koha/REST/V1/Cities.pm b/Koha/REST/V1/Cities.pm index e8ababe10a..3dfc91d56f 100644 --- a/Koha/REST/V1/Cities.pm +++ b/Koha/REST/V1/Cities.pm @@ -2,135 +2,138 @@ package Koha::REST::V1::Cities; # 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 . 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;