Bug 17501: Use Koha::Object in Koha::Upload::_delete
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Mon, 21 Nov 2016 13:12:17 +0000 (14:12 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 20 Jan 2017 14:20:04 +0000 (14:20 +0000)
Note: This is the last occurrence where we use DBI to perform a CRUD
operation. In this case a delete from uploaded_files.

We now call Koha::UploadedFile[s]->delete to only delete the record
from the table. A next step will be moving the additional functionality
of removing the file(s) too.

Test plan:
[1] Run t/db_dependent/Upload.t
[2] Delete an upload from tools/upload.pl

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Upload.pm

index f30de0e..4f4f5c5 100644 (file)
@@ -202,15 +202,7 @@ sub get {
 
 sub delete {
     my ( $self, $params ) = @_;
-    return if !$params->{id};
-    my @res;
-    my $temp = $self->_lookup({ id => $params->{id} });
-    foreach( @$temp ) {
-        my $d = $self->_delete( $_ );
-        push @res, $d if $d;
-    }
-    return if !@res;
-    return @res;
+    return $self->_delete( $params->{id} );
 }
 
 =head1 CLASS METHODS
@@ -411,17 +403,23 @@ sub _lookup {
 }
 
 sub _delete {
-    my ( $self, $rec ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sql = 'DELETE FROM uploaded_files WHERE id=?';
-    my $file = $self->_full_fname($rec);
+    my ( $self, $id ) = @_;
+    my $rec = Koha::UploadedFiles->find($id) || return;
+    my $filename = $rec->filename;
+    my $file = $self->_full_fname({
+        permanent => $rec->permanent,
+        dir       => $rec->dir,
+        hashvalue => $rec->hashvalue,
+        filename  => $filename,
+    });
+
     if( !-e $file ) { # we will just delete the record
         # TODO Should we add a trace here for the missing file?
-        $dbh->do( $sql, undef, ( $rec->{id} ) );
-        return $rec->{filename};
+        $rec->delete;
+        return $filename;
     } elsif( unlink($file) ) {
-        $dbh->do( $sql, undef, ( $rec->{id} ) );
-        return $rec->{filename};
+        $rec->delete;
+        return $filename;
     }
     $self->{files}->{ $rec->{filename} }->{errcode} = 7;
     #NOTE: errcode=6 is used to report successful delete (see template)