Controller 구현
@PutMapping("/comment/{id}")
public CommentResponseDto updateComment(@PathVariable Long id, @RequestBody Map<String,String> contents, HttpServletRequest req) {
User user = (User) req.getAttribute("user");
return commentService.updateComment(id, contents.get("contents"), user.getUsername(), user.getRole());
}
Service 구현
@Transactional
public CommentResponseDto updateComment(Long id, String contents, String username, UserRoleEnum role) {
Comment comment = findComment(id);
if(role.getAuthority().equals("ROLE_ADMIN")|| comment.getUsername().equals(username) )
comment.update(contents);
else
throw new IllegalArgumentException("당신에겐 글을 수정할 권한이 없습니다 >.< !!");
return new CommentResponseDto().fromComment(comment);
}
Test