TIL/Web Back

[Sparta] 내일배움캠프 TIL 29일차

헤르로우워르드 2024. 5. 31. 20:06
숙련주차 개인과제

 

댓글 컨트롤러 단

    @PostMapping("/write")
    public CommentResponseDto createComment(@RequestBody @Valid CommentRequestDto requestDto, BindingResult bindingResult
    ,@CookieValue(JwtUtil.AUTHORIZATION_HEADER) String tokenValue) {

        checkError(bindingResult);
        if(!requestDto.getUsername().equals(getJwt(tokenValue))) {
            throw new IllegalArgumentException("잘못된 username");
        }
        return commentService.createComment(requestDto);
    }
@PutMapping("/update")
    public CommentResponseDto updateComment(long commentId, @RequestBody @Valid CommentRequestDto requestDto, BindingResult bindingResult
            ,@CookieValue(JwtUtil.AUTHORIZATION_HEADER) String tokenValue)
    {
        if(commentId < 1) {
            throw new IllegalArgumentException();
        }
        checkError(bindingResult);

        if(!requestDto.getUsername().equals(getJwt(tokenValue))) {
            throw new IllegalArgumentException("잘못된 username");
        }

        return commentService.updateComment(commentId, requestDto);
    }
    @DeleteMapping
    public String deleteComment(long commentId, @RequestBody CommentRequestDto requestDto
    ,@CookieValue(JwtUtil.AUTHORIZATION_HEADER) String tokenValue) {

        commentService.deleteComment(commentId, requestDto);

        if(!requestDto.getUsername().equals(getJwt(tokenValue))) {
            throw new IllegalArgumentException("잘못된 username");
        }

        ResponseEntity.status(201);
        return "성공했습니다";
    }