Bug 17428: [REST] Koha::REST::V1::Cities introduced
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 11 Oct 2016 11:26:39 +0000 (13:26 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 28 Oct 2016 14:42:49 +0000 (14:42 +0000)
This patch introduces full CRUD for Koha::Cit(ies|y) classes through
the REST api. To test, point your browser to /api/v1/cities to use
HTTPRequester/Postman (or the like).

And of course, run the unit tests:
- Apply the patches
- Update your minified swagger file:
  $ cd kohaclone/
  $ misc/devel/minifySwagger.pl -s api/v1/swagger/swagger.json \
                                -d api/v1/swagger/swagger.min.json
- Run:
  $ prove t/db_dependent/api/v1/cities.t
=> SUCCESS: Tests should return green, and no warnings.
- Sign off :-D

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Claire Gravely <claire_gravely@hotmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/REST/V1/Cities.pm [new file with mode: 0644]

diff --git a/Koha/REST/V1/Cities.pm b/Koha/REST/V1/Cities.pm
new file mode 100644 (file)
index 0000000..1b04e10
--- /dev/null
@@ -0,0 +1,133 @@
+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 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.
+
+use Modern::Perl;
+
+use Mojo::Base 'Mojolicious::Controller';
+
+use C4::Auth qw( haspermission );
+use Koha::City;
+use Koha::Cities;
+
+use Try::Tiny;
+
+sub list {
+    my ( $c, $args, $cb ) = @_;
+
+    my $cities;
+
+    return try {
+        $cities =
+          Koha::Cities->search( $c->req->query_params->to_hash )->unblessed;
+        return $c->$cb( $cities, 200 );
+    }
+    catch {
+        if ( $_->isa('DBIx::Class::Exception') ) {
+            return $c->$cb( { error => $_->{msg} }, 500 );
+        }
+        else {
+            return $c->$cb(
+                { error => "Something went wrong, check the logs." }, 500 );
+        }
+    };
+}
+
+sub get {
+    my ( $c, $args, $cb ) = @_;
+
+    my $city = Koha::Cities->find( $args->{cityid} );
+    unless ($city) {
+        return $c->$cb( { error => "City not found" }, 404 );
+    }
+
+    return $c->$cb( $city->unblessed, 200 );
+}
+
+sub add {
+    my ( $c, $args, $cb ) = @_;
+
+    my $city = Koha::City->new( $c->req->json );
+
+    return try {
+        $city->store;
+        return $c->$cb( $city->unblessed, 200 );
+    }
+    catch {
+        if ( $_->isa('DBIx::Class::Exception') ) {
+            return $c->$cb( { error => $_->msg }, 500 );
+        }
+        else {
+            return $c->$cb(
+                { error => "Something went wrong, check the logs." }, 500 );
+        }
+    };
+}
+
+sub update {
+    my ( $c, $args, $cb ) = @_;
+
+    my $city;
+
+    return try {
+        $city = Koha::Cities->find( $args->{cityid} );
+        while ( my ( $k, $v ) = each %{ $c->req->json } ) {
+            $city->$k($v);
+        }
+        $city->store;
+        return $c->$cb( $city->unblessed, 200 );
+    }
+    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 );
+        }
+    };
+
+}
+
+sub delete {
+    my ( $c, $args, $cb ) = @_;
+
+    my $city;
+
+    return try {
+        $city = Koha::Cities->find( $args->{cityid} );
+        $city->delete;
+        return $c->$cb( "", 200 );
+    }
+    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 );
+        }
+    };
+
+}
+
+1;