From 3f6ca625649ae070058dde8e63a837953bf671b9 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 18 Jul 2014 10:42:03 -0400 Subject: [PATCH] Bug 12609 - Replace use of DBI in C4::Ratings with DBIx::Class Test Plan: 1) Apply this patch 2) prove t/db_dependent/Ratings.t 3) Note all unit tests pass --- C4/Ratings.pm | 85 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 50 insertions(+), 35 deletions(-) diff --git a/C4/Ratings.pm b/C4/Ratings.pm index b9ae7de..10cb4bf 100644 --- a/C4/Ratings.pm +++ b/C4/Ratings.pm @@ -147,41 +147,44 @@ A hashref containing: sub GetRating { my ( $biblionumber, $borrowernumber ) = @_; - my $query = qq| SELECT COUNT(*) AS total, SUM(rating_value) AS sum -FROM ratings WHERE biblionumber = ? |; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute($biblionumber); - my $res = $sth->fetchrow_hashref(); + my $ratings = Koha::Database->new()->schema->resultset('Rating')->search( + { + biblionumber => $biblionumber, + } + ); + + my $sum = $ratings->get_column('rating_value')->sum(); + my $total = $ratings->count(); my ( $avg, $avg_int ) = 0; - if ( $res->{sum} and $res->{total} ) { - eval { $avg = $res->{sum} / $res->{total} }; + if ( $sum and $total ) { + eval { $avg = $sum / $total }; } $avg_int = sprintf( "%.1f", $avg ); $avg = sprintf( "%.0f", $avg ); my %rating_hash; - $rating_hash{rating_total} = $res->{total} || 0; + $rating_hash{rating_total} = $total; $rating_hash{rating_avg} = $avg || 0; $rating_hash{rating_avg_int} = $avg_int ||0; if ($borrowernumber) { - my $q2 = qq| -SELECT rating_value FROM ratings WHERE biblionumber = ? AND borrowernumber = ?|; - my $sth1 = C4::Context->dbh->prepare($q2); - $sth1->execute( $biblionumber, $borrowernumber ); - my $res1 = $sth1->fetchrow_hashref(); - $rating_hash{'rating_value'} = $res1->{"rating_value"}; + my $rating = Koha::Database->new()->schema->resultset('Rating')->find( + { + biblionumber => $biblionumber, + borrowernumber => $borrowernumber, + } + ); + $rating_hash{'rating_value'} = $rating->rating_value(); } else { $rating_hash{rating_borrowernumber} = undef; $rating_hash{rating_value} = undef; } -#### %rating_hash return \%rating_hash; } @@ -199,13 +202,16 @@ is 0, then the rating will be deleted. If the value is out of the range of sub AddRating { my ( $biblionumber, $borrowernumber, $rating_value ) = @_; - my $query = - qq| INSERT INTO ratings (borrowernumber,biblionumber,rating_value) - VALUES (?,?,?)|; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute( $borrowernumber, $biblionumber, $rating_value ); - my $rating = GetRating( $biblionumber, $borrowernumber ); - return $rating; + + my $rating = Koha::Database->new()->schema->resultset('Rating')->create( + { + biblionumber => $biblionumber, + borrowernumber => $borrowernumber, + rating_value => $rating_value + } + ); + + return GetRating( $biblionumber, $borrowernumber ); } =head2 ModRating @@ -218,12 +224,17 @@ Mod a rating for a bib sub ModRating { my ( $biblionumber, $borrowernumber, $rating_value ) = @_; - my $query = -qq|UPDATE ratings SET rating_value = ? WHERE borrowernumber = ? AND biblionumber = ?|; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute( $rating_value, $borrowernumber, $biblionumber ); - my $rating = GetRating( $biblionumber, $borrowernumber ); - return $rating; + + my $rating = Koha::Database->new()->schema->resultset('Rating')->find( + { + borrowernumber => $borrowernumber, + biblionumber => $biblionumber + } + ); + + $rating->update( { rating_value => $rating_value } ); + + return GetRating( $biblionumber, $borrowernumber ); } =head2 DelRating @@ -236,13 +247,17 @@ Delete a rating for a bib sub DelRating { my ( $biblionumber, $borrowernumber ) = @_; - my $dbh = C4::Context->dbh; - my $query = - "delete from ratings where borrowernumber = ? and biblionumber = ?"; - my $sth = C4::Context->dbh->prepare($query); - my $rv = $sth->execute( $borrowernumber, $biblionumber ); - my $rating = GetRating( $biblionumber, undef ); - return $rating; + + my $rating = Koha::Database->new()->schema->resultset('Rating')->find( + { + borrowernumber => $borrowernumber, + biblionumber => $biblionumber + } + ); + + $rating->delete() if $rating; + + return GetRating($biblionumber); } 1; -- 1.7.2.5