From 8ba3af26e2aff2b858b5c111921bfe5bc5817f97 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 11 Apr 2019 10:05:14 -0400 Subject: [PATCH] Bug 15496: Add API endoint for deleting a bib --- Koha/REST/V1/Biblios.pm | 76 +++++++++++++++++++++++++++ api/v1/swagger/parameters.json | 3 ++ api/v1/swagger/parameters/biblio.json | 9 ++++ api/v1/swagger/paths.json | 3 ++ api/v1/swagger/paths/biblios.json | 64 ++++++++++++++++++++++ t/db_dependent/api/v1/biblios.t | 72 +++++++++++++++++++++++++ 6 files changed, 227 insertions(+) create mode 100644 Koha/REST/V1/Biblios.pm create mode 100644 api/v1/swagger/parameters/biblio.json create mode 100644 api/v1/swagger/paths/biblios.json create mode 100644 t/db_dependent/api/v1/biblios.t diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm new file mode 100644 index 0000000000..d5e5564b0c --- /dev/null +++ b/Koha/REST/V1/Biblios.pm @@ -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; diff --git a/api/v1/swagger/parameters.json b/api/v1/swagger/parameters.json index 11ce57cd9a..fce13be958 100644 --- a/api/v1/swagger/parameters.json +++ b/api/v1/swagger/parameters.json @@ -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 index 0000000000..95b8ffcb31 --- /dev/null +++ b/api/v1/swagger/parameters/biblio.json @@ -0,0 +1,9 @@ +{ + "biblio_id_pp": { + "name": "biblio_id", + "in": "path", + "description": "Record internal identifier", + "required": true, + "type": "integer" + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index d0c27338d0..33cfd3aa5a 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -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 index 0000000000..73ace63c07 --- /dev/null +++ b/api/v1/swagger/paths/biblios.json @@ -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 index 0000000000..6fe18b1a68 --- /dev/null +++ b/t/db_dependent/api/v1/biblios.t @@ -0,0 +1,72 @@ +#!/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; +}; + -- 2.20.1 (Apple Git-117)