From 0093c33c2d463fbbc5eed5c12a3bef6b75900553 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 14 Dec 2023 11:08:37 +0100 Subject: [PATCH] Bug 33036: Improve REST API controller Signed-off-by: Jonathan Druart Signed-off-by: Katrin Fischer (cherry picked from commit 8a9bf27dc11ee12a882cace6443120e9f294d0b2) Signed-off-by: Fridolin Somers --- Koha/REST/V1/Biblios.pm | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index ec174d4f80..0f66754f83 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -907,13 +907,9 @@ sub merge { my $ref_biblionumber = $c->param('biblio_id'); my $json = decode_json( $c->req->body ); my $bn_merge = $json->{'biblio_id_to_merge'}; - my $framework = $json->{'framework_to_use'}; - my $rules = $json->{'rules'}; - my $override_rec = $json->{'datarecord'}; - - if ( ( !defined $rules ) || ( $rules eq '' ) ) { $rules = 'override'; } - if ( ( !defined $override_rec ) ) { $override_rec = ''; } - if ( ( !defined $framework ) ) { $framework = ''; } + my $framework = $json->{'framework_to_use'} // q{}; + my $rules = $json->{'rules'} || q{override}; + my $override_rec = $json->{'datarecord'} // q{}; my $biblio = Koha::Biblios->find($ref_biblionumber); if ( not defined $biblio ) { @@ -947,25 +943,17 @@ sub merge { ); } - my $results; - eval { + return try { if ( $rules eq 'override_ext' ) { my $record = MARC::Record::MiJ->new_from_mij_structure($override_rec); $record->encoding('UTF-8'); - if ( $framework eq '' ) { $framework = $biblio->frameworkcode; } + $framework ||= $biblio->frameworkcode; my $chk = ModBiblio( $record, $ref_biblionumber, $framework ); if ( $chk != 1 ) { die "Error on ModBiblio"; } # ModBiblio returns 1 if everything as gone well - my @biblio_ids_to_merge = ($bn_merge); - $results = $biblio->merge_with( \@biblio_ids_to_merge ); - } - if ( $rules eq 'override' ) { - my @biblio_ids_to_merge = ($bn_merge); - $results = $biblio->merge_with( \@biblio_ids_to_merge ); } - }; - if ($@) { - return $c->render( status => 400, json => { error => $@ } ); - } else { + + $biblio->merge_with( [$bn_merge] ); + $c->respond_to( mij => { status => 200, @@ -973,7 +961,9 @@ sub merge { data => $biblio->metadata->record->to_mij } ); - } + } catch { + $c->render( status => 400, json => { error => $@ } ); + }; } 1; -- 2.39.5