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

(-)a/Koha/REST/V1/Authorities.pm (-35 / +32 lines)
Lines 138-144 sub add { Link Here
138
    my $c = shift->openapi->valid_input or return;
138
    my $c = shift->openapi->valid_input or return;
139
139
140
    try {
140
    try {
141
        my $body = $c->validation->param('Body');
141
        my $headers   = $c->req->headers;
142
        my $overrides = $c->stash('koha.overrides');
142
143
143
        my $flavour =
144
        my $flavour =
144
            C4::Context->preference('marcflavour') eq 'UNIMARC'
145
            C4::Context->preference('marcflavour') eq 'UNIMARC'
Lines 146-194 sub add { Link Here
146
            : 'MARC21';
147
            : 'MARC21';
147
148
148
        my $record;
149
        my $record;
149
        my $authtypecode;
150
150
151
        if ( $c->req->headers->content_type =~ m/application\/json/ ) {
151
        my $authtypecode = $headers->header('x-authority-type');
152
            $record = MARC::Record->new_from_xml( $body->{marcxml}, 'UTF-8', $flavour );
152
        my $content_type = $headers->content_type;
153
            $authtypecode = $body->{authtypecode};
153
154
        if ( $content_type =~ m/application\/marcxml\+xml/ ) {
155
            $record = MARC::Record->new_from_xml( $c->req->body, 'UTF-8', $flavour );
156
        } elsif ( $content_type =~ m/application\/marc-in-json/ ) {
157
            $record = MARC::Record->new_from_mij_structure( $c->req->json );
158
        } elsif ( $content_type =~ m/application\/marc/ ) {
159
            $record = MARC::Record->new_from_usmarc( $c->req->body );
154
        } else {
160
        } else {
155
            $authtypecode = $c->validation->param('x-authority-type');
161
            return $c->render(
156
            if ( $c->req->headers->content_type =~ m/application\/marcxml\+xml/ ) {
162
                status  => 406,
157
                $record = MARC::Record->new_from_xml( $body, 'UTF-8', $flavour );
163
                openapi => [
158
            } elsif ( $c->req->headers->content_type =~ m/application\/marc-in-json/ ) {
164
                    "application/marcxml+xml",
159
                $record = MARC::Record->new_from_mij_structure( $body );
165
                    "application/marc-in-json",
160
            } elsif ( $c->req->headers->content_type =~ m/application\/marc/ ) {
166
                    "application/marc"
161
                $record = MARC::Record->new_from_usmarc( $body );
167
                ]
162
            } else {
168
            );
163
                return $c->render(
164
                    status  => 406,
165
                    openapi => [
166
                        "application/json",
167
                        "application/marcxml+xml",
168
                        "application/marc-in-json",
169
                        "application/marc"
170
                    ]
171
                );
172
            }
173
        }
169
        }
174
170
175
        my ($duplicateauthid,$duplicateauthvalue);
171
        unless ( $overrides->{any} || $overrides->{duplicate} ) {
176
            ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority($record,$authtypecode);
172
            my ( $duplicateauthid, $duplicateauthvalue ) = FindDuplicateAuthority( $record, $authtypecode );
177
178
        my $confirm_not_duplicate = $c->validation->param('x-confirm-not-duplicate');
179
173
180
        return $c->render(
174
            return $c->render(
181
            status  => 400,
175
                status  => 409,
182
            openapi => {
176
                openapi => {
183
                error => "Duplicate authority $duplicateauthid"
177
                    error      => "Duplicate record ($duplicateauthid)",
184
            }
178
                    error_code => 'duplicate',
185
        ) unless !$duplicateauthid || $confirm_not_duplicate;
179
                }
180
            ) unless !$duplicateauthid;
181
        }
186
182
187
        my $authid = AddAuthority( $record, undef, $authtypecode );
183
        my $authid = AddAuthority( $record, undef, $authtypecode );
188
184
185
        $c->res->headers->location($c->req->url->to_string . '/' . $authid);
189
        $c->render(
186
        $c->render(
190
            status  => 200,
187
            status  => 201,
191
            openapi => { id => $authid }
188
            openapi => q{},
192
        );
189
        );
193
    }
190
    }
194
    catch {
191
    catch {
(-)a/api/v1/swagger/paths/authorities.yaml (-13 / +32 lines)
Lines 6-29 Link Here
6
    tags:
6
    tags:
7
      - authorities
7
      - authorities
8
    summary: Add authority
8
    summary: Add authority
9
    description: |
10
      Add an authority record to Koha. An optional `x-authority-type`
11
      may be passed to specify the cataloguing framework to be used (instead
12
      of the default).
13
14
      The request body is expected to contain a MARC record in the format specified in
15
      the `Content-type` header you pass. Possible values for this header and the corresponding
16
      record formats expected are listed below:
17
18
      * application/marcxml+xml: MARCXML
19
      * application/marc-in-json: MARC-in-JSON
20
      * application/marc: Raw USMARC binary data
9
    parameters:
21
    parameters:
10
      - name: Body
11
        in: body
12
        description: A JSON object or the Marc string describing an authority
13
        required: true
14
        schema:
15
          type:
16
            - string
17
            - object
18
      - $ref: "../swagger.yaml#/parameters/authority_type_header"
22
      - $ref: "../swagger.yaml#/parameters/authority_type_header"
19
      - $ref: "../swagger.yaml#/parameters/confirm_not_duplicate_header"
23
      - name: x-koha-override
20
    produces:
24
        in: header
21
      - application/json
25
        required: false
26
        description: Overrides list sent as a request header
27
        type: array
28
        items:
29
          type: string
30
          enum:
31
            - any
32
            - duplicate
33
        collectionFormat: csv
22
    responses:
34
    responses:
23
      "200":
35
      "201":
24
        description: An authority
36
        description: An authority
25
      "400":
37
      "400":
26
        description: Bad request
38
        description: Bad request.
27
        schema:
39
        schema:
28
          $ref: "../swagger.yaml#/definitions/error"
40
          $ref: "../swagger.yaml#/definitions/error"
29
      "401":
41
      "401":
Lines 41-46 Link Here
41
          description: Accepted content-types
53
          description: Accepted content-types
42
          items:
54
          items:
43
            type: string
55
            type: string
56
      "409":
57
        description: |
58
          Conflict creating the resource. Possible `error_code` attribute values:
59
60
          * `duplicate`
61
        schema:
62
          $ref: "../swagger.yaml#/definitions/error"
44
      "500":
63
      "500":
45
        description: |
64
        description: |
46
          Internal server error. Possible `error_code` attribute values:
65
          Internal server error. Possible `error_code` attribute values:
(-)a/t/db_dependent/api/v1/authorities.t (-19 / +32 lines)
Lines 155-166 subtest 'delete() tests' => sub { Link Here
155
155
156
subtest 'post() tests' => sub {
156
subtest 'post() tests' => sub {
157
157
158
    plan tests => 14;
158
    plan tests => 19;
159
159
160
    $schema->storage->txn_begin;
160
    $schema->storage->txn_begin;
161
161
162
    Koha::Authorities->delete;
163
164
    my $patron = $builder->build_object(
162
    my $patron = $builder->build_object(
165
        {
163
        {
166
            class => 'Koha::Patrons',
164
            class => 'Koha::Patrons',
Lines 202-222 subtest 'post() tests' => sub { Link Here
202
        }
200
        }
203
    );
201
    );
204
202
205
    $t->post_ok("//$userid:$password@/api/v1/authorities" => json => $json)
203
    # x-koha-override passed to make sure it goes through
206
      ->status_is(200)
204
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'any' } => $marcxml)
207
      ->json_has('/id');
205
      ->status_is(201)
208
206
      ->json_is(q{})
209
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marcxml+xml', 'x-authority-type' => 'CORPO_NAME'} => $marcxml)
207
      ->header_like(
210
      ->status_is(200)
208
          Location => qr|^\/api\/v1\/authorities/\d*|,
211
      ->json_has('/id');
209
          'SWAGGER3.4.1'
212
210
      );
213
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME'} => $mij)
211
214
      ->status_is(200)
212
    # x-koha-override not passed to force block because duplicate
215
      ->json_has('/id');
213
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME' } => $mij)
216
214
      ->status_is(409)
217
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME'} => $marc)
215
      ->header_exists_not( 'Location', 'Location header is only set when the new resource is created' )
218
      ->status_is(200)
216
      ->json_like( '/error' => qr/Duplicate record (\d*)/ )
219
      ->json_has('/id');
217
      ->json_is( '/error_code' => q{duplicate} );
218
219
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc-in-json', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $mij)
220
      ->status_is(201)
221
      ->json_is(q{})
222
      ->header_like(
223
          Location => qr|^\/api\/v1\/authorities/\d*|,
224
          'SWAGGER3.4.1'
225
      );
226
227
    $t->post_ok("//$userid:$password@/api/v1/authorities" => {'Content-Type' => 'application/marc', 'x-authority-type' => 'CORPO_NAME', 'x-koha-override' => 'duplicate' } => $marc)
228
      ->status_is(201)
229
      ->json_is(q{})
230
      ->header_like(
231
          Location => qr|^\/api\/v1\/authorities/\d*|,
232
          'SWAGGER3.4.1'
233
      );
220
234
221
    $schema->storage->txn_rollback;
235
    $schema->storage->txn_rollback;
222
};
236
};
223
- 

Return to bug 31795