본문 바로가기
웹서비스 개발/Spring Rest API

게시판 댓글 API

by 후닝훈 2021. 5. 22.
반응형

CommentVO

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
 
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class CommentVO {
    private Integer id;
    private String context;
    private String created;
    private String updated;
    private Integer board_id;
}

CommentMapper

import org.apache.ibatis.annotations.*;

@Mapper
public interface CommentMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert({"<script>",
            "INSERT INTO comment(content, board_id)",
            "VALUES(#{content}, #{board_id})",
            "</script>"})
    int insertComment(CommentVO commentVO);
 
    @Select({"<script>",
            "SELECT * from comment",
            "where id = #{id}",
            "</script>"})
    CommentVO findOneComment(int id);
 
    @Select({"<script>",
            "SELECT * from comment",
            "order by id desc",
            "</script>"})
    List<CommentVO> findComment(int board_id);
 
    @Update({"<script>",
            "UPDATE comment",
            "set content = #{content}",
            "WHERE id = #{id}",
            "</script>"})
    int updateComment(CommentVO commentVO);
 
    @Delete({"<script>",
            "DELETE FROM comment",
            "WHERE id = #{id}",
            "</script>"})
    int deleteComment(int id);
}

Controller

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class CommentController {
    private final CommentMapper commentMapper;

    @PostMapping("/comment")
    public CommentVO addComment(@RequestBody CommentVO commentVO) {
        commentMapper.insertComment(commentVO);
        return commentVO;
    }

    @GetMapping("/comment")
    public CommentVO findOneComment(@RequestParam Integer id) {
        return commentMapper.findOneComment(id);
    }

    @GetMapping("/comments")
    public List<CommentVO> findAllComment(@RequestParam Integer board_id) {
        List<CommentVO> comments = commentMapper.findComment(board_id);
        return comments;
    }

    @PutMapping("/comment")
    public ResultVO modifyComment(@RequestBody CommentVO commentVO) {
        int result = commentMapper.updateComment(commentVO);
        if ( result > 0) {
            return new ResultVO(0, "success");
        } else {
            return new ResultVO(100, "fail");
        }
    }

    @DeleteMapping("/comment")
    public ResultVO removeComment(@RequestParam int id) {
        int result = commentMapper.deleteComment(id);
        if (result > 0) {
            return new ResultVO(0, "success");
        } else {
            return new ResultVO(100, "fail");
        }
    }
}

 

POSTMAN TEST

반응형

'웹서비스 개발 > Spring Rest API' 카테고리의 다른 글

Swagger  (0) 2021.05.23
이미지 보기 API  (0) 2021.05.21
이미지 업로드, DB 저장  (0) 2021.05.21
Board 삭제 API  (0) 2021.05.20
Board 수정 API  (0) 2021.05.20

댓글