Spring Level 3 댓글 수정 API 구현

song yuheon·2023년 9월 2일
0

Spring

목록 보기
37/93
post-thumbnail

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 // updateMemo는 따로 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); // update는 memo 클래스에서 만든 것
    else
        throw new IllegalArgumentException("당신에겐 글을 수정할 권한이 없습니다 >.< !!");

    return new CommentResponseDto().fromComment(comment);
}

Test



profile
backend_Devloper

0개의 댓글