From 0ebe8527ae248de317bf4de81534a337457a6232 Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Wed, 31 Jul 2024 01:42:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=ED=86=A0=EA=B8=80=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../likelion/MZConnent/api/culture/CultureController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/likelion/MZConnent/api/culture/CultureController.java b/src/main/java/likelion/MZConnent/api/culture/CultureController.java index 63f6dba..e74bc2d 100644 --- a/src/main/java/likelion/MZConnent/api/culture/CultureController.java +++ b/src/main/java/likelion/MZConnent/api/culture/CultureController.java @@ -59,8 +59,8 @@ public ResponseEntity getCultureDetailInfo(@PathVariable( } // 문화 관심 토글 - @PostMapping("/api/cultures/{cultureId}/interests") - public ResponseEntity> addCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) { + @PutMapping("/api/cultures/{cultureId}/interests") + public ResponseEntity> toggleCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) { boolean status = cultureService.toggleCultureInterest(userPrinciple.getEmail(), cultureId); if (status) { From 6db3dd511928323095eb5a0708a56db23bcd7f11 Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Wed, 31 Jul 2024 01:43:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=ED=9B=84=EA=B8=B0=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=ED=86=A0=EA=B8=80=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 후기 좋아요 토글(추가/삭제) 기능 추가함 --- .../api/review/ReviewController.java | 13 +++++ .../MZConnent/domain/review/Review.java | 2 + .../review/ReviewLikeRepository.java | 10 ++++ .../service/review/ReviewService.java | 58 ++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/main/java/likelion/MZConnent/api/review/ReviewController.java b/src/main/java/likelion/MZConnent/api/review/ReviewController.java index d1d488d..140cd9a 100644 --- a/src/main/java/likelion/MZConnent/api/review/ReviewController.java +++ b/src/main/java/likelion/MZConnent/api/review/ReviewController.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController @RequiredArgsConstructor @@ -58,4 +59,16 @@ public ResponseEntity saveReview( return ResponseEntity.status(500).body(null); } } + + // 후기 좋아요 토글 + @PutMapping("/api/reviews/{reviewId}/likes") + public ResponseEntity> toggleReviewLike(@PathVariable("reviewId") Long reviewId, @AuthenticationPrincipal UserPrinciple userPrinciple) { + boolean status = reviewService.toggleReviewLike(userPrinciple.getEmail(), reviewId); + + if (status) { + return ResponseEntity.ok(Map.of("message", "후기 좋아요 추가 성공")); + } else { + return ResponseEntity.ok(Map.of("message", "후기 좋아요 삭제 성공")); + } + } } diff --git a/src/main/java/likelion/MZConnent/domain/review/Review.java b/src/main/java/likelion/MZConnent/domain/review/Review.java index af71b79..e0fa734 100644 --- a/src/main/java/likelion/MZConnent/domain/review/Review.java +++ b/src/main/java/likelion/MZConnent/domain/review/Review.java @@ -6,12 +6,14 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.time.LocalDateTime; import java.util.List; @Entity @Getter +@Setter @NoArgsConstructor public class Review { @Id diff --git a/src/main/java/likelion/MZConnent/repository/review/ReviewLikeRepository.java b/src/main/java/likelion/MZConnent/repository/review/ReviewLikeRepository.java index 6a20243..d83a486 100644 --- a/src/main/java/likelion/MZConnent/repository/review/ReviewLikeRepository.java +++ b/src/main/java/likelion/MZConnent/repository/review/ReviewLikeRepository.java @@ -1,9 +1,19 @@ package likelion.MZConnent.repository.review; +import likelion.MZConnent.domain.culture.Culture; +import likelion.MZConnent.domain.culture.CultureInterest; +import likelion.MZConnent.domain.member.Member; +import likelion.MZConnent.domain.review.Review; import likelion.MZConnent.domain.review.ReviewLike; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface ReviewLikeRepository extends JpaRepository { + + boolean existsByMemberAndReview(Member member, Review review); + + Optional findByMemberAndReview(Member member, Review review); } diff --git a/src/main/java/likelion/MZConnent/service/review/ReviewService.java b/src/main/java/likelion/MZConnent/service/review/ReviewService.java index 93cdd4d..e8587d3 100644 --- a/src/main/java/likelion/MZConnent/service/review/ReviewService.java +++ b/src/main/java/likelion/MZConnent/service/review/ReviewService.java @@ -2,14 +2,17 @@ import jakarta.transaction.Transactional; import likelion.MZConnent.domain.culture.Culture; +import likelion.MZConnent.domain.culture.CultureInterest; import likelion.MZConnent.domain.member.Member; import likelion.MZConnent.domain.review.Review; +import likelion.MZConnent.domain.review.ReviewLike; import likelion.MZConnent.dto.paging.response.PageContentResponse; import likelion.MZConnent.dto.review.request.SaveReviewRequest; import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse; import likelion.MZConnent.dto.review.response.SaveReviewResponse; import likelion.MZConnent.repository.culture.CultureRepository; import likelion.MZConnent.repository.member.MemberRepository; +import likelion.MZConnent.repository.review.ReviewLikeRepository; import likelion.MZConnent.repository.review.ReviewRepository; import likelion.MZConnent.service.image.S3ImageService; import lombok.RequiredArgsConstructor; @@ -36,6 +39,7 @@ public class ReviewService { private final S3ImageService s3ImageService; private final MemberRepository memberRepository; private final CultureRepository cultureRepository; + private final ReviewLikeRepository reviewLikeRepository; private final int PAGE_SIZE = 6; public PageContentResponse getReviewsSimpleList(String keyword, int page) { @@ -61,8 +65,8 @@ public PageContentResponse getReviewsSimpleList(String ke public SaveReviewResponse createReview(String email, SaveReviewRequest request, List images, Long cultureId) throws IOException { List imageUrls = uploadImages(images); - Member member = memberRepository.findByEmail(email).orElseThrow(() -> new IllegalArgumentException("회원 정보를 찾을 수 없습니다.")); - Culture culture = cultureRepository.findById(cultureId).orElseThrow(() -> new IllegalArgumentException("문화 정보를 찾을 수 없습니다.")); + Member member = findMemberByEmail(email); + Culture culture = findCultureById(cultureId); Review review = buildReview(request, imageUrls, member, culture); review = reviewRepository.save(review); @@ -116,4 +120,54 @@ private SaveReviewResponse buildSaveReviewResponse(Review review, Member member, .likeCount(review.getLikeCount()) .build(); } + + public boolean toggleReviewLike(String email, Long reviewId) { + + Member member = findMemberByEmail(email); + Review review = findReviewById(reviewId); + + boolean exists = reviewLikeRepository.existsByMemberAndReview(member, review); + + + if (exists) { // 이미 후기 좋아요를 등록한 경우 (삭제) + ReviewLike reviewLike = reviewLikeRepository.findByMemberAndReview(member, review).get(); + reviewLikeRepository.delete(reviewLike); + + member.getReviewLikes().remove(reviewLike); + review.getReviewLikes().remove(reviewLike); + review.setLikeCount(review.getLikeCount() - 1); + return false; + } + else { // 후기 좋아요를 하지 않은 경우 (추가) + ReviewLike reviewLike = reviewLikeRepository.save(ReviewLike.builder() + .member(member) + .review(review) + .build()); + + member.getReviewLikes().add(reviewLike); + review.getReviewLikes().add(reviewLike); + review.setLikeCount(review.getLikeCount() + 1); + return true; + } + } + + private Review findReviewById(Long reviewId) { + return reviewRepository.findById(reviewId).orElseThrow(() -> { + log.info("후기 정보를 찾을 수 없음."); + return new IllegalArgumentException("후기 정보를 찾을 수 없습니다."); + }); + } + private Culture findCultureById(Long cultureId) { + return cultureRepository.findById(cultureId).orElseThrow(() -> { + log.info("문화 정보를 찾을 수 없음."); + return new IllegalArgumentException("문화 정보를 찾을 수 없습니다."); + }); + } + + private Member findMemberByEmail(String email) { + return memberRepository.findByEmail(email).orElseThrow(() -> { + log.info("회원 정보를 찾을 수 없음."); + return new IllegalArgumentException("회원 정보를 찾을 수 없습니다."); + }); + } }