From a0088a2eedf7eed41a28adee7b6648ab63626ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8A=B9=EC=A4=80?= Date: Fri, 23 Aug 2024 15:13:59 +0900 Subject: [PATCH] =?UTF-8?q?=ED=95=98=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HeartController.java | 35 +++++++------- .../healthylife/entity/TodayEntity.java | 12 +++++ .../repository/HeartRepository.java | 2 + .../healthylife/service/HeartService.java | 47 ++++++++++++++++--- 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/example/healthylife/controller/HeartController.java b/src/main/java/com/example/healthylife/controller/HeartController.java index 4333547..736ac31 100644 --- a/src/main/java/com/example/healthylife/controller/HeartController.java +++ b/src/main/java/com/example/healthylife/controller/HeartController.java @@ -7,6 +7,8 @@ import com.example.healthylife.service.UserService; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.util.Optional; @@ -24,24 +26,23 @@ public class HeartController { @Autowired private TodayService todayService; - @ApiOperation("하트") - @PostMapping("/heart/{todaySq}") - public void like(@PathVariable("todaySq") long todaySq, @RequestParam String userId) { - Optional user = userService.findUserById(userId); - Optional today = todayService.findbytodaysq(todaySq); - - if (user.isPresent() && today.isPresent()) { - heartService.Like(user.get(), today.get()); - } else { - // Handle the case when user or today entity is not found - throw new RuntimeException("User or Today entity not found"); - } + @ApiOperation(value = "오늘의 글 좋아요 토글") + @PostMapping("/toggle/{todaySq}") + public ResponseEntity toggleLike(@PathVariable Long todaySq, Authentication authentication) { + String username = authentication.getName(); + UserEntity user = userService.findUserById(username) + .orElseThrow(() -> new RuntimeException("유저가 없습니다.")); + TodayEntity today = todayService.findbytodaysq(todaySq) + .orElseThrow(() -> new RuntimeException("오늘의 글이 없습니다.")); + + heartService.toggleLike(user, today); + return ResponseEntity.ok().build(); } - @ApiOperation("하트 수") - @GetMapping("/count/{todaySq}") - public Long heartCount(@PathVariable("todaySq") long todaySq) { - Optional today = todayService.findbytodaysq(todaySq); - return heartService.HeartCount(today.orElse(null)); + @ApiOperation(value = "오늘의 글에 대해 좋아요를 눌렀는지 확인") + @GetMapping("/hasLiked/{todaySq}") + public ResponseEntity hasUserLiked(@PathVariable Long todaySq) { + boolean hasLiked = heartService.hasUserLiked(todaySq); + return ResponseEntity.ok(hasLiked); } } diff --git a/src/main/java/com/example/healthylife/entity/TodayEntity.java b/src/main/java/com/example/healthylife/entity/TodayEntity.java index 251242f..2530dd7 100644 --- a/src/main/java/com/example/healthylife/entity/TodayEntity.java +++ b/src/main/java/com/example/healthylife/entity/TodayEntity.java @@ -64,5 +64,17 @@ public TodayEntity(long todaySq, String todayContents, this.imageurl = imageurl; } + // 좋아요 수 증가 + public void incrementLikeCount() { + this.todayHearts++; + } + + // 좋아요 수 감소 + public void decrementLikeCount() { + if (this.todayHearts > 0) { + this.todayHearts--; + } + } + } diff --git a/src/main/java/com/example/healthylife/repository/HeartRepository.java b/src/main/java/com/example/healthylife/repository/HeartRepository.java index 1e839aa..6b06652 100644 --- a/src/main/java/com/example/healthylife/repository/HeartRepository.java +++ b/src/main/java/com/example/healthylife/repository/HeartRepository.java @@ -15,4 +15,6 @@ public interface HeartRepository extends JpaRepository { @Query("SELECT COUNT(h) FROM HeartEntity h WHERE h.today = :today AND h.status = true") Long HeartCount(@Param("today") TodayEntity today); + + boolean existsByUserAndToday(UserEntity user, TodayEntity today); } diff --git a/src/main/java/com/example/healthylife/service/HeartService.java b/src/main/java/com/example/healthylife/service/HeartService.java index 6553f3c..43b48c1 100644 --- a/src/main/java/com/example/healthylife/service/HeartService.java +++ b/src/main/java/com/example/healthylife/service/HeartService.java @@ -5,24 +5,43 @@ import com.example.healthylife.entity.TodayEntity; import com.example.healthylife.entity.UserEntity; import com.example.healthylife.repository.HeartRepository; +import com.example.healthylife.repository.TodayRepository; +import com.example.healthylife.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import javax.transaction.Transactional; import java.time.LocalDateTime; import java.util.Optional; @Service public class HeartService { - @Autowired - private HeartRepository heartRepository; + private final HeartRepository heartRepository; + private final TodayRepository todayRepository; + private final UserRepository userRepository; - public void Like(UserEntity user, TodayEntity today) { + public HeartService(HeartRepository heartRepository, TodayRepository todayRepository, UserRepository userRepository) { + this.heartRepository = heartRepository; + this.todayRepository = todayRepository; + this.userRepository = userRepository; + } + + @Transactional + public void toggleLike(UserEntity user, TodayEntity today) { Optional existingHeart = heartRepository.findByUserAndToday(user, today); if (existingHeart.isPresent()) { HeartEntity heart = existingHeart.get(); - heart.toggleStatus(); + heart.toggleStatus(); // 좋아요 상태를 토글 heartRepository.save(heart); + + if (heart.getStatus()) { + today.incrementLikeCount(); // 좋아요 수 증가 + } else { + today.decrementLikeCount(); // 좋아요 수 감소 + } } else { HeartEntity heart = HeartEntity.builder() .user(user) @@ -31,10 +50,26 @@ public void Like(UserEntity user, TodayEntity today) { .createdAt(LocalDateTime.now()) .build(); heartRepository.save(heart); + today.incrementLikeCount(); // 새로운 좋아요가 추가된 경우 좋아요 수 증가 } + + // 오늘의 글에 대한 변경 사항 저장 + todayRepository.save(today); } - public Long HeartCount(TodayEntity today) { - return heartRepository.HeartCount(today); + // 사용자가 특정 오늘의 글에 대해 좋아요를 눌렀는지 여부 확인 + public boolean hasUserLiked(Long todaySq) { + // 인증된 사용자의 정보를 가져옴 + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String username = authentication.getName(); + + // 오늘의 글 및 사용자 엔티티 조회 + TodayEntity today = todayRepository.findById(todaySq) + .orElseThrow(() -> new RuntimeException("오늘의 글이 없습니다.")); + UserEntity user = userRepository.findByUserId(username) + .orElseThrow(() -> new RuntimeException("유저가 없습니다.")); + + // 좋아요 여부 확인 + return heartRepository.existsByUserAndToday(user, today); } }