From 141c7cdf1a20e3758a76744b0d20283b3cb149f5 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 Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- 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 ab70dcc88f..f1198b246a 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -314,24 +314,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 08e9a476ac..1506d019aa 100644 --- a/api/v1/swagger/paths/biblios.yaml +++ b/api/v1/swagger/paths/biblios.yaml @@ -437,6 +437,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 8b02dad0f4..a28885f2df 100644 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -1690,7 +1690,7 @@ subtest 'list() tests' => sub { }; subtest 'add_item() tests' => sub { - plan tests => 5; + plan tests => 7; $schema->storage->txn_begin; @@ -1735,5 +1735,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.39.5