diff --git a/src/main/java/com/example/healthylife/service/HeartService.java b/src/main/java/com/example/healthylife/service/HeartService.java index cc46786..049c5cd 100644 --- a/src/main/java/com/example/healthylife/service/HeartService.java +++ b/src/main/java/com/example/healthylife/service/HeartService.java @@ -26,7 +26,6 @@ public long toggleLike(Long userSq, Long todaySq) { UserEntity user = userRepository.findById(userSq) .orElseThrow(() -> new RuntimeException("유저가 없습니다.")); - //찜 여부 확인 boolean alreadyHeart = heartRepository.existsByUserAndToday(user,today); if(alreadyHeart) { @@ -35,15 +34,16 @@ public long toggleLike(Long userSq, Long todaySq) { heartRepository.delete(heart); today.decrementLikeCount(); } else { - HeartEntity heart = new HeartEntity(today,user); - heartRepository.save(heart); - today.incrementLikeCount(); + HeartEntity heart = new HeartEntity(today,user); + heartRepository.saveAndFlush(heart); // 즉시 저장 시도 + today.incrementLikeCount(); } - TodayEntity updateToday = todayRepository.save(today); - return updateToday.getTodayHearts(); + todayRepository.saveAndFlush(today); // 즉시 저장 시도 + return today.getTodayHearts(); } + // 사용자가 특정 오늘의 글에 대해 좋아요를 눌렀는지 여부 확인 public boolean hasUserLiked(Long todaySq, Long userSq) { // 오늘의 글 및 사용자 엔티티 조회