Skip to content

Commit

Permalink
♻️ refactor: JPA에서 addedTime 기준으로 정렬 (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
synoti21 committed Nov 16, 2023
1 parent da35321 commit 4692468
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public interface FoodRepository extends JpaRepository<Food, Long> {
boolean existsByIdAndUserId(Long id, Long userId); // 유저가 먹은 음식인지 확인
boolean existsByName(String name);
List<Food> findAllByUserIdAndDate(Long userId, LocalDate date, Sort sort); //유저가 특정 날짜에 먹은 음식 반환
List<Food> findAllByUserIdAndDateBetween(Long userId, LocalDate startDate, LocalDate endDate, Sort sort); // 유저가 특정 기간 내에 먹은 음식 반환
List<Food> findAllByUserIdAndDateOrderByAddedTimeAsc(Long userId, LocalDate date); //유저가 특정 날짜에 먹은 음식 반환
List<Food> findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(Long userId, LocalDate startDate, LocalDate endDate); // 유저가 특정 기간 내에 먹은 음식 반환
}
21 changes: 7 additions & 14 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public Long saveFood(CreateFoodDto createFoodDto) {
@Transactional(readOnly = true)
public List<ResponseFoodDto> getFoodListByDate(Long userId, LocalDate date){
validateUser(userId);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateOrderByAddedTimeAsc(userId, date);
return foodList.stream()
.map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList());
}
Expand Down Expand Up @@ -123,8 +122,7 @@ public void deleteFavoriteFood(Long favoriteFoodId, Long userId) {
// 유저의 특정 날짜에 먹은 음식들의 영양성분별 총합 조회 (섭취영양소/기준영양소 및 비율까지 계산해서 반환, dto 구체적 협의 필요)
public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDate date) {
validateUser(userId);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateOrderByAddedTimeAsc(userId, date);
return calculateNutritionSumAndRatio(userId, foodList, date, 1);
}

Expand All @@ -133,8 +131,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDat
public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId, int year, int month, int day) {
validateUser(userId);
LocalDate endDate = LocalDate.of(year, month, day);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 7);
}
Expand All @@ -144,8 +141,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId, int year
public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId, int year, int month, int day) {
validateUser(userId);
LocalDate endDate = LocalDate.of(year, month, day);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusMonths(1), endDate, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusMonths(1), endDate);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 30);
}
Expand All @@ -155,8 +151,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId, int yea
public ResponseFoodRankDto getBestFoodByWeek(Long userId, int year, int month, int day) {
validateUser(userId);
LocalDate endDate = LocalDate.of(year, month, day);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate);

List<Food> top3Foods = foodList.stream()
.sorted(Comparator.comparingDouble((Food food) ->
Expand All @@ -178,8 +173,7 @@ public ResponseFoodRankDto getBestFoodByWeek(Long userId, int year, int month, i
public ResponseFoodRankDto getWorstFoodByWeek(Long userId, int year, int month, int day) {
validateUser(userId);
LocalDate endDate = LocalDate.of(year, month, day);
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate);

List<Food> worst3Foods = foodList.stream()
.sorted(Comparator.comparingDouble((Food food) ->
Expand Down Expand Up @@ -414,8 +408,7 @@ private void validateFavoriteFood(Long favoriteFoodId, Long userId) {
// 1주일동안 먹은 음식들의 영양성분 총합을 요일을 Key로 한 Map을 통해 반환
private HashMap<LocalDate, List<BaseNutrition>> getNutritionSumByDateMap(Long userId, LocalDate startDate, LocalDate endDate) {
HashMap<LocalDate, List<BaseNutrition>> maps = new HashMap<>();
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate, sort);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, startDate, endDate);
for (Food food : foodList) {
if (maps.containsKey(food.getDate())) {
maps.get(food.getDate()).add(food.getBaseNutrition());
Expand Down

0 comments on commit 4692468

Please sign in to comment.