From f581395af9232b4722572853ea0f3530dee6ff8d Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Tue, 22 Jul 2014 12:08:43 +0200 Subject: [PATCH] Bug 12623: SQLHelper replacement - Borrower::Modifications With this patch, the subroutines AddModification and ApproveModifications uses DBIx::Class instead of C4::SQLHelper. Test plan: 1) Apply the patch 2) Execute the unit tests by launching : prove t/db_dependent/Koha_borrower_modifications.t 3) The result has to be a success without error or warning : t/db_dependent/Koha_borrower_modifications.t .. ok All tests successful. Files=1, Tests=14, 2 wallclock secs ( 0.03 usr 0.01 sys + 1.60 cusr 0.08 csys = 1.72 CPU) Result: PASS --- Koha/Borrower/Modifications.pm | 54 ++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/Koha/Borrower/Modifications.pm b/Koha/Borrower/Modifications.pm index 1d32d16..19f0bfa 100644 --- a/Koha/Borrower/Modifications.pm +++ b/Koha/Borrower/Modifications.pm @@ -26,7 +26,6 @@ use Modern::Perl; use C4::Context; use C4::Debug; -use C4::SQLHelper qw(InsertInTable UpdateInTable); sub new { my ( $class, %args ) = @_; @@ -48,42 +47,20 @@ to be part of the passed in hash. sub AddModifications { my ( $self, $data ) = @_; - if ( $self->{'borrowernumber'} ) { - delete $data->{'borrowernumber'}; - - if ( keys %$data ) { - $data->{'borrowernumber'} = $self->{'borrowernumber'}; - my $dbh = C4::Context->dbh; - - my $query = " - SELECT COUNT(*) AS count - FROM borrower_modifications - WHERE borrowernumber = ? - "; - - my $sth = $dbh->prepare($query); - $sth->execute( $self->{'borrowernumber'} ); - my $result = $sth->fetchrow_hashref(); - - if ( $result->{'count'} ) { - $data->{'verification_token'} = q{}; - return UpdateInTable( "borrower_modifications", $data ); - } - else { - return InsertInTable( "borrower_modifications", $data ); - } - } - + delete $data->{borrowernumber}; + if( $self->{borrowernumber} ) { + return if( not keys %$data ); + $data->{borrowernumber} = $self->{borrowernumber}; } - elsif ( $self->{'verification_token'} ) { - delete $data->{'borrowernumber'}; - $data->{'verification_token'} = $self->{'verification_token'}; - - return InsertInTable( "borrower_modifications", $data ); + elsif( $self->{verification_token} ) { + $data->{verification_token} = $self->{verification_token}; } else { return; } + + my $rs = Koha::Database->new()->schema->resultset('BorrowerModification'); + return $rs->update_or_create($data); } =head2 Verify @@ -120,6 +97,7 @@ sub Verify { $count = Koha::Borrower::Modifications->GetPendingModificationsCount(); Returns the number of pending modifications for existing borrowers. + =cut sub GetPendingModificationsCount { @@ -203,10 +181,14 @@ sub ApproveModifications { return unless $borrowernumber; - my $data = $self->GetModifications({ borrowernumber => $borrowernumber }); + my $data = $self->GetModifications( { borrowernumber => $borrowernumber } ); + delete $data->{timestamp}; + delete $data->{verification_token}; - if ( UpdateInTable( "borrowers", $data ) ) { - $self->DelModifications({ borrowernumber => $borrowernumber }); + my $borrower = Koha::Database->new()->schema->resultset('Borrower')->find($borrowernumber); + return unless($borrower); + if( $borrower->update($data) ) { + $self->DelModifications( { borrowernumber => $borrowernumber } ); } } @@ -227,7 +209,7 @@ sub DenyModifications { return unless $borrowernumber; - return $self->DelModifications({ borrowernumber => $borrowernumber }); + return $self->DelModifications( { borrowernumber => $borrowernumber } ); } =head2 DelModifications -- 1.9.1