public int insertComment(CommentDTO dto) {
CommentEntity comment = dto.toComment();
if (comment.getCno() != 0) { // 대댓글
handleReply(comment);
} else { // 댓글
handleComment(comment);
}
return 1;
}
private void handleComment(CommentEntity comment) {
comment.setDepth(0);
comment.setParent_cno(0);
comment.setOrder_cno(0); //
mapper.insertComment(comment);
comment.setGroup_cno(comment.getCno()); // group_cno를 새로 생성된 cno로 설정
mapper.updateGroupCno(comment);
}
private void handleReply(CommentEntity comment) {
comment.setDepth(comment.getDepth() + 1);
// 부모 댓글의 바로 다음 order_cno 값을 찾기
int nextOrderCno = mapper.getOrderCno(comment.getCno()) + 1;
// nextOrderCno 값과 동일하거나 큰 order_cno를 가진 모든 댓글의 order_cno 값을 1씩 증가
mapper.upOrderCno(comment.getBno(), nextOrderCno);
comment.setOrder_cno(nextOrderCno);
comment.setParent_cno(comment.getCno());
comment.setGroup_cno(comment.getGroup_cno());
mapper.insertComment(comment);
}
Mapper
@Insert("INSERT INTO comments(bno, Group_cno, parent_cno, content, depth, id, comment_date, order_cno) VALUES(#{bno}, #{group_cno}, #{parent_cno}, #{content}, #{depth}, #{id}, NOW(), #{order_cno})")
@Options(useGeneratedKeys = true, keyProperty = "cno")
public int insertComment(CommentEntity commen);
@Select("SELECT `order_cno` FROM comments WHERE cno = #{cno}")
public int getOrderCnoParent(int cno);
@Update("UPDATE comments SET `order_cno` = `order_cno` + 1 WHERE bno = #{bno} AND `order_cno` >= #{startOrderCno}")
public void upOrderCno(int bno, int startOrderCno);
@Update("UPDATE comments SET group_cno = #{group_cno} WHERE cno = #{cno}")
void updateGroupCno(CommentEntity comment);
대댓글과 대대댓글 정렬 꼬임
그래서 오라클 connect by 쓰기로 했는데 이건 정말 하다하다 안되면 해야지
참고할 사이트
https://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/