Skip to content

Commit

Permalink
Merge pull request #117 from Developer-Wikis/feature/#109
Browse files Browse the repository at this point in the history
refactro: 댓글 삭제 로직 수정
  • Loading branch information
dhkstnaos authored Dec 11, 2022
2 parents 51aac87 + e7b73c9 commit 22118bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 22118bd

Please sign in to comment.