From f96b727400ae5ab2a41ea41a059927497a069ffb Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Wed, 21 Apr 2021 07:56:50 +0200
Subject: [PATCH] Bug 28186: Use Koha::Authority in
 C4::AuthoritiesMarc::AddAuthority
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch replaces the use of $dbh->do by corresponding use of
Koha::Authority object

Test plan:
1. Verify that creating/modifying authority still work
2. prove t/db_dependent/AuthoritiesMarc.t

Signed-off-by: Joonas Kylmälä <joonas.kylmala@iki.fi>
---
 C4/AuthoritiesMarc.pm | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm
index e211af2b94..b417213742 100644
--- a/C4/AuthoritiesMarc.pm
+++ b/C4/AuthoritiesMarc.pm
@@ -643,20 +643,24 @@ sub AddAuthority {
 
     # Save record into auth_header, update 001
     my $action;
+    my $authority;
     if (!$authid ) {
         $action = 'create';
         # Save a blank record, get authid
-        $dbh->do( "INSERT INTO auth_header (datecreated,marcxml) values (NOW(),?)", undef, '' );
-        $authid = $dbh->last_insert_id( undef, undef, 'auth_header', 'authid' );
+        $authority = Koha::Authority->new({ datecreated => \'NOW()', marcxml => '' })->store();
+        $authority->discard_changes();
+        $authid = $authority->authid;
         logaction( "AUTHORITIES", "ADD", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog");
     } else {
         $action = 'modify';
+        $authority = Koha::Authorities->find($authid);
     }
+
     # Insert/update the recordID in MARC record
     $record->delete_field( $record->field('001') );
     $record->insert_fields_ordered( MARC::Field->new( '001', $authid ) );
     # Update
-    $dbh->do( "UPDATE auth_header SET authtypecode=?, marc=?, marcxml=? WHERE authid=?", undef, $authtypecode, $record->as_usmarc, $record->as_xml_record($format), $authid ) or die $DBI::errstr;
+    $authority->update({ authtypecode => $authtypecode, marc => $record->as_usmarc, marcxml => $record->as_xml_record($format) });
 
     unless ( $skip_record_index ) {
         my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
-- 
2.30.2