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

(-)a/Koha/REST/V1/Biblios.pm (-1 / +70 lines)
Lines 21-27 use Mojo::Base 'Mojolicious::Controller'; Link Here
21
21
22
use Koha::Biblios;
22
use Koha::Biblios;
23
use Koha::RecordProcessor;
23
use Koha::RecordProcessor;
24
use C4::Biblio qw( DelBiblio );
24
use C4::Biblio qw( AddBiblio DelBiblio );
25
25
26
use List::MoreUtils qw( any );
26
use List::MoreUtils qw( any );
27
use MARC::Record::MiJ;
27
use MARC::Record::MiJ;
Lines 32-37 use Try::Tiny qw( catch try ); Link Here
32
32
33
=head2 Methods
33
=head2 Methods
34
34
35
=head2 add
36
37
=cut
38
39
sub add {
40
    my $c = shift->openapi->valid_input or return;
41
42
    my $body = $c->validation->param('body');
43
    my $marcxml = $body->{marcxml};
44
    my $frameworkcode = $body->{framework_id} // '';
45
46
    try {
47
        my $record = MARC::Record->new_from_xml($marcxml);
48
        my ($biblionumber) = AddBiblio($record, $frameworkcode);
49
50
        my $attributes;
51
        $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
52
            unless $c->req->headers->accept =~ m/application\/json/;
53
54
        my $biblio = Koha::Biblios->find( { biblionumber => $biblionumber }, $attributes );
55
56
        if ( $c->req->headers->accept =~ m/application\/json/ ) {
57
            return $c->render(
58
                status => 200,
59
                json   => $biblio->to_api
60
            );
61
        }
62
        else {
63
            my $record = $biblio->metadata->record;
64
65
            $c->respond_to(
66
                marcxml => {
67
                    status => 200,
68
                    format => 'marcxml',
69
                    text   => $record->as_xml_record
70
                },
71
                mij => {
72
                    status => 200,
73
                    format => 'mij',
74
                    data   => $record->to_mij
75
                },
76
                marc => {
77
                    status => 200,
78
                    format => 'marc',
79
                    text   => $record->as_usmarc
80
                },
81
                txt => {
82
                    status => 200,
83
                    format => 'text/plain',
84
                    text   => $record->as_formatted
85
                },
86
                any => {
87
                    status  => 406,
88
                    openapi => [
89
                        "application/json",
90
                        "application/marcxml+xml",
91
                        "application/marc-in-json",
92
                        "application/marc",
93
                        "text/plain"
94
                    ]
95
                }
96
            );
97
        }
98
    }
99
    catch {
100
        $c->unhandled_exception($_);
101
    };
102
}
103
35
=head3 get
104
=head3 get
36
105
37
Controller function that handles retrieving a single biblio object
106
Controller function that handles retrieving a single biblio object
(-)a/api/v1/swagger/paths.yaml (+2 lines)
Lines 13-18 Link Here
13
  $ref: paths/acquisitions_funds.yaml#/~1acquisitions~1funds
13
  $ref: paths/acquisitions_funds.yaml#/~1acquisitions~1funds
14
"/article_requests/{article_request_id}":
14
"/article_requests/{article_request_id}":
15
  $ref: paths/article_requests.yaml#/~1article_requests~1{article_request_id}
15
  $ref: paths/article_requests.yaml#/~1article_requests~1{article_request_id}
16
"/biblios":
17
  $ref: paths/biblios.yaml#/~1biblios
16
"/biblios/{biblio_id}":
18
"/biblios/{biblio_id}":
17
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}
19
  $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}
18
"/biblios/{biblio_id}/checkouts":
20
"/biblios/{biblio_id}/checkouts":
(-)a/api/v1/swagger/paths/biblios.yaml (+58 lines)
Lines 1-4 Link Here
1
---
1
---
2
"/biblios":
3
  post:
4
    x-mojo-to: Biblios#add
5
    operationId: addBiblio
6
    tags:
7
      - biblios
8
    summary: Add biblio
9
    parameters:
10
      - name: body
11
        in: body
12
        required: true
13
        schema:
14
          type: object
15
          required:
16
            - marcxml
17
          properties:
18
            marcxml:
19
              type: string
20
            framework_id:
21
              type: string
22
          additionalProperties: false
23
    consumes:
24
      - application/json
25
    produces:
26
      - application/json
27
      - application/marcxml+xml
28
      - application/marc-in-json
29
      - application/marc
30
      - text/plain
31
    responses:
32
      "201":
33
        description: A biblio
34
      "401":
35
        description: Authentication required
36
        schema:
37
          $ref: ../definitions.yaml#/error
38
      "403":
39
        description: Access forbidden
40
        schema:
41
          $ref: ../definitions.yaml#/error
42
      "406":
43
        description: Not acceptable
44
        schema:
45
          type: array
46
          description: Accepted content-types
47
          items:
48
              type: string
49
      "500":
50
        description: Internal server error
51
        schema:
52
          $ref: ../definitions.yaml#/error
53
      "503":
54
        description: Under maintenance
55
        schema:
56
          $ref: ../definitions.yaml#/error
57
    x-koha-authorization:
58
      permissions:
59
          catalogue: "1"
2
"/biblios/{biblio_id}":
60
"/biblios/{biblio_id}":
3
  get:
61
  get:
4
    x-mojo-to: Biblios#get
62
    x-mojo-to: Biblios#get
(-)a/t/db_dependent/api/v1/biblios/post.t (-1 / +68 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 17;
6
use Test::Mojo;
7
8
use t::lib::Mocks;
9
use t::lib::TestBuilder;
10
11
my $schema  = Koha::Database->new->schema;
12
my $builder = t::lib::TestBuilder->new;
13
14
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
15
16
my $t = Test::Mojo->new('Koha::REST::V1');
17
18
$schema->storage->txn_begin;
19
20
my $patron = $builder->build_object(
21
    {
22
        class => 'Koha::Patrons',
23
        value => { flags => 0 }
24
    }
25
);
26
my $password = 'thePassword123';
27
$patron->set_password( { password => $password, skip_validation => 1 } );
28
my $userid = $patron->userid;
29
30
my $biblio = $builder->build_sample_biblio({
31
    title  => 'The unbearable lightness of being',
32
    author => 'Milan Kundera'
33
});
34
35
my $url = "//$userid:$password@/api/v1/biblios";
36
my $marcxml = $biblio->metadata->record->as_xml_record;
37
38
$t->post_ok($url, json => { marcxml => $marcxml })
39
  ->status_is(403);
40
41
$patron->flags(4)->store;
42
43
$t->post_ok($url, { Accept => 'application/weird+format' }, json => { marcxml => $marcxml })
44
  ->status_is(406)
45
  ->json_is( [ "application/json",
46
               "application/marcxml+xml",
47
               "application/marc-in-json",
48
               "application/marc",
49
               "text/plain" ] );
50
51
$t->post_ok($url, { Accept => 'application/json' }, json => { marcxml => $marcxml })
52
  ->status_is(200)
53
  ->json_is( '/title', 'The unbearable lightness of being' )
54
  ->json_is( '/author', 'Milan Kundera' );
55
56
$t->post_ok($url, { Accept => 'application/marcxml+xml' }, json => { marcxml => $marcxml })
57
  ->status_is(200);
58
59
$t->post_ok($url, { Accept => 'application/marc-in-json' }, json => { marcxml => $marcxml })
60
  ->status_is(200);
61
62
$t->post_ok($url, { Accept => 'application/marc' }, json => { marcxml => $marcxml })
63
  ->status_is(200);
64
65
$t->post_ok($url, { Accept => 'text/plain' }, json => { marcxml => $marcxml })
66
  ->status_is(200);
67
68
$schema->storage->txn_rollback;

Return to bug 28201