Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 빈 배열과 모든 값이 0인 응답 구분 #130

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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