@@ -, +, @@ --- Koha/REST/V1/Biblios.pm | 20 ++++++++------------ api/v1/swagger/paths/biblios.yaml | 4 ++++ t/db_dependent/api/v1/biblios.t | 9 ++++++++- 3 files changed, 20 insertions(+), 13 deletions(-) --- a/Koha/REST/V1/Biblios.pm +++ a/Koha/REST/V1/Biblios.pm @@ -310,24 +310,20 @@ sub add_item { $item->barcode($barcode); } - if ( defined $item->barcode - && Koha::Items->search( { barcode => $item->barcode } )->count ) - { - return $c->render( - status => 400, - openapi => { error => "Barcode not unique" } - ); - } - - my $storedItem = $item->store; - $storedItem->discard_changes; + $item->store->discard_changes; $c->render( status => 201, - openapi => $storedItem->to_api + openapi => $item->to_api ); } catch { + if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + return $c->render( + status => 409, + openapi => { error => 'Duplicate barcode.' } + ); + } $c->unhandled_exception($_); } } --- a/api/v1/swagger/paths/biblios.yaml +++ a/api/v1/swagger/paths/biblios.yaml @@ -254,6 +254,10 @@ description: Not found schema: $ref: "../swagger.yaml#/definitions/error" + "409": + description: Conflict + schema: + $ref: "../swagger.yaml#/definitions/error" "500": description: | Internal server error. Possible `error_code` attribute values: --- a/t/db_dependent/api/v1/biblios.t +++ a/t/db_dependent/api/v1/biblios.t @@ -715,7 +715,7 @@ subtest 'set_rating() tests' => sub { }; subtest 'add_item() tests' => sub { - plan tests => 5; + plan tests => 7; $schema->storage->txn_begin; @@ -760,5 +760,12 @@ subtest 'add_item() tests' => sub { ->status_is(201, 'Item created') ->json_is('/biblio_id', $biblio_id); + my $item = $builder->build_sample_item(); + + $t->post_ok("//$userid:$password@/api/v1/biblios/$biblio_id/items" => json => { + external_id => $item->barcode, + }) + ->status_is(409, 'Duplicate barcode'); + $schema->storage->txn_rollback; }; --