롤 전적 사이트 구현 - 댓글(1)

essential·2023년 8월 29일
1

op.gg

목록 보기
10/16
@Getter
@Setter
public class CommentDTO {

    private int cno;
    private int bno;
    private int parent_cno;
    private String content;
    private String id;
    private int depth;
    private String comment_date;
    private int group_cno;

    public CommentEntity toComment() { // CommentEntity 에 Data 넣기
        CommentEntity comment = new CommentEntity();
        comment.setCno(this.getCno());
        comment.setBno(this.getBno());
        comment.setGroup_cno(this.getGroup_cno());
        comment.setParent_cno(this.getParent_cno());
        comment.setContent(this.getContent());
        comment.setId(this.getId());
        comment.setDepth(this.getDepth());
        comment.setComment_date(this.getComment_date());
        return comment;
    }
    
    public void fromComment(CommentEntity comment) { // CommentEntity 에서 Data 꺼내기
        this.setCno(comment.getCno());
        this.setBno(comment.getBno());
        this.setGroup_cno(comment.getGroup_cno());
        this.setParent_cno(comment.getParent_cno());
        this.setContent(comment.getContent());
        this.setId(comment.getId());
        this.setDepth(comment.getDepth());
        this.setComment_date((comment.getComment_date()));
    }
}

@Override
    public int insertComment(CommentDTO dto) {
        CommentEntity comment = dto.toComment();

        if(comment.getCno() != 0) { // cno가 존재하면 대댓글.
            handleReply(comment);
        } else { // cno가 없으면 원본 댓글.
            handleComment(comment);
        }
        return 1;
    }

    private void handleComment(CommentEntity comment) {
        comment.setDepth(0);
        comment.setParent_cno(0);
        mapper.insertComment(comment);
        comment.setGroup_cno(comment.getCno()); // group_cno를 생성된 cno로 설정
        mapper.updateGroupCno(comment);
    }

    private void handleReply(CommentEntity comment) {
        comment.setParent_cno(comment.getCno());
        comment.setDepth(comment.getDepth() + 1);
        comment.setGroup_cno(comment.getGroup_cno());
        mapper.insertComment(comment);
    }

cno 로 댓글인지 대댓글인지 판단해서 데이터 설정을 다르게 하고 있다

댓글인 경우

  • 깊이 0 (들여쓰기 X)
  • 부모 cno 0 (댓글이니까)
  • 설정한 값대로 insert
  • insert 하고 나온 cno 를 group에 넣어줌
  • group update

대댓글인 경우

  • 부모의 번호 가져오기(getCno)
  • Depth 가져와서 +1 (들여쓰기 O)
  • 부모의 group_cno 가져와서 자식의 Group_cno 넣어줌 (대댓글의 group_cno 설정)
  • insert

++
쿼리

@Select("SELECT * FROM comments WHERE bno = #{bno} ORDER BY group_cno ASC, parent_cno ASC, comment_date ASC;")

첫대댓의 댓글이 이상하게 나온다.. 첫대댓에 아래에 있어야하는데 왜 두대댓에 들어갔을까?
두대댓에 대댓글을 쓰고 첫대댓에 또 대댓을 달면 순서가

첫대댓
두대댓
(들여쓰기)두대댓의 첫댓
(들여쓰기)첫대댓의 첫댓
이딴식으로 나옴,,, 아아아아아아ㅏ아ㅏ아ㅏㄱ

profile
essential

0개의 댓글