Skip to content

Commit

Permalink
fix: updateComment 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
lotuxsoo committed May 30, 2024
1 parent 9abdc01 commit b194f41
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public static Comment makeOnlyComment(Long issueId, String userId, String conten
.files(new ArrayList<>()).
build();
}

public static Comment makeOnlyComment(Long id, Long issueId, String userId, String content) {
return Comment.builder()
.id(id)
.issueId(issueId)
.userId(userId)
.content(content)
.files(new ArrayList<>()).
build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public CommentListResponse updateComment(@PathVariable long id, @RequestBody Com

@Authenticate
@DeleteMapping("/comment/{id}")
public void deleteComment(@PathVariable long id, @AuthenticatedUserId String userId) throws NoSuchElementException, AuthorizeException {
public void deleteComment(@PathVariable long id, @AuthenticatedUserId String userId)
throws NoSuchElementException, AuthorizeException {
commentService.deleteComment(id, userId);
}

Expand All @@ -51,4 +52,4 @@ public int likeComment(@PathVariable long id, @AuthenticatedUserId String userId
public int unLikeComment(@PathVariable long id, @AuthenticatedUserId String userId) {
return commentService.unlikeComment(userId, id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ public int unlikeComment(String userId, long commentId) {

public String getFirstCommentTextAtIssue(Issue issue) {
Comment firstAtIssue = commentRepository.findFirstAtIssue(issue.getId());
if (firstAtIssue == null) return null;
if (firstAtIssue == null) {
return null;
}

return firstAtIssue.getContent();
}

public Comment updateComment(long id, String userId, CommentPostRequest commentInfo) throws NoSuchElementException, AuthorizeException {
public Comment updateComment(long id, String userId, CommentPostRequest commentInfo)
throws NoSuchElementException, AuthorizeException {
Comment origin = authorize(id, userId);
Comment newComment = Comment.makeOnlyComment(origin.getIssueId(), userId, commentInfo.content());
Comment newComment = Comment.makeOnlyComment(id, origin.getIssueId(), userId, commentInfo.content());
commentRepository.save(newComment);
return getCommentById(id);
}
Expand All @@ -89,21 +92,25 @@ public void deleteComment(Long commentId, String userId) throws NoSuchElementExc
}

@Override
public Comment authorize(Long commentId, String userId) throws NoSuchElementException{
public Comment authorize(Long commentId, String userId) throws NoSuchElementException {
Comment comment = getCommentById(commentId);
if(!comment.getUserId().equals(userId)) throw new AuthorizeException(commentId + "번 댓글에 대한 권한이 없습니다");
if (!comment.getUserId().equals(userId)) {
throw new AuthorizeException(commentId + "번 댓글에 대한 권한이 없습니다");
}

return comment;
}

private Comment getCommentById(long commentId) {
Optional<Comment> byId = commentRepository.findById(commentId);
if (byId.isEmpty()) throw new NoSuchElementException("존재하지 않는 댓글입니다");
if (byId.isEmpty()) {
throw new NoSuchElementException("존재하지 않는 댓글입니다");
}

return byId.get();
}

private Predicate<Like> isSame(String userId, long commentId) {
return like -> like.getCommentId().equals(commentId) && like.getUserId().equals(userId);
}
}
}

0 comments on commit b194f41

Please sign in to comment.