1. (Mission 수행)기존 Board 테이블 글 수정 기능에 패스워드 일치시 수정가능하게 하자
BoardController.java
//4 글 수정 기능: 패스워드 일치하면 수정 되도록
/*
PUT /board/{id} 고쳐서.
패스워드 일치: json response 아래처럼 나오도록
{
“success”: true,
“message”: “success to update board id {id}”
}
패스워드 일치하지 않으면
아래 json response 나오도록
{
“success”: false,
“message”: “password incorrect in board id {id}”
“data”: null
}
*/
@PutMapping(value = "/{id}")
public ApiResponse<BoardDTO> putBoard(@PathVariable int id,
@RequestBody BoardDTO boardDTO) throws Exception {
log.debug("id: " + id);
return boardService.putBoard(id, boardDTO);
}
BoardDAO.java
int putBoard(BoardDTO boardDTO);
BoardService.java
- selectdData.getPassword()는 EndUser가 글쓰기할때 입력한 password이고, userInputPassword는 사용자가 수정하기 위해 입력한 password의 값이 들어있다.
- selectdData.getPassword()의 값과 userInputPassword의 값을 비교해서 같으면 글을 수정하면서 "success to update board id {id}"를 다르면 글을 수정하지 않고 "password incorrect in board id {id}"를 출력
- 처음에 if (userInputPassword.equals(selectedData.getPassword()))를 하는 바람에 패스워드가 같던 다르던 수정이 되어서 찾는다고 시간을 오래 소비했다. 코드의 실행 순서를 잘 생각하고 코드를 작성하자
public ApiResponse<BoardDTO> putBoard(int id, BoardDTO boardDTO) {
BoardDTO selectedData = boardDAO.getBoardById(id);
String userInputPassword = boardDTO.getPassword();
if (userInputPassword.equals(selectedData.getPassword())) {
// author,
// content,
// subject,
// content
// writeDate, writeTime 업데이트
boardDTO.setId(id);
boardDTO.setWriteDate(LocalDate.now());
boardDTO.setWriteTime(LocalTime.now());
int result = boardDAO.putBoard(boardDTO);
if (result > 0) {
return new ApiResponse(true, "success to update board id " + id);
}
}
return new ApiResponse(false,"password incorrect in board id " + id);
}
public ApiResponse<BoardDTO> getBoardById(int id) {
BoardDTO data = boardDAO.getBoardById(id);
return new ApiResponse(true, data);
}
BoardMapper.xml
<update id="putBoard" parameterType="kr.ac.daegu.springbootapi.board.model.BoardDTO">
UPDATE BOARD
SET author=#{author},
content=#{content},
subject=#{subject},
writeDate=#{writeDate},
writeTime=#{writeTime}
WHERE id=#{id}
</update>
postman
- 패스워드가 일치할 경우 "success to update board id {id}"를 출력하면서 글이 수정됨
- 패스워드가 일치하지 않을 경우 "password incorrect in board id {id}"를 출력하면서 글이 수정되지 않음
'Springboot' 카테고리의 다른 글
2021-10-05 (Springboot - Board테이블 - 글 전체보기 수정 ) (0) | 2021.10.05 |
---|---|
2021-10-05 (Springboot - Board테이블 - 글삭제기능(패스워드 일치) ) (0) | 2021.10.05 |
2021-10-05 (Springboot - comment테이블 - 댓글 쓰기 ) (0) | 2021.10.05 |
2021-10-01 (Springboot - comment테이블 - 댓글 목록 불러오기 ) (0) | 2021.10.01 |
2021-10-01 (Springboot - mission Comment 기능 만들기 ) (0) | 2021.10.01 |