Skip to content

Commit

Permalink
Merge pull request #130 from CAUSOLDOUTMEN/fix/129-food-is-empty
Browse files Browse the repository at this point in the history
Fix: 빈 배열과 모든 값이 0인 응답 구분
  • Loading branch information
win-luck authored Dec 2, 2023
2 parents c863a64 + 6ceceb6 commit 3889aef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public class ResponseNutritionSumByDateDto implements Serializable {

BaseNutrition baseNutrition;

boolean isEmpty;

public static ResponseNutritionSumByDateDto of (Long userId, LocalDate checkDate, int nutritionSumType, int totalKcal,
int totalCarbohydrate, int totalProtein, int totalFat, double ratioKcal,
double ratioCarbohydrate, double ratioProtein, double ratioFat,
BaseNutrition baseNutrition){
BaseNutrition baseNutrition, boolean isEmpty){
return new ResponseNutritionSumByDateDto(userId, nutritionSumType, totalKcal,
totalCarbohydrate, totalProtein, totalFat, ratioKcal,
ratioCarbohydrate, ratioProtein, ratioFat, baseNutrition);
ratioCarbohydrate, ratioProtein, ratioFat, baseNutrition, isEmpty);
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ void testGetNutritionSumByDate() throws Exception{
LocalDate date = LocalDate.of(2021,10,10);
ResponseNutritionSumByDateDto responseNutritionSumByDateDto = ResponseNutritionSumByDateDto.of(testUserId,date,1
,500,100,50,50,0.2,0.3,0.4,0.5,
BaseNutrition.createNutrition(500,100,50,50));
BaseNutrition.createNutrition(500,100,50,50), false);
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto,ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByDate(any(Long.class),any(LocalDate.class))).thenReturn(responseNutritionSumByDateDto);

Expand Down Expand Up @@ -315,7 +315,7 @@ void testGetNutritionSumByWeek() throws Exception {
LocalDate date = LocalDate.of(2021, 10, 10);
ResponseNutritionSumByDateDto responseNutritionSumByDateDto = ResponseNutritionSumByDateDto.of(testUserId, date, 7
, 500, 100, 50, 50, 0.2, 0.3, 0.4, 0.5,
BaseNutrition.createNutrition(500, 100, 50, 50));
BaseNutrition.createNutrition(500, 100, 50, 50), false);
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByWeek(any(Long.class), any(int.class), any(int.class), any(int.class))).thenReturn(responseNutritionSumByDateDto);

Expand Down Expand Up @@ -353,7 +353,7 @@ void testGetNutritionSumByMonth() throws Exception {
LocalDate date = LocalDate.of(2021, 10, 10);
ResponseNutritionSumByDateDto responseNutritionSumByDateDto = ResponseNutritionSumByDateDto.of(testUserId, date, 30
, 500, 100, 50, 50, 0.2, 0.3, 0.4, 0.5,
BaseNutrition.createNutrition(500, 100, 50, 50));
BaseNutrition.createNutrition(500, 100, 50, 50), false);
ApiResponse<ResponseNutritionSumByDateDto> expectedResponse = ApiResponse.success(responseNutritionSumByDateDto, ResponseCode.FOOD_READ_SUCCESS.getMessage());
when(foodService.getNutritionSumByMonth(any(Long.class), any(int.class), any(int.class), any(int.class))).thenReturn(responseNutritionSumByDateDto);

Expand Down

0 comments on commit 3889aef

Please sign in to comment.