Skip to content

Commit

Permalink
[Feat] 댓글 삭제 #135 (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
yr0202 authored Jan 23, 2025
2 parents e6d1910 + d1ea853 commit 6949bd8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -94,4 +95,23 @@ public ResponseEntity<Void> updateCommentByPostId(

return ResponseEntity.status(HttpStatus.OK).build();
}

@Operation(
description = "내가 작성한 댓글을 삭제하는 기능입니다",
summary = "댓글 삭제"
)
@DeleteMapping("/{commentId}")
public ResponseEntity<Void> deleteCommentByPostId(
@AuthenticationPrincipal CustomOAuth2User auth,
@PathVariable Long postId,
@PathVariable Long commentId) {

if (auth == null || auth.getId() == null) {
throw new GeneralException(ResponseCode.EMPTY_TOKEN);
}

commentService.deleteComment(postId, commentId, auth.getId());

return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,50 @@ public void registerComment(Long postId, CommentRegisterRequest request, String
commentRepository.save(comment);
}

public void updateComment(Long postId, Long commentUd, CommentUpdateRequest request, String memberId) {
Member member = memberRepository.findById(memberId)
public void updateComment(Long postId, Long commentId, CommentUpdateRequest request, String memberId) {
Member member = validateMember(memberId);

validatePost(postId);

Comment comment = validateComment(commentId);

validateCommentOwnerMatch(member.getId(), comment.getMember().getId());

comment.updateComment(request.getContent(), request.getIsAnonymous()); // dirty-check -> save() 필요없음
}

public void deleteComment(Long postId, Long commentId, String memberId) {
Member member = validateMember(memberId);

validatePost(postId);

Comment comment = validateComment(commentId);

validateCommentOwnerMatch(member.getId(), comment.getMember().getId());

comment.deleteComment();
}

// 유효성 검증 메서드
private Member validateMember(String memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_MEMBER_ID));
}

private void validatePost(Long postId) {
postRepository.findByIdAndIsDeletedFalse(postId)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_POST));
}

Comment comment = commentRepository.findById(commentUd)
private Comment validateComment(Long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(() -> new GeneralException(ResponseCode.NOT_EXIST_COMMENT));
}

// 로그인한 사용자와 댓글 작성자가 일치하는지 확인
if (!Objects.equals(comment.getMember().getId(), member.getId())) {
// 로그인한 사용자와 댓글 작성자가 일치하는지 확인
private void validateCommentOwnerMatch(String commentOwnerId, String memberId) {
if (!Objects.equals(commentOwnerId, memberId)) {
throw new GeneralException(ResponseCode.NOT_AUTHORIZED_COMMENT_MODIFICATION);
}

comment.updateComment(request.getContent(), request.getIsAnonymous()); // dirty-check -> save() 필요없음
}
}

0 comments on commit 6949bd8

Please sign in to comment.