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

(-)a/Koha/REST/V1/Biblios.pm (+36 lines)
Lines 409-412 sub get_items_public { Link Here
409
    };
409
    };
410
}
410
}
411
411
412
=head3 setMetadata
413
414
Updates biblio metadata
415
416
=cut
417
418
sub setMetadata {
419
    my $c = shift->openapi->valid_input or return;
420
421
    my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
422
423
    if ( not defined $biblio ) {
424
        return $c->render(
425
            status  => 404,
426
            openapi => { error => "Object not found" }
427
        );
428
    }
429
430
    my $marcxml = $c->validation->param('body');
431
    my $format = C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'UNIMARC' : 'MARC21';
432
    my $record = eval { MARC::Record->new_from_xml($marcxml, 'UTF-8', $format) };
433
    if ($@) {
434
        return $c->render(status => 400, openapi => { error => "$@" });
435
    }
436
437
    my $success = eval { C4::Biblio::ModBiblio( $record, $biblio->id, $biblio->frameworkcode ) };
438
    if (!$success || $@) {
439
        return $c->render(
440
            status  => 500,
441
            openapi => { error => "$@" || "Internal server error" }
442
        );
443
    }
444
445
    return $c->render( status => 204, openapi => "" );
446
}
447
412
1;
448
1;
(-)a/api/v1/swagger/paths/biblios.yaml (+49 lines)
Lines 217-222 Link Here
217
    x-koha-authorization:
217
    x-koha-authorization:
218
      permissions:
218
      permissions:
219
        catalogue: "1"
219
        catalogue: "1"
220
"/biblios/{biblio_id}/metadata":
221
  parameters:
222
    - $ref: "../swagger.yaml#/parameters/biblio_id_pp"
223
  put:
224
    x-mojo-to: Biblios#setMetadata
225
    operationId: setBiblioMetadata
226
    tags:
227
      - biblios
228
    summary: Set biblio metadata
229
    parameters:
230
      - name: body
231
        in: body
232
        required: true
233
        schema:
234
          type: string
235
    produces:
236
      - application/json
237
    responses:
238
      "204":
239
        description: Biblio metadata updated
240
        schema:
241
          type: string
242
      "400":
243
        description: Bad request
244
        schema:
245
          $ref: "../swagger.yaml#/definitions/error"
246
      "401":
247
        description: Authentication required
248
        schema:
249
          $ref: "../swagger.yaml#/definitions/error"
250
      "403":
251
        description: Access forbidden
252
        schema:
253
          $ref: "../swagger.yaml#/definitions/error"
254
      "404":
255
        description: Biblio not found
256
        schema:
257
          $ref: "../swagger.yaml#/definitions/error"
258
      "500":
259
        description: Internal error
260
        schema:
261
          $ref: "../swagger.yaml#/definitions/error"
262
      "503":
263
        description: Under maintenance
264
        schema:
265
          $ref: "../swagger.yaml#/definitions/error"
266
    x-koha-authorization:
267
      permissions:
268
        editcatalogue: edit_catalogue
220
"/biblios/{biblio_id}/pickup_locations":
269
"/biblios/{biblio_id}/pickup_locations":
221
  get:
270
  get:
222
    x-mojo-to: Biblios#pickup_locations
271
    x-mojo-to: Biblios#pickup_locations
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 99-104 paths: Link Here
99
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
99
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts"
100
  "/biblios/{biblio_id}/items":
100
  "/biblios/{biblio_id}/items":
101
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
101
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1items"
102
  "/biblios/{biblio_id}/metadata":
103
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1metadata"
102
  "/biblios/{biblio_id}/pickup_locations":
104
  "/biblios/{biblio_id}/pickup_locations":
103
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
105
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations"
104
  "/cash_registers/{cash_register_id}/cashups":
106
  "/cash_registers/{cash_register_id}/cashups":
(-)a/t/db_dependent/api/v1/biblios/metadata/put.t (-1 / +90 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
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use utf8;
21
use Encode;
22
23
use Test::More tests => 1;
24
use Test::MockModule;
25
use Test::Mojo;
26
use Test::Warn;
27
use JSON;
28
29
use t::lib::Mocks;
30
use t::lib::TestBuilder;
31
32
use C4::Auth;
33
34
use Koha::Biblios;
35
use Koha::Database;
36
37
my $schema  = Koha::Database->new->schema;
38
my $builder = t::lib::TestBuilder->new;
39
40
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
41
42
my $t = Test::Mojo->new('Koha::REST::V1');
43
44
subtest 'PUT /api/v1/biblios/{biblio_id}/metadata' => sub {
45
    plan tests => 10;
46
47
    $schema->storage->txn_begin;
48
49
    my $patron = $builder->build_object(
50
        {
51
            class => 'Koha::Patrons',
52
            value => { flags => 0 }
53
        }
54
    );
55
    my $password = 'thePassword123';
56
    $patron->set_password( { password => $password, skip_validation => 1 } );
57
    $patron->discard_changes;
58
    my $userid = $patron->userid;
59
60
    my $biblio = $builder->build_sample_biblio({
61
        title  => 'The unbearable lightness of being',
62
        author => 'Milan Kundera',
63
    });
64
    my $biblio_id = $biblio->id;
65
66
    $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/metadata")
67
      ->status_is(403);
68
69
    $patron->flags(9)->store;
70
71
    $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/metadata")
72
      ->status_is(400);
73
74
    $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/metadata", "not marcxml")
75
      ->status_is(400)
76
      ->json_like('/error', qr/parser error/);
77
78
    my $biblio2 = $builder->build_sample_biblio({
79
        'title' => 'Another title',
80
    });
81
    my $record2 = $biblio2->metadata->record;
82
83
    $t->put_ok("//$userid:$password@/api/v1/biblios/$biblio_id/metadata", $record2->as_xml_record)
84
      ->status_is(204);
85
86
    $biblio->discard_changes;
87
    is($biblio->title, 'Another title');
88
89
    $schema->storage->txn_rollback;
90
};

Return to bug 30799