Skip to content

Commit

Permalink
🐛 fix: 빈 배열일 경우 이를 알리도록 수정 (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Dec 2, 2023
1 parent 71ecda5 commit 08c6526
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDat
validateUser(userId);
List<Food> foodList = foodRepository.findAllByUserIdAndDateOrderByAddedTimeAsc(userId, date);
log.info(date.toString() + "기준 "+ userId + "의 음식들의 영양성분별 총합 조회 완료");
return calculateNutritionSumAndRatio(userId, foodList, date, 1);
return calculateNutritionSumAndRatio(userId, foodList, date, 1, foodList.isEmpty());
}

@Transactional(readOnly = true)
Expand All @@ -151,7 +151,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId, int year
validateUser(userId);
LocalDate endDate = LocalDate.of(year, month, day);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate);
return calculateNutritionSumAndRatio(userId, foodList, endDate, 7);
return calculateNutritionSumAndRatio(userId, foodList, endDate, 7, foodList.isEmpty());
}

@Transactional(readOnly = true)
Expand All @@ -161,7 +161,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId, int yea
LocalDate endDate = LocalDate.of(year, month, day);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusMonths(1), endDate);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 30);
return calculateNutritionSumAndRatio(userId, foodList, endDate, 30, foodList.isEmpty());
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -386,7 +386,8 @@ private ResponseRankUserDto calculateUserScoreThisWeek(User targetUser, LocalDat
return ResponseRankUserDto.of(targetUser.getId(), targetUser.getName(), targetUser.getImage(), kcalScore, carbohydrateScore, proteinScore, fatScore, totalScore);
}

private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, List<Food> foodList, LocalDate checkDate, int nutritionSumType) {
private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, List<Food> foodList, LocalDate checkDate, int nutritionSumType,
boolean isEmpty) {
User targetUser = getUserById(userId);
int totalKcal = 0;
int totalCarbohydrate = 0;
Expand All @@ -408,7 +409,7 @@ private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId,

return ResponseNutritionSumByDateDto.of(userId, checkDate, nutritionSumType, totalKcal,
totalCarbohydrate, totalProtein, totalFat, ratioKcal,
ratioCarbohydrate, ratioProtein, ratioFat, targetUser.getBaseNutrition());
ratioCarbohydrate, ratioProtein, ratioFat, targetUser.getBaseNutrition(), isEmpty);
}

private void validateUser(Long userId) {
Expand Down

0 comments on commit 08c6526

Please sign in to comment.