JPA를 활용해 CRUD 기능을 만들어보자
8. 답글 기능 추가로 인해 =>
- 원글 입력 시 maxID 값 조회 후 직접 지정 (ReplyRootId 적용하기 위해서)
- 페이징 수정 (최근 작성된 원글, 답글 구조화 하여 sorting 되도록)
8.1 원글 입력 시 maxID 값 조회 후 직접 지정
BoardJpaService
public Board postBoard(BoardDTO boardDTO) {
int newBoardIdValue = this.getNewBoardIdValue(boardRepository);
log.debug("newBoardIdValue="+newBoardIdValue);
Board postData = Board.builder()
.id(newBoardIdValue)
.author(boardDTO.getAuthor())
.subject(boardDTO.getSubject())
.content(boardDTO.getContent())
.password(boardDTO.getPassword())
.commentCount(0)
.depth(0)
.orderNum(0)
.isDel("N")
.readCount(0)
.replyRootId(newBoardIdValue)
.writeDate(LocalDate.now())
.writeTime(LocalTime.now())
.build();
return boardRepository.save(postData);
}
private int getNewBoardIdValue(BoardRepository boardRepository) {
int result;
Board boardOfMaxId = boardRepository.findTopByOrderByIdDesc();
if(boardOfMaxId == null) {
result = 1;
log.debug("no board data, maxId is 1");
} else {
result = boardOfMaxId.getId() + 1;
log.debug("maxIdFromBoard="+boardOfMaxId.getId());
}
log.debug("newBoardIdValue="+result);
return result;
}
- getNewBoardIdValue(BoardRepository boardRepository) 메소드를 통해 Board 테이블에 id 값이 존재하지않으면 result에 1을 넣어주고 만약 id 값이 존재한다면 가장 큰 ID 값에 + 1을 한 값을 result 넣어준다.
- 결과 값을 newBoardIdValue에 넣어주고 id값과 replyRootId 값을 지정해준다
BoardRepository
// 최고 Id값을 가진 Board 엔터티를 가져옴
Board findTopByOrderByIdDesc();
8.2 페이징 수정
BoardJpaService
public Page<Board> getBoardList(int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
// 페이지네이션 + 정렬조건 + 다른 개발자 부여 조건 하여 List하는 여러 방법들..
// https://dar0m.tistory.com/61
// 그외 쿼리메소드로 작성하여 바로 적용.
return boardRepository.findBoardsByIsDelOrderByReplyRootIdDescOrderNumAsc("N", pageRequest);
}
- ReplyRootId 내림차순, OrderNum 오름차순
BoardRepository
Page<Board> findBoardsByIsDelOrderByReplyRootIdDescOrderNumAsc(String isDel, Pageable pageable);
Postman
{
"success": true,
"data": {
"content": [
{
"id": 19,
"author": "replyAuthor",
"subject": "RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "15:31:37",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 2,
"orderNum": 1,
"isDel": "N"
},
{
"id": 20,
"author": "replyAuthor",
"subject": "RE : RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "15:44:33",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 3,
"orderNum": 2,
"isDel": "N"
},
{
"id": 23,
"author": "replyAuthor",
"subject": "RE : RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:23:45",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 3,
"orderNum": 3,
"isDel": "N"
},
{
"id": 24,
"author": "replyAuthor",
"subject": "RE : RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:25:03",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 3,
"orderNum": 4,
"isDel": "N"
},
{
"id": 25,
"author": "replyAuthor",
"subject": "RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:26:43",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 2,
"orderNum": 5,
"isDel": "N"
},
{
"id": 31,
"author": "replyAuthor",
"subject": "RE : RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:33:18",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 2,
"orderNum": 6,
"isDel": "N"
},
{
"id": 26,
"author": "replyAuthor",
"subject": "RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:26:49",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 1,
"orderNum": 7,
"isDel": "N"
},
{
"id": 27,
"author": "replyAuthor",
"subject": "RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:26:54",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 1,
"orderNum": 8,
"isDel": "N"
},
{
"id": 28,
"author": "replyAuthor",
"subject": "RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:27:02",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 1,
"orderNum": 9,
"isDel": "N"
},
{
"id": 29,
"author": "replyAuthor",
"subject": "RE : replySubject",
"content": "replyContent",
"writeDate": "2021-11-03",
"writeTime": "17:27:07",
"readCount": 0,
"commentCount": 0,
"password": "0000",
"replyRootId": 11,
"depth": 1,
"orderNum": 10,
"isDel": "N"
}
],
"pageable": {
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"offset": 10,
"pageSize": 10,
"pageNumber": 1,
"unpaged": false,
"paged": true
},
"totalPages": 3,
"last": false,
"totalElements": 26,
"number": 1,
"size": 10,
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"numberOfElements": 10,
"first": false,
"empty": false
},
"message": null
}
'Springboot' 카테고리의 다른 글
SpringBoot+Vue.js - 회원가입 (0) | 2022.02.24 |
---|---|
2021-11-04 (Springboot - 공공데이터 api 활용(2)) (0) | 2021.11.04 |
2021-11-01 (Springboot - JPA - CRUD(9) ) (0) | 2021.11.03 |
2021-10-27 (Springboot - JPA - CRUD(8) ) (0) | 2021.11.02 |
2021-10-27 (Springboot - JPA - CRUD(7) ) (0) | 2021.10.27 |