From 53965448223cf56116a98893631d97a47f0e349d Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Tue, 3 Jan 2023 12:12:39 -0300 Subject: [PATCH] Bug 31798: (follow-up) Tidy up and add duplicate barcode test --- 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(-) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 2cc70b7151..3e278fae52 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/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($_); } } diff --git a/api/v1/swagger/paths/biblios.yaml b/api/v1/swagger/paths/biblios.yaml index 5b2fa3b45d..4bfb9e8c87 100644 --- a/api/v1/swagger/paths/biblios.yaml +++ b/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: diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index 7dbbf859b9..f8a5592aaa 100755 --- a/t/db_dependent/api/v1/biblios.t +++ b/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; }; -- 2.25.1