Skip to content

Commit

Permalink
fix: 같은 주류 평점 및 리뷰 통합 시 리뷰 갯수 합치기
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Jun 6, 2023
1 parent 8c40685 commit a03f809
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions backend/src/main/java/com/WAT/BEJURYU/service/DrinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -92,11 +93,20 @@ public List<DrinkWithRatingResponse> findTop10ByRating() {
private List<DrinkWithRatingResponse> getDrinkWithRatingResponses() {
final List<Drink> drinks = drinkRepository.findAll();

return drinks.stream()
final Map<String, List<DrinkWithRatingResponse>> collected = drinks.stream()
.map(drink -> DrinkWithRatingResponse.from(drink,
reviewService.getAverageScore(drink.getId()),
reviewService.getReviewSize(drink.getId())))
.distinct()
.collect(Collectors.groupingBy(DrinkWithRatingResponse::getName));

return collected.values().stream()
.map(list -> {
final int reviewCount = list.stream().mapToInt(DrinkWithRatingResponse::getReviewCount).sum();
final double rating = list.stream().mapToDouble(DrinkWithRatingResponse::getRating).sum() / list.size();

final DrinkWithRatingResponse drink = list.get(0);
return new DrinkWithRatingResponse(drink.getId(), drink.getName(), drink.getType(), rating, reviewCount, drink.getImage());
})
.collect(Collectors.toList());
}

Expand Down

0 comments on commit a03f809

Please sign in to comment.