Browse Source

Bug 15496: Add API endoint for deleting a bib

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>
19.05.x
Kyle Hall 4 years ago
committed by Nick Clemens
parent
commit
2b45630e42
  1. 76
      Koha/REST/V1/Biblios.pm
  2. 3
      api/v1/swagger/parameters.json
  3. 9
      api/v1/swagger/parameters/biblio.json
  4. 3
      api/v1/swagger/paths.json
  5. 64
      api/v1/swagger/paths/biblios.json
  6. 71
      t/db_dependent/api/v1/biblios.t

76
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;

3
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"
},

9
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"
}
}

3
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"
},

64
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"
}
}
}
}
}

71
t/db_dependent/api/v1/biblios.t

@ -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;
};
Loading…
Cancel
Save