From dd3065ae8efbfc3657166d16c9d25e6e192fa3e2 Mon Sep 17 00:00:00 2001 From: Soyeon-Cha <7103sy@naver.com> Date: Thu, 23 Nov 2023 11:51:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8F=84=EC=9B=80=EB=8F=BC=EC=9A=94=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Review/service/ReviewService.java | 11 +++++ .../controller/ReviewHelpController.java | 28 +++++++++++ .../ReviewHelp/dto/ReviewHelpRequestDto.java | 11 +++++ .../repository/ReviewHelpRepository.java | 12 +++++ .../ReviewHelp/service/ReviewHelpService.java | 48 +++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 src/main/java/MARKETFUBY/ReviewHelp/controller/ReviewHelpController.java create mode 100644 src/main/java/MARKETFUBY/ReviewHelp/dto/ReviewHelpRequestDto.java create mode 100644 src/main/java/MARKETFUBY/ReviewHelp/repository/ReviewHelpRepository.java create mode 100644 src/main/java/MARKETFUBY/ReviewHelp/service/ReviewHelpService.java diff --git a/src/main/java/MARKETFUBY/Review/service/ReviewService.java b/src/main/java/MARKETFUBY/Review/service/ReviewService.java index 4845d5d..794ae22 100644 --- a/src/main/java/MARKETFUBY/Review/service/ReviewService.java +++ b/src/main/java/MARKETFUBY/Review/service/ReviewService.java @@ -19,6 +19,9 @@ import MARKETFUBY.ReviewImage.domain.ReviewImage; import MARKETFUBY.ReviewImage.repository.ReviewImageRepository; import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityNotFoundException; @Service @RequiredArgsConstructor @@ -80,4 +83,12 @@ public void update(Long reviewId, ReviewRequestDto reviewRequestDto){ review.updateReview(reviewRequestDto.getContent()); reviewRepository.save(review); } + + // 도움돼요에서 사용 + @Transactional(readOnly = true) + public Review findReviewById(Long reviewId){ + return reviewRepository.findById(reviewId) + .orElseThrow(()->new EntityNotFoundException("해당 리뷰가 존재하지 않습니다.")); + } + } diff --git a/src/main/java/MARKETFUBY/ReviewHelp/controller/ReviewHelpController.java b/src/main/java/MARKETFUBY/ReviewHelp/controller/ReviewHelpController.java new file mode 100644 index 0000000..c2454d0 --- /dev/null +++ b/src/main/java/MARKETFUBY/ReviewHelp/controller/ReviewHelpController.java @@ -0,0 +1,28 @@ +package MARKETFUBY.ReviewHelp.controller; + +import MARKETFUBY.ReviewHelp.dto.ReviewHelpRequestDto; +import MARKETFUBY.ReviewHelp.service.ReviewHelpService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/goods/reviews/{reviewId}/helps") +public class ReviewHelpController { + private final ReviewHelpService reviewHelpService; + + @PostMapping + @ResponseStatus(value = HttpStatus.CREATED) + public String createReviewHelp(@PathVariable Long reviewId, @RequestBody ReviewHelpRequestDto reviewHelpRequestDto){ + Long memberId = reviewHelpRequestDto.getMemberId(); + if(memberId != null){ + reviewHelpService.createReviewHelp(reviewId, memberId); + return "도움돼요가 반영되었습니다."; + } + else{ + return "로그인이 필요한 서비스입니다."; + } + } + +} diff --git a/src/main/java/MARKETFUBY/ReviewHelp/dto/ReviewHelpRequestDto.java b/src/main/java/MARKETFUBY/ReviewHelp/dto/ReviewHelpRequestDto.java new file mode 100644 index 0000000..a0d3b93 --- /dev/null +++ b/src/main/java/MARKETFUBY/ReviewHelp/dto/ReviewHelpRequestDto.java @@ -0,0 +1,11 @@ +package MARKETFUBY.ReviewHelp.dto; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ReviewHelpRequestDto { + private Long memberId; +} diff --git a/src/main/java/MARKETFUBY/ReviewHelp/repository/ReviewHelpRepository.java b/src/main/java/MARKETFUBY/ReviewHelp/repository/ReviewHelpRepository.java new file mode 100644 index 0000000..42dc922 --- /dev/null +++ b/src/main/java/MARKETFUBY/ReviewHelp/repository/ReviewHelpRepository.java @@ -0,0 +1,12 @@ +package MARKETFUBY.ReviewHelp.repository; + +import MARKETFUBY.Member.domain.Member; +import MARKETFUBY.Review.domain.Review; +import MARKETFUBY.ReviewHelp.domain.ReviewHelp; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReviewHelpRepository extends JpaRepository { + boolean existsByMemberAndReview(Member member, Review review); +} diff --git a/src/main/java/MARKETFUBY/ReviewHelp/service/ReviewHelpService.java b/src/main/java/MARKETFUBY/ReviewHelp/service/ReviewHelpService.java new file mode 100644 index 0000000..bd84954 --- /dev/null +++ b/src/main/java/MARKETFUBY/ReviewHelp/service/ReviewHelpService.java @@ -0,0 +1,48 @@ +package MARKETFUBY.ReviewHelp.service; + +import MARKETFUBY.Member.domain.Member; +import MARKETFUBY.Member.service.MemberService; +import MARKETFUBY.Review.domain.Review; +import MARKETFUBY.Review.service.ReviewService; +import MARKETFUBY.ReviewHelp.domain.ReviewHelp; +import MARKETFUBY.ReviewHelp.repository.ReviewHelpRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Service +@Transactional +@RequiredArgsConstructor +public class ReviewHelpService { + private final MemberService memberService; + private final ReviewHelpRepository reviewHelpRepository; + + @Lazy + @Autowired + private ReviewService reviewService; + + public void createReviewHelp(Long reviewId, Long memberId){ + Review review = reviewService.findReviewById(reviewId); + Member member = memberService.findMemberById(memberId); + + if(isExistsByMemberAndReview(member, review)){ + throw new RuntimeException("이미 도움돼요가 반영된 리뷰입니다."); + } + + ReviewHelp reviewHelp = ReviewHelp.builder() + .review(review) + .member(member) + .build(); + + reviewHelpRepository.save(reviewHelp); + } + + @Transactional(readOnly = true) + public boolean isExistsByMemberAndReview(Member member, Review review){ + return reviewHelpRepository.existsByMemberAndReview(member, review); + } +}