Springboot

2021-10-05 (Springboot - comment테이블 - 댓글 쓰기 )

BSYeop 2021. 10. 5. 18:49

1. (Mission 수행)Comment 테이블 댓글 써보자

CommentController.java

//3 댓글 쓰기
    //POST /comment/
    //성공 시 json response 아래처럼 나오도록
    //{
    //“success”: true,
    //“message”: “success to post comment in board id {id}”
    //}
    @PostMapping(value = "/")
    public ApiResponse<CommentDTO> insertComment(@RequestBody CommentDTO commentDTO) throws Exception{
        CommentDTO dto = commentService.insertComment(commentDTO);
        return new ApiResponse(true, "success to post comment in board id " + commentDTO.getId());
    }

CommentDAO.java

int insertComment(CommentDTO commentDTO);

CommentService.java

public CommentDTO insertComment(CommentDTO commentDTO) throws Exception {
        commentDTO.setWriteDate(LocalDate.now());
        commentDTO.setWriteTime(LocalTime.now());

        int insertRowCount = commentDAO.insertComment(commentDTO);
        if (insertRowCount > 0){
            return commentDTO;
        }else {
            throw new Exception("failed to insert comment data");
        }
    }

CommentMapper.xml

<insert id="insertComment" parameterType="kr.ac.daegu.springbootapi.comment.model.CommentDTO">
        insert into comment (id, author, content,writeDate,writeTime)
        values (#{id},#{author},#{content},#{writeDate},#{writeTime})
    </insert>

- 게시글의 id값과 작성자,내용을 body에 json request하면 성공메세지를 출력