View | Details | Raw Unified | Return to bug 15496
Collapse All | Expand All

(-)a/Koha/REST/V1/Biblios.pm (+76 lines)
Line 0 Link Here
1
package Koha::REST::V1::Biblios;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Biblios;
23
use C4::Biblio qw(DelBiblio);
24
25
use Try::Tiny;
26
27
=head1 API
28
29
=head2 Class Methods
30
31
=head3 delete
32
33
=cut
34
35
sub delete {
36
    my $c = shift->openapi->valid_input or return;
37
38
    my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
39
40
    if ( not defined $biblio ) {
41
        return $c->render(
42
            status  => 404,
43
            openapi => { error => "Object not found" }
44
        );
45
    }
46
47
    return try {
48
        my $error = DelBiblio( $biblio->id );
49
50
        if ($error) {
51
            return $c->render(
52
                status  => 409,
53
                openapi => { error => $error }
54
            );
55
        }
56
        else {
57
            return $c->render( status => 200, openapi => "" );
58
        }
59
    }
60
    catch {
61
        if ( $_->isa('DBIx::Class::Exception') ) {
62
            return $c->render(
63
                status  => 500,
64
                openapi => { error => $_->{msg} }
65
            );
66
        }
67
        else {
68
            return $c->render(
69
                status  => 500,
70
                openapi => { error => "Something went wrong, check the logs." }
71
            );
72
        }
73
    };
74
}
75
76
1;
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 1-4 Link Here
1
{
1
{
2
  "biblio_id_pp": {
3
    "$ref": "parameters/biblio.json#/biblio_id_pp"
4
  },
2
  "patron_id_pp": {
5
  "patron_id_pp": {
3
    "$ref": "parameters/patron.json#/patron_id_pp"
6
    "$ref": "parameters/patron.json#/patron_id_pp"
4
  },
7
  },
(-)a/api/v1/swagger/parameters/biblio.json (+9 lines)
Line 0 Link Here
1
{
2
    "biblio_id_pp": {
3
      "name": "biblio_id",
4
      "in": "path",
5
      "description": "Record internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+3 lines)
Lines 26-31 Link Here
26
  "/cities/{city_id}": {
26
  "/cities/{city_id}": {
27
    "$ref": "paths/cities.json#/~1cities~1{city_id}"
27
    "$ref": "paths/cities.json#/~1cities~1{city_id}"
28
  },
28
  },
29
  "/biblios/{biblio_id}": {
30
    "$ref": "paths/biblios.json#/~1biblios~1{biblio_id}"
31
  },
29
  "/holds": {
32
  "/holds": {
30
    "$ref": "paths/holds.json#/~1holds"
33
    "$ref": "paths/holds.json#/~1holds"
31
  },
34
  },
(-)a/api/v1/swagger/paths/biblios.json (+64 lines)
Line 0 Link Here
1
{
2
  "/biblios/{biblio_id}": {
3
    "delete": {
4
      "x-mojo-to": "Biblios#delete",
5
      "operationId": "deleteBiblio",
6
      "tags": ["biblios"],
7
      "parameters": [{
8
        "$ref": "../parameters.json#/biblio_id_pp"
9
      }],
10
      "produces": [
11
        "application/json"
12
      ],
13
      "responses": {
14
        "200": {
15
          "description": "Biblio deleted",
16
          "schema": {
17
            "type": "string"
18
          }
19
        },
20
        "401": {
21
          "description": "Authentication required",
22
          "schema": {
23
            "$ref": "../definitions.json#/error"
24
          }
25
        },
26
        "403": {
27
          "description": "Access forbidden",
28
          "schema": {
29
            "$ref": "../definitions.json#/error"
30
          }
31
        },
32
        "404": {
33
          "description": "Biblio not found",
34
          "schema": {
35
            "$ref": "../definitions.json#/error"
36
          }
37
        },
38
        "409": {
39
          "description": "Unable to perform action on biblio",
40
          "schema": {
41
            "$ref": "../definitions.json#/error"
42
          }
43
        },
44
        "500": {
45
          "description": "Internal error",
46
          "schema": {
47
            "$ref": "../definitions.json#/error"
48
          }
49
        },
50
        "503": {
51
          "description": "Under maintenance",
52
          "schema": {
53
            "$ref": "../definitions.json#/error"
54
          }
55
        }
56
      },
57
      "x-koha-authorization": {
58
        "permissions": {
59
          "parameters": "edit_catalogue"
60
        }
61
      }
62
    }
63
  }
64
}
(-)a/t/db_dependent/api/v1/biblios.t (-1 / +71 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Mojo;
22
use Test::Warn;
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use C4::Auth;
28
use Koha::Biblios;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36
my $t = Test::Mojo->new('Koha::REST::V1');
37
38
subtest 'delete() tests' => sub {
39
40
    plan tests => 7;
41
42
    $schema->storage->txn_begin;
43
44
    my $patron = $builder->build_object(
45
        {
46
            class => 'Koha::Patrons',
47
            value => { flags => 9 }
48
        }
49
    );
50
    my $password = 'thePassword123';
51
    $patron->set_password( { password => $password, skip_validation => 1 } );
52
    my $userid = $patron->userid;
53
54
    my $item      = $builder->build_sample_item();
55
    my $biblio_id = $item->biblionumber;
56
57
    # Bibs with items cannot be deleted
58
    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
59
      ->status_is(409);
60
61
    $item->delete();
62
63
    # Bibs with no items can be deleted
64
    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
65
      ->status_is(200)->content_is(q{""});
66
67
    $t->delete_ok("//$userid:$password@/api/v1/biblios/$biblio_id")
68
      ->status_is(404);
69
70
    $schema->storage->txn_rollback;
71
};

Return to bug 15496