From 46924684a51902b79b19fc03541672621134863c Mon Sep 17 00:00:00 2001 From: Ahn Jiwan Date: Thu, 16 Nov 2023 22:47:51 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20refactor:=20JPA=EC=97=90=EC=84=9C?= =?UTF-8?q?=20addedTime=20=EA=B8=B0=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20(#102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../food/repository/FoodRepository.java | 4 ++-- .../diareat/food/service/FoodService.java | 21 +++++++------------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java b/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java index d11d496..db42ed6 100644 --- a/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java +++ b/src/main/java/com/diareat/diareat/food/repository/FoodRepository.java @@ -10,6 +10,6 @@ public interface FoodRepository extends JpaRepository { boolean existsByIdAndUserId(Long id, Long userId); // 유저가 먹은 음식인지 확인 boolean existsByName(String name); - List findAllByUserIdAndDate(Long userId, LocalDate date, Sort sort); //유저가 특정 날짜에 먹은 음식 반환 - List findAllByUserIdAndDateBetween(Long userId, LocalDate startDate, LocalDate endDate, Sort sort); // 유저가 특정 기간 내에 먹은 음식 반환 + List findAllByUserIdAndDateOrderByAddedTimeAsc(Long userId, LocalDate date); //유저가 특정 날짜에 먹은 음식 반환 + List findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(Long userId, LocalDate startDate, LocalDate endDate); // 유저가 특정 기간 내에 먹은 음식 반환 } diff --git a/src/main/java/com/diareat/diareat/food/service/FoodService.java b/src/main/java/com/diareat/diareat/food/service/FoodService.java index 61d5c58..63a6395 100644 --- a/src/main/java/com/diareat/diareat/food/service/FoodService.java +++ b/src/main/java/com/diareat/diareat/food/service/FoodService.java @@ -52,8 +52,7 @@ public Long saveFood(CreateFoodDto createFoodDto) { @Transactional(readOnly = true) public List getFoodListByDate(Long userId, LocalDate date){ validateUser(userId); - Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP); - List foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort); + List 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()); } @@ -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 foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort); + List foodList = foodRepository.findAllByUserIdAndDateOrderByAddedTimeAsc(userId, date); return calculateNutritionSumAndRatio(userId, foodList, date, 1); } @@ -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 foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort); + List foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate); return calculateNutritionSumAndRatio(userId, foodList, endDate, 7); } @@ -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 foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusMonths(1), endDate, sort); + List foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusMonths(1), endDate); return calculateNutritionSumAndRatio(userId, foodList, endDate, 30); } @@ -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 foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort); + List foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate); List top3Foods = foodList.stream() .sorted(Comparator.comparingDouble((Food food) -> @@ -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 foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort); + List foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, endDate.minusWeeks(1), endDate); List worst3Foods = foodList.stream() .sorted(Comparator.comparingDouble((Food food) -> @@ -414,8 +408,7 @@ private void validateFavoriteFood(Long favoriteFoodId, Long userId) { // 1주일동안 먹은 음식들의 영양성분 총합을 요일을 Key로 한 Map을 통해 반환 private HashMap> getNutritionSumByDateMap(Long userId, LocalDate startDate, LocalDate endDate) { HashMap> maps = new HashMap<>(); - Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP); - List foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate, sort); + List foodList = foodRepository.findAllByUserIdAndDateBetweenOrderByAddedTimeAsc(userId, startDate, endDate); for (Food food : foodList) { if (maps.containsKey(food.getDate())) { maps.get(food.getDate()).add(food.getBaseNutrition());