Bug 15496: Add API endoint for deleting a bib
authorKyle M Hall <kyle@bywatersolutions.com>
Thu, 11 Apr 2019 14:05:14 +0000 (10:05 -0400)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 26 Apr 2019 10:54:49 +0000 (10:54 +0000)
Signed-off-by: Liz Rea <wizzyrea@gmail.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/REST/V1/Biblios.pm [new file with mode: 0644]
api/v1/swagger/parameters.json
api/v1/swagger/parameters/biblio.json [new file with mode: 0644]
api/v1/swagger/paths.json
api/v1/swagger/paths/biblios.json [new file with mode: 0644]
t/db_dependent/api/v1/biblios.t [new file with mode: 0644]

diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm
new file mode 100644 (file)
index 0000000..d5e5564
--- /dev/null
@@ -0,0 +1,76 @@
+package Koha::REST::V1::Biblios;
+
+# 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 Koha::Biblios;
+use C4::Biblio qw(DelBiblio);
+
+use Try::Tiny;
+
+=head1 API
+
+=head2 Class Methods
+
+=head3 delete
+
+=cut
+
+sub delete {
+    my $c = shift->openapi->valid_input or return;
+
+    my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
+
+    if ( not defined $biblio ) {
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Object not found" }
+        );
+    }
+
+    return try {
+        my $error = DelBiblio( $biblio->id );
+
+        if ($error) {
+            return $c->render(
+                status  => 409,
+                openapi => { error => $error }
+            );
+        }
+        else {
+            return $c->render( status => 200, openapi => "" );
+        }
+    }
+    catch {
+        if ( $_->isa('DBIx::Class::Exception') ) {
+            return $c->render(
+                status  => 500,
+                openapi => { error => $_->{msg} }
+            );
+        }
+        else {
+            return $c->render(
+                status  => 500,
+                openapi => { error => "Something went wrong, check the logs." }
+            );
+        }
+    };
+}
+
+1;
index 11ce57c..fce13be 100644 (file)
@@ -1,4 +1,7 @@
 {
+  "biblio_id_pp": {
+    "$ref": "parameters/biblio.json#/biblio_id_pp"
+  },
   "patron_id_pp": {
     "$ref": "parameters/patron.json#/patron_id_pp"
   },
diff --git a/api/v1/swagger/parameters/biblio.json b/api/v1/swagger/parameters/biblio.json
new file mode 100644 (file)
index 0000000..95b8ffc
--- /dev/null
@@ -0,0 +1,9 @@
+{
+    "biblio_id_pp": {
+      "name": "biblio_id",
+      "in": "path",
+      "description": "Record internal identifier",
+      "required": true,
+      "type": "integer"
+    }
+}
index d0c2733..33cfd3a 100644 (file)
@@ -26,6 +26,9 @@
   "/cities/{city_id}": {
     "$ref": "paths/cities.json#/~1cities~1{city_id}"
   },
+  "/biblios/{biblio_id}": {
+    "$ref": "paths/biblios.json#/~1biblios~1{biblio_id}"
+  },
   "/holds": {
     "$ref": "paths/holds.json#/~1holds"
   },
diff --git a/api/v1/swagger/paths/biblios.json b/api/v1/swagger/paths/biblios.json
new file mode 100644 (file)
index 0000000..73ace63
--- /dev/null
@@ -0,0 +1,64 @@
+{
+  "/biblios/{biblio_id}": {
+    "delete": {
+      "x-mojo-to": "Biblios#delete",
+      "operationId": "deleteBiblio",
+      "tags": ["biblios"],
+      "parameters": [{
+        "$ref": "../parameters.json#/biblio_id_pp"
+      }],
+      "produces": [
+        "application/json"
+      ],
+      "responses": {
+        "200": {
+          "description": "Biblio deleted",
+          "schema": {
+            "type": "string"
+          }
+        },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "403": {
+          "description": "Access forbidden",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "404": {
+          "description": "Biblio not found",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "409": {
+          "description": "Unable to perform action on biblio",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "500": {
+          "description": "Internal error",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        }
+      },
+      "x-koha-authorization": {
+        "permissions": {
+          "parameters": "edit_catalogue"
+        }
+      }
+    }
+  }
+}
diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t
new file mode 100644 (file)
index 0000000..03ef7cd
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+
+# 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 Test::More tests => 1;
+use Test::Mojo;
+use Test::Warn;
+
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use C4::Auth;
+use Koha::Biblios;
+use Koha::Database;
+
+my $schema  = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
+
+my $t = Test::Mojo->new('Koha::REST::V1');
+
+subtest 'delete() tests' => sub {
+
+    plan tests => 7;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { flags => 9 }
+        }
+    );
+    my $password = 'thePassword123';
+    $patron->set_password( { password => $password, skip_validation => 1 } );
+    my $userid = $patron->userid;
+
+    my $item      = $builder->build_sample_item();
+    my $biblio_id = $item->biblionumber;
+
+    # Bibs with items cannot be deleted
+    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
+      ->status_is(409);
+
+    $item->delete();
+
+    # Bibs with no items can be deleted
+    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
+      ->status_is(200)->content_is(q{""});
+
+    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
+      ->status_is(404);
+
+    $schema->storage->txn_rollback;
+};