Commit a86d9e81 authored by Daniel Puchala's avatar Daniel Puchala
Browse files

refactor: get average rating in review service impl

parent a5732d6f
......@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* @author Petr Šlézar
......@@ -90,20 +91,16 @@ public class ReviewServiceImpl extends GenericServiceImpl<Review> implements Rev
if (movie == null) throw new BadArgumentException("Movie is null");
List<Review> allReviewsForMovie = findByMovie(movie);
Double totalScore = Double.valueOf(
allReviewsForMovie.stream().map(review -> review.getActingRating() +
review.getIdeaRating() +
review.getMusicRating() +
review.getScriptRating() +
review.getVisualsEditRating()).reduce(0, Integer::sum)
);
Double divisor = Double.valueOf(allReviewsForMovie.size() * 5);
Double averageRating = 0d;
if (totalScore != 0) {
averageRating = totalScore / divisor;
averageRating = Precision.round(averageRating, 2);
}
return averageRating;
Optional<Integer> totalScore = allReviewsForMovie.stream()
.map(review -> review.getActingRating() +
review.getIdeaRating() +
review.getMusicRating() +
review.getScriptRating() +
review.getVisualsEditRating())
.reduce(Integer::sum);
var averageRating = totalScore
.map(total -> total / (allReviewsForMovie.size() * 5.0));
return averageRating.orElse(null);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment