Skip to content

Commit

Permalink
🚑 Fix: 캐싱 동기화 적용 및 TimeStamp 필드명 MessageUtil에서 통합 관리 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Nov 12, 2023
1 parent d95a931 commit e554b1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/java/com/diareat/diareat/food/service/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.diareat.diareat.user.dto.response.ResponseRankUserDto;
import com.diareat.diareat.user.repository.FollowRepository;
import com.diareat.diareat.user.repository.UserRepository;
import com.diareat.diareat.util.MessageUtil;
import com.diareat.diareat.util.api.ResponseCode;
import com.diareat.diareat.util.exception.FavoriteException;
import com.diareat.diareat.util.exception.FoodException;
Expand All @@ -36,7 +37,7 @@ public class FoodService {
private final UserRepository userRepository;

// 촬영 후, 음식 정보 저장
@CacheEvict(value = "ResponseFoodDto", key = "#createFoodDto.getUserId()+#createFoodDto.getDate().toString()", cacheManager = "diareatCacheManager")
@CacheEvict(value = "ResponseFoodDto, ResponseNutritionSumByDateDto", key = "#createFoodDto.getUserId()+#createFoodDto.getDate().toString()", cacheManager = "diareatCacheManager")
@Transactional
public Long saveFood(CreateFoodDto createFoodDto) {
if (foodRepository.existsByName(createFoodDto.getName())){
Expand All @@ -52,25 +53,25 @@ 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, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
return foodList.stream()
.map(food -> ResponseFoodDto.of(food.getId(), food.getUser().getId(), food.getName(), food.getBaseNutrition(), food.isFavorite())).collect(Collectors.toList());
}

// 음식 정보 수정
@CacheEvict(value = "ResponseFoodDto", key = "#updateFoodDto.getUserId()", cacheManager = "diareatCacheManager")
@CacheEvict(value = "ResponseFoodDto, ResponseNutritionSumByDateDto", key = "#updateFoodDto.getUserId()+date.toString()", cacheManager = "diareatCacheManager")
@Transactional
public void updateFood(UpdateFoodDto updateFoodDto) {
public void updateFood(UpdateFoodDto updateFoodDto, LocalDate date) {
Food food = getFoodById(updateFoodDto.getFoodId());
food.updateFood(updateFoodDto.getName(), updateFoodDto.getBaseNutrition());
foodRepository.save(food);
}

// 음식 삭제
@CacheEvict(value = "ResponseFoodDto", key = "#userId", cacheManager = "diareatCacheManager")
@CacheEvict(value = "ResponseFoodDto, ResponseNutritionSumByDateDto", key = "#userId+date.toString()", cacheManager = "diareatCacheManager")
@Transactional
public void deleteFood(Long foodId, Long userId) {
public void deleteFood(Long foodId, Long userId, LocalDate date) {
validateFood(foodId, userId);
foodRepository.deleteById(foodId);
}
Expand Down Expand Up @@ -119,7 +120,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, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDate(userId, date, sort);
return calculateNutritionSumAndRatio(userId, foodList, date, 1);
}
Expand All @@ -129,7 +130,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByDate(Long userId, LocalDat
public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 7);
Expand All @@ -140,7 +141,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByWeek(Long userId) {
public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusMonths(1), endDate, sort);

return calculateNutritionSumAndRatio(userId, foodList, endDate, 30);
Expand All @@ -151,7 +152,7 @@ public ResponseNutritionSumByDateDto getNutritionSumByMonth(Long userId) {
public ResponseFoodRankDto getBestFoodByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

List<Food> top3Foods = foodList.stream()
Expand All @@ -174,7 +175,7 @@ public ResponseFoodRankDto getBestFoodByWeek(Long userId) {
public ResponseFoodRankDto getWorstFoodByWeek(Long userId) {
validateUser(userId);
LocalDate endDate = LocalDate.now();
Sort sort = Sort.by(Sort.Direction.DESC, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, endDate.minusWeeks(1), endDate, sort);

List<Food> worst3Foods = foodList.stream()
Expand Down Expand Up @@ -402,7 +403,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, "addedTime");
Sort sort = Sort.by(Sort.Direction.DESC, MessageUtil.TIME_STAMP);
List<Food> foodList = foodRepository.findAllByUserIdAndDateBetween(userId, startDate, endDate, sort);
for (Food food : foodList) {
if (maps.containsKey(food.getDate())) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/diareat/diareat/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public class MessageUtil { // 반복되는 메시지의 형식을 저장하고
public static final String FAT_RANGE = "지방은 25 이상, 500 이하의 값을 입력해주세요.";

public static final String PAST_OR_PRESENT = "과거 또는 오늘 날짜여야 합니다.";
public static final String TIME_STAMP = "addedTime";
}

0 comments on commit e554b1c

Please sign in to comment.