Skip to content

Commit

Permalink
feat: 후기 댓글 삭제 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeyoungeee committed Jul 30, 2024
1 parent 4406b21 commit 11cfe1b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ public ResponseEntity<SaveCommentResponse> saveComment(@RequestBody Map<String,
return ResponseEntity.ok(comment);
}

@DeleteMapping("/api/reviews/{reviewId}/comments/{commentId}")
public ResponseEntity<Map<String, String>> deleteComment(@PathVariable("commentId") Long commentId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
commentService.deleteComment(userPrinciple.getEmail(), commentId);
return ResponseEntity.ok(Map.of("message", "댓글 삭제 성공"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@Slf4j
Expand All @@ -24,6 +25,8 @@ public class CommentService {
private final ReviewCommentRepository reviewCommentRepository;
private final ReviewRepository reviewRepository;


// 후기 댓글 작성
@Transactional
public SaveCommentResponse saveComment(String email, Long reviewId, String content) {
Member member = findMemberByEmail(email);
Expand All @@ -41,17 +44,39 @@ public SaveCommentResponse saveComment(String email, Long reviewId, String conte
return SaveCommentResponse.builder().comment(comment).build();
}

// 후기 댓글 삭제
@Transactional
public void deleteComment(String email, Long commentId) {
ReviewComment comment = findCommentById(commentId);
Member member = findMemberByEmail(email);
Review review = findReviewById(comment.getReview().getReviewId());

member.getReviewComments().remove(comment);
review.getReviewComments().remove(comment);
reviewCommentRepository.delete(comment);
}

private ReviewComment findCommentById(Long commentId) {
return reviewCommentRepository.findById(commentId).orElseThrow(() -> {
log.info("해당 후기 댓글이 존재하지 않음.");
return new IllegalArgumentException("해당 후기 댓글이 존재하지 않습니다.");
});
}

private Review findReviewById(Long reviewId ) {
return reviewRepository.findById(reviewId).orElseThrow(() -> {
log.info("후기가 존재하지 않음.");
return new IllegalArgumentException("후기가 존재하지 않습니다.");
});
}


private Member findMemberByEmail(String email) {
return memberRepository.findByEmail(email).orElseThrow(() -> {
log.info("회원이 존재하지 않음.");
return new IllegalArgumentException("회원이 존재하지 않습니다.");
});
}


}

0 comments on commit 11cfe1b

Please sign in to comment.