diff --git a/src/main/java/com/developer/wiki/question/command/application/comment/CommentDeleteService.java b/src/main/java/com/developer/wiki/question/command/application/comment/CommentDeleteService.java index 922e40e..cab6bb7 100644 --- a/src/main/java/com/developer/wiki/question/command/application/comment/CommentDeleteService.java +++ b/src/main/java/com/developer/wiki/question/command/application/comment/CommentDeleteService.java @@ -1,5 +1,7 @@ package com.developer.wiki.question.command.application.comment; +import com.developer.wiki.common.exception.BadRequestException; +import com.developer.wiki.common.exception.UnAuthorizedException; import com.developer.wiki.question.command.application.dto.PasswordRequest; import com.developer.wiki.question.command.domain.Comment; import com.developer.wiki.question.command.domain.CommentRepository; @@ -18,12 +20,29 @@ public class CommentDeleteService { public void delete(Long id, PasswordRequest passwordRequest, Long userId) { Comment comment = commentRepository.findById(id).orElseThrow(EntityNotFoundException::new); - if (!Objects.isNull(userId)) { - commentRepository.delete(comment); + checkInvalidAuthorization(passwordRequest, userId, comment); + commentRepository.delete(comment); + } + + private void checkInvalidAuthorization(PasswordRequest passwordRequest, Long userId, + Comment comment) { + if (checkInvalidUser(userId, comment)) { + throw new UnAuthorizedException("댓글 삭제 권한이 없습니다."); + } else if (checkInvalidAnonymous(passwordRequest, userId, comment)) { + throw new UnAuthorizedException("댓글 삭제를 할 수 없습니다."); } - else if(Objects.isNull(userId) && Objects.nonNull(passwordRequest)){ - comment.matchPassword(passwordRequest.getPassword()); - commentRepository.delete(comment); + } + + private boolean checkInvalidUser(Long userId, Comment comment) { + return Objects.nonNull(userId) && (Objects.isNull(comment.getUser()) || !comment.getUser() + .getId().equals(userId)); + } + + private boolean checkInvalidAnonymous(PasswordRequest passwordRequest, Long userId, + Comment comment) { + if (Objects.isNull(userId) && !comment.checkPassword(passwordRequest.getPassword())) { + throw new BadRequestException("비밀번호가 틀렸습니다."); } + return Objects.isNull(userId) && Objects.isNull(passwordRequest); } } diff --git a/src/main/java/com/developer/wiki/question/command/domain/Comment.java b/src/main/java/com/developer/wiki/question/command/domain/Comment.java index cebb576..c79a1ce 100644 --- a/src/main/java/com/developer/wiki/question/command/domain/Comment.java +++ b/src/main/java/com/developer/wiki/question/command/domain/Comment.java @@ -3,6 +3,7 @@ import com.developer.wiki.oauth.User; import com.developer.wiki.question.util.PasswordEncrypter; import java.time.LocalDateTime; +import java.util.Objects; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; @@ -73,6 +74,9 @@ public void matchPassword(String password) { } public boolean checkPassword(String password) { + if (Objects.isNull(this.password)) { + return false; + } if (!PasswordEncrypter.isMatch(password, this.password)) { return false; }