Skip to content

Commit

Permalink
Merge pull request #19 from MARKETFUBY/feat/help
Browse files Browse the repository at this point in the history
[차소연] 도움돼요 생성/취소 구현
  • Loading branch information
Soyeon-Cha authored Nov 23, 2023
2 parents 1b718a0 + dd3065a commit aa6fa18
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/MARKETFUBY/Review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Expand Down Expand Up @@ -83,4 +86,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("해당 리뷰가 존재하지 않습니다."));
}

}
Original file line number Diff line number Diff line change
@@ -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 "로그인이 필요한 서비스입니다.";
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/MARKETFUBY/ReviewHelp/dto/ReviewHelpRequestDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<ReviewHelp, Long> {
boolean existsByMemberAndReview(Member member, Review review);
}
48 changes: 48 additions & 0 deletions src/main/java/MARKETFUBY/ReviewHelp/service/ReviewHelpService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit aa6fa18

Please sign in to comment.