댓글 삭제 기능을 구현하는 부분이 나에게 살짝 복잡했어서 별도로 기록으로 남긴다.
댓글 삭제는 대댓글이 있는 댓글을 삭제할 경우 DB에서 삭제하는 것이 아니라 삭제여부 컬럼에 삭제되었다는 표시(N -> Y)로 변경하고 화면에는 '삭제된 댓글입니다.'라고 표시하고자 했다.
그리고 대댓글이 없는 댓글과 댓글을 바로 삭제되도록 하고 싶었고, 삭제된 댓글이라고 표시한 댓글의 경우 대댓글이 전부 삭제되면 db에서도 삭제되도록 구현하고 싶었다.
처음에는 그냥 구현하였더니 원하는대로 작동되지 않았다. 그래서 흐름을 도식화한 뒤 구현하였다.
/** 댓글 삭제 */
public Long updateRemoveY(Long replyId) {
Reply reply = replyRepository.findById(replyId).orElseThrow(() -> new IllegalArgumentException("해당 댓글이 없습니다."));
if (!isNoChild(replyId)) {
reply.update('Y');
return replyId;
}
if (isNoParent(reply)) {
replyRepository.delete(reply);
return replyId;
}
if (!isParentRemoveY(reply)) {
replyRepository.delete(reply);
return replyId;
}
if (isChildCountGreaterThanZero(reply)) {
replyRepository.delete(reply);
return replyId;
}
replyRepository.delete(reply.getParent());
return replyId;
}
public List<ReplyResponseDto> findByPostId(Long postId) {
return replyRepository.findByPostId(postId).stream().map(ReplyResponseDto::new).collect(Collectors.toList());
}
public Long updateContent(Long replyId, String content) {
Reply reply = findById(replyId);
reply.update(content);
return replyId;
}
private boolean isChildCountGreaterThanZero(Reply reply) {
return reply.getReplies().size() > 0;
}
private boolean isNoParent(Reply reply) {
return reply.getParent()==null;
}
private boolean isParentRemoveY(Reply reply) {
return reply.getParent().getRemove().equals('Y');
}
private boolean isNoChild(Long replyId) {
Reply reply = findById(replyId);
return reply.getReplies().size() == 0;
}
private Reply findById(Long replyId) {
return replyRepository.findById(replyId).orElseThrow(() -> new IllegalArgumentException("해당 댓글이 없습니다."));
}
NextStep에서 박재성님의 자바 플레이그라운 with TDD, 클린코드를 수강 중인데, 거기서 배운 부분을 살짝 적용해보고자 했다.
배우는 단계라 많이 부족하겠지만, 그래도 내가 보기에는 조금은 코드가 가독성이 있고 깔끔하지 않나 싶다. 프로젝트를 진행하며 클린코드를 제대로 적용하지는 못했지만, 추후 리팩토링을 해보면 좋을 것 같다.
'SpringBoot > 개인프로젝트' 카테고리의 다른 글
https 적용 후 무중단 배포가 안되는 문제 (0) | 2023.05.02 |
---|---|
에러조치 (0) | 2023.05.01 |
Ajax가 작동이 되지 않는 문제 (0) | 2023.05.01 |
트위치 API 채널 검색 결과를 받아오는 과정에서의 문제 (0) | 2023.05.01 |
AWS EC2 https 적용 (1) | 2023.05.01 |