From 09aa67dc8f7dac6f4692e487723a4171745690e3 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 22 Oct 2024 09:29:06 -0300 Subject: [PATCH] Bug 38224: (follow-up) Rewrite endpoint using new core methods Now we have core methods, we can use them to rewrite the controller. Behavior should remain unchanged, so to test: 1. Apply this patches 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/rpc/biblios.t => SUCCESS: tests pass! 3. Sign off :-D Signed-off-by: Nick Clemens --- Koha/Exceptions/Object.pm | 10 ++++ Koha/REST/V1/RPC/Biblios.pm | 95 +++++++++++++++---------------------- 2 files changed, 48 insertions(+), 57 deletions(-) diff --git a/Koha/Exceptions/Object.pm b/Koha/Exceptions/Object.pm index 4181675826c..1a01f7a7031 100644 --- a/Koha/Exceptions/Object.pm +++ b/Koha/Exceptions/Object.pm @@ -46,6 +46,11 @@ use Exception::Class ( isa => 'Koha::Exceptions::Object', description => "Invalid property", }, + 'Koha::Exceptions::Object::MissingMessage' => { + isa => 'Koha::Exceptions::Object', + description => "Expected message missing", + fields => ['type'] + }, 'Koha::Exceptions::Object::ReadOnlyProperty' => { isa => 'Koha::Exceptions::Object', description => "Change of readonly property attempted", @@ -116,6 +121,11 @@ Exception to be used when a foreign key constraint is broken. Exception to be used when an invalid class method has been invoked. +=head2 Koha::Exceptions::Object::MissingMessage + +Exception to be used when a message is expected to be added to the object +but it is not present. + =head2 Koha::Exceptions::Object::PropertyNotFound Exception to be used when an invalid object property has been requested. diff --git a/Koha/REST/V1/RPC/Biblios.pm b/Koha/REST/V1/RPC/Biblios.pm index 98a97fa7d1c..7c07abb6084 100644 --- a/Koha/REST/V1/RPC/Biblios.pm +++ b/Koha/REST/V1/RPC/Biblios.pm @@ -19,7 +19,10 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; -use Try::Tiny qw( catch try ); +use Scalar::Util qw( blessed ); +use Try::Tiny qw( catch try ); + +use Koha::Exceptions::Object; =head1 API @@ -39,74 +42,52 @@ sub populate_empty_callnumbers { return $c->render_resource_not_found("Bibliographic record") unless $biblio; - my $items = $biblio->items->search( - { - -or => [ - itemcallnumber => undef, - itemcallnumber => q{}, - ] - } - ); + my $filter = { + -or => [ + itemcallnumber => undef, + itemcallnumber => q{}, + ] + }; - $items = $items->search( { itemnumber => $c->param('item_id') } ) + $filter->{itemnumber} = $c->param('item_id') if $c->param('item_id'); return try { - my $cn_fields = C4::Context->preference('itemcallnumber'); - return $c->render( - status => 409, - openapi => { - error => "Callnumber fields not found", - error_code => 'missing_configuration', - } - ) unless $cn_fields; - - my $record = $biblio->record; - my $callnumber; + $biblio->populate_item_callnumbers($filter); - foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) { - my $callnumber_tag = substr( $callnumber_marc_field, 0, 3 ); - my $callnumber_subfields = substr( $callnumber_marc_field, 3 ); + my ($message) = + grep { $_->message eq 'populate_item_callnumbers' } @{ $biblio->object_messages }; - next unless $callnumber_tag && $callnumber_subfields; - - my $field = $record->field($callnumber_tag); - - next unless $field; - - $callnumber = $field->as_string( $callnumber_subfields, '' ); - last if $callnumber; - } - - return $c->render( - status => 409, - openapi => { - error => "Callnumber empty in bibliographic record", - error_code => 'callnumber_empty', - } - ) unless $callnumber; - - return $c->render( - status => 200, - openapi => { - updated_items_count => 0, - callnumber => $callnumber - }, - ) unless $items->count; - - my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } ); - my @modified_itemnumbers = @{ $res->{modified_itemnumbers} }; + Koha::Exceptions::Object::MissingMessage->throw( type => 'populate_item_callnumbers' ) + unless $message; return $c->render( status => 200, - openapi => { - updated_items_count => scalar @modified_itemnumbers, - callnumber => $callnumber, - modified_item_ids => \@modified_itemnumbers, - }, + openapi => $message->payload, ); + } catch { + if ( blessed $_ ) { + if ( ref($_) eq 'Koha::Exceptions::SysPref::NotSet' ) { + return $c->render( + status => 409, + openapi => { + error => "Callnumber fields not found", + error_code => 'missing_configuration', + } + ); + } elsif ( ref($_) eq 'Koha::Exceptions::Biblio::MissingField' ) { + return $c->render( + status => 409, + openapi => { + error => "Callnumber empty in bibliographic record", + error_code => 'callnumber_empty', + } + ); + } + } + $c->unhandled_exception($_); }; } -- 2.39.5