Skip to content

Commit

Permalink
♻️ refactor: Dto 수정에 따른 영양성분 합 조회 코드 수정 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Oct 11, 2023
1 parent ea0adcb commit ce61bdd
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,25 @@ public void deleteFavoriteFood(Long favoriteFoodId) {
// 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) {
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date);
return calculateNutritionSumAndRatio(userId, foodList);
return calculateNutritionSumAndRatio(userId, foodList, date, 1);
}

@Transactional(readOnly = true)
// 유저의 최근 7일간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) {
LocalDate endDate = LocalDate.now();
LocalDate startDate = endDate.minusDays(6);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate);

return calculateNutritionSumAndRatio(userId, foodList);
return calculateNutritionSumAndRatio(userId, foodList, endDate, 7);
}

@Transactional(readOnly = true)
// 유저의 최근 1개월간의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) {
LocalDate endDate = LocalDate.now();
LocalDate startDate = endDate.minusMonths(1);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate);

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

@Transactional(readOnly = true)
Expand Down Expand Up @@ -169,7 +167,7 @@ private FavoriteFood getFavoriteFoodById(Long foodId){
.orElseThrow(() -> new FoodException(ResponseCode.FOOD_NOT_FOUND));
}

private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, List<Food> foodList){
private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, List<Food> foodList, LocalDate checkDate, int nutritionSumType){
User targetUser = getUserById(userId);
int totalKcal = 0;
int totalCarbohydrate = 0;
Expand All @@ -189,7 +187,7 @@ private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId,
double ratioProtein = Math.round((((double) totalProtein /(double) targetUser.getBaseNutrition().getProtein())*100.0)*10.0)/10.0;
double ratioFat =Math.round((((double) totalFat /(double) targetUser.getBaseNutrition().getFat())*100.0)*10.0)/10.0;

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


Expand Down

0 comments on commit ce61bdd

Please sign in to comment.