Bug 24545: Fix license statements
[srvgit] / Koha / REST / V1 / Cities.pm
index 5d32ea9..4c49e1a 100644 (file)
@@ -2,134 +2,162 @@ 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 <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
 use Mojo::Base 'Mojolicious::Controller';
 
-use Koha::City;
 use Koha::Cities;
 
 use Try::Tiny;
 
-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);
-        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 );
+            return $c->render( status  => 500,
+                               openapi => { error => $_->{msg} } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
+
 }
 
+=head3 get
+
+=cut
+
 sub get {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
 
-    my $city = Koha::Cities->find( $args->{cityid} );
+    my $city = Koha::Cities->find( $c->validation->param('city_id') );
     unless ($city) {
-        return $c->$cb( { error => "City not found" }, 404 );
+        return $c->render( status  => 404,
+                           openapi => { error => "City not found" } );
     }
 
-    return $c->$cb( $city, 200 );
+    return $c->render( status => 200, openapi => $city->to_api );
 }
 
-sub add {
-    my ( $c, $args, $cb ) = @_;
+=head3 add
 
-    my $city = Koha::City->new( $args->{body} );
+=cut
+
+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, 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 );
+            return $c->render(
+                status  => 500,
+                openapi => { error => $_->{msg} }
+            );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check the logs." }
+            );
         }
     };
 }
 
+=head3 update
+
+=cut
+
 sub update {
-    my ( $c, $args, $cb ) = @_;
+    my $c = shift->openapi->valid_input or return;
+
+    my $city = Koha::Cities->find( $c->validation->param('city_id') );
 
-    my $city;
+    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, 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 );
+        if ( $_->isa('Koha::Exceptions::Object') ) {
+            return $c->render( status  => 500,
+                               openapi => { error => $_->message } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
-
 }
 
+=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 => 200, openapi => "" );
     }
     catch {
-        if ( not defined $city ) {
-            return $c->$cb( { error => "Object not found" }, 404 );
-        }
-        elsif ( $_->isa('DBIx::Class::Exception') ) {
-            return $c->$cb( { error => $_->msg }, 500 );
+        if ( $_->isa('DBIx::Class::Exception') ) {
+            return $c->render( status  => 500,
+                               openapi => { error => $_->{msg} } );
         }
         else {
-            return $c->$cb(
-                { error => "Something went wrong, check the logs." }, 500 );
+            return $c->render( status => 500,
+                openapi => { error => "Something went wrong, check the logs."} );
         }
     };
-
 }
 
 1;