Skip to content

Commit

Permalink
♻️ refactor: Builder 패턴 적용 (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Jan 6, 2024
1 parent 94a194d commit 254eec2
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/diareat/diareat/food/domain/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.diareat.diareat.user.domain.BaseNutrition;
import com.diareat.diareat.user.domain.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diareat.diareat.food.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -9,6 +10,7 @@
import java.util.Map;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseAnalysisDto { // 그래프 + 점수에 사용되는 DTO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.diareat.diareat.food.domain.FavoriteFood;
import com.diareat.diareat.user.domain.BaseNutrition;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseFavoriteFoodDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.diareat.diareat.food.domain.Food;
import com.diareat.diareat.user.domain.BaseNutrition;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalTime;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ResponseFoodDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.diareat.diareat.food.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseFoodRankDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.diareat.diareat.user.domain.BaseNutrition;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseNutritionSumByDateDto implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.diareat.diareat.food.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseScoreBestWorstDto { // 일기 분석 자세히보기에 사용되는 DTO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.diareat.diareat.food.domain.Food;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseSimpleFoodDto { // Best 3 and Worst 3에 사용될 객체
Expand Down
107 changes: 90 additions & 17 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ public List<ResponseFoodDto> getFoodListByDate(Long userId, LocalDate date){
List<Food> foodList = foodRepository.findAllByUserIdAndDateOrderByAddedTimeAsc(userId, date);
log.info(date.toString() + "의 "+ userId + "에게 조회된 음식 개수: " + foodList.size() + "개");
return foodList.stream()
.map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getBaseNutrition(), food.isFavorite(), food.getAddedTime().getHour(), food.getAddedTime().getMinute())).collect(Collectors.toList());
.map(food -> ResponseFoodDto.builder()
.foodId(food.getId())
.userId(food.getUser().getId())
.name(food.getName())
.baseNutrition(food.getBaseNutrition())
.favoriteChecked(food.isFavorite())
.hour(food.getAddedTime().getHour())
.minute(food.getAddedTime().getMinute())
.build())
.collect(Collectors.toList());
}

// 음식 정보 수정
Expand Down Expand Up @@ -101,8 +110,13 @@ public List<ResponseFavoriteFoodDto> getFavoriteFoodList(Long userId){
List<FavoriteFood> foodList = favoriteFoodRepository.findAllByUserId(userId);
log.info(userId + "의 즐겨찾기 음식 개수: " + foodList.size() + "개 조회 완료");
return foodList.stream()
.map(favoriteFood -> ResponseFavoriteFoodDto.of(favoriteFood.getId(),favoriteFood.getName(),
favoriteFood.getBaseNutrition(), favoriteFood.getCount())).collect(Collectors.toList());
.map(favoriteFood -> ResponseFavoriteFoodDto.builder()
.favoriteFoodId(favoriteFood.getId())
.name(favoriteFood.getName())
.baseNutrition(favoriteFood.getBaseNutrition())
.count(favoriteFood.getCount())
.build())
.collect(Collectors.toList());
}

// 즐겨찾기 음식 수정
Expand Down Expand Up @@ -171,10 +185,22 @@ public ResponseFoodRankDto getBestFoodByWeek(Long userId, int year, int month, i
// ** Best 3 기준 논의 필요 **

List<ResponseSimpleFoodDto> top3FoodsDtoList = top3Foods.stream()
.map(food -> ResponseSimpleFoodDto.of(food.getName(), food.getBaseNutrition().getKcal(), food.getBaseNutrition().getCarbohydrate(),
food.getBaseNutrition().getProtein(), food.getBaseNutrition().getFat(), food.getDate())).collect(Collectors.toList());

return ResponseFoodRankDto.of(userId, top3FoodsDtoList, endDate, true);
.map(food -> ResponseSimpleFoodDto.builder()
.name(food.getName())
.calorie(food.getBaseNutrition().getKcal())
.carbohydrate(food.getBaseNutrition().getCarbohydrate())
.protein(food.getBaseNutrition().getProtein())
.fat(food.getBaseNutrition().getFat())
.date(food.getDate())
.build())
.collect(Collectors.toList());

return ResponseFoodRankDto.builder()
.userId(userId)
.rankFoodList(top3FoodsDtoList)
.startDate(endDate.minusWeeks(1))
.isBest(true)
.build();
}

@Transactional(readOnly = true)
Expand All @@ -195,10 +221,22 @@ public ResponseFoodRankDto getWorstFoodByWeek(Long userId, int year, int month,
// 우선 임시로 지방 비율을 높게 설정

List<ResponseSimpleFoodDto> worst3FoodDtoList = worst3Foods.stream()
.map(food -> ResponseSimpleFoodDto.of(food.getName(), food.getBaseNutrition().getKcal(), food.getBaseNutrition().getCarbohydrate(),
food.getBaseNutrition().getProtein(), food.getBaseNutrition().getFat(), food.getDate())).collect(Collectors.toList());

return ResponseFoodRankDto.of(userId, worst3FoodDtoList, endDate, false);
.map(food -> ResponseSimpleFoodDto.builder()
.name(food.getName())
.calorie(food.getBaseNutrition().getKcal())
.carbohydrate(food.getBaseNutrition().getCarbohydrate())
.protein(food.getBaseNutrition().getProtein())
.fat(food.getBaseNutrition().getFat())
.date(food.getDate())
.build())
.collect(Collectors.toList());

return ResponseFoodRankDto.builder()
.userId(userId)
.rankFoodList(worst3FoodDtoList)
.startDate(endDate.minusWeeks(1))
.isBest(false)
.build();
}

// 잔여 기능 구현 부분
Expand Down Expand Up @@ -228,7 +266,14 @@ public ResponseScoreBestWorstDto getScoreOfUserWithBestAndWorstFoods(Long userId
List<ResponseSimpleFoodDto> simpleBestFoodList = getBestFoodByWeek(userId, year, month, day).getRankFoodList();
List<ResponseSimpleFoodDto> simpleWorstFoodList = getWorstFoodByWeek(userId, year, month, day).getRankFoodList();

return ResponseScoreBestWorstDto.of(kcalScore, carbohydrateScore, proteinScore, fatScore, totalScore, simpleBestFoodList, simpleWorstFoodList);
return ResponseScoreBestWorstDto.builder()
.totalScore(totalScore)
.carbohydrateScore(carbohydrateScore)
.proteinScore(proteinScore)
.fatScore(fatScore)
.best(simpleBestFoodList)
.worst(simpleWorstFoodList)
.build();
}


Expand Down Expand Up @@ -300,7 +345,17 @@ public ResponseAnalysisDto getAnalysisOfUser(Long userId, int year, int month, i

totalWeekScore = Math.round(totalWeekScore * 100.0) / 100.0;

return ResponseAnalysisDto.of(totalWeekScore, calorieLastSevenDays, calorieLastFourWeek, carbohydrateLastSevenDays, carbohydrateLastFourWeek, proteinLastSevenDays, proteinLastFourWeek, fatLastSevenDays, fatLastFourWeek);
return ResponseAnalysisDto.builder()
.totalScore(totalWeekScore)
.calorieLastSevenDays(calorieLastSevenDays)
.calorieLastFourWeek(calorieLastFourWeek)
.carbohydrateLastSevenDays(carbohydrateLastSevenDays)
.carbohydrateLastFourWeek(carbohydrateLastFourWeek)
.proteinLastSevenDays(proteinLastSevenDays)
.proteinLastFourWeek(proteinLastFourWeek)
.fatLastSevenDays(fatLastSevenDays)
.fatLastFourWeek(fatLastFourWeek)
.build();
}


Expand Down Expand Up @@ -374,7 +429,14 @@ private ResponseRankUserDto calculateUserScoreThisWeek(User targetUser, LocalDat
kcalScore += calculateNutriRatioAndScore(totalKcal, targetUser.getBaseNutrition().getKcal(), 1);
}
totalScore = (kcalScore + carbohydrateScore + proteinScore + fatScore);
return ResponseRankUserDto.of(targetUser.getId(), targetUser.getName(), targetUser.getImage(), kcalScore, carbohydrateScore, proteinScore, fatScore, totalScore);
return ResponseRankUserDto.builder()
.userId(targetUser.getId())
.totalScore(totalScore)
.calorieScore(kcalScore)
.carbohydrateScore(carbohydrateScore)
.proteinScore(proteinScore)
.fatScore(fatScore)
.build();
}

private ResponseNutritionSumByDateDto calculateNutritionSumAndRatio(Long userId, List<Food> foodList, LocalDate checkDate, int nutritionSumType,
Expand All @@ -398,9 +460,20 @@ 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(userId, checkDate, nutritionSumType, totalKcal,
totalCarbohydrate, totalProtein, totalFat, ratioKcal,
ratioCarbohydrate, ratioProtein, ratioFat, targetUser.getBaseNutrition(), isEmpty);
return ResponseNutritionSumByDateDto.builder()
.userId(userId)
.nutritionSumType(nutritionSumType)
.totalKcal(totalKcal)
.totalCarbohydrate(totalCarbohydrate)
.totalProtein(totalProtein)
.totalFat(totalFat)
.ratioKcal(ratioKcal)
.ratioCarbohydrate(ratioCarbohydrate)
.ratioProtein(ratioProtein)
.ratioFat(ratioFat)
.baseNutrition(targetUser.getBaseNutrition())
.isEmpty(isEmpty)
.build();
}

private void validateUser(Long userId) {
Expand Down

0 comments on commit 254eec2

Please sign in to comment.