Skip to content

Commit

Permalink
Merge pull request #141 from chjcode/feat/comment
Browse files Browse the repository at this point in the history
Feat/comment
  • Loading branch information
chjcode authored Aug 10, 2024
2 parents a97763f + 580378e commit dbc586f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record CommentResponseDTO(
boolean deleted,
String createdDate,
String createdAt,
int depth,
List<CommentResponseDTO> childComments
) {
public static CommentResponseDTO from(Comment comment) {
Expand All @@ -24,6 +25,7 @@ public static CommentResponseDTO from(Comment comment) {
comment.isDeleted(),
comment.getCreatedAt().format(DateTimeFormatter.ofPattern("yy년 MM월 dd일")),
comment.getCreatedAt().format(DateTimeFormatter.ofPattern("HH:mm")),
comment.getDepth(),
comment.getChildComments().stream().map(CommentResponseDTO::from).collect(Collectors.toList())
);
}
Expand All @@ -36,6 +38,7 @@ public static CommentResponseDTO fromDeleted(Comment comment) {
true,
null,
null,
comment.getDepth(),
comment.getChildComments().stream().map(CommentResponseDTO::from).collect(Collectors.toList())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public record CommentSaveRequestDTO(
String content,
Long parentCommentId
) {
public Comment toEntity(Member member, Diary diary, Comment parentComment) {
public Comment toEntity(Member member, Diary diary, Comment parentComment, int depth) {
return Comment.builder()
.member(member)
.diary(diary)
.content(content)
.parentComment(parentComment)
.depth(depth)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ public class Comment extends BaseTimeEntity {

private boolean deleted;

@Column(nullable = false)
private int depth;

@Builder
protected Comment(Member member, Diary diary, String content, Comment parentComment) {
protected Comment(Member member, Diary diary, String content, Comment parentComment, int depth) {
this.member = member;
this.diary = diary;
this.content = content;
this.parentComment = parentComment;
this.deleted = false;
this.depth = depth;
}

public void update(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public CommentResponseDTO save(Long diaryId, CommentSaveRequestDTO requestDTO, L
Member member = getMemberById(logInMemberId);
Diary diary = getDiaryById(diaryId);
Comment parentComment = getParentCommentById(requestDTO.parentCommentId());
Comment comment = requestDTO.toEntity(member, diary, parentComment);

int commentDepth = calculateCommentDepth(parentComment);
validateCommentDepth(commentDepth);
Comment comment = requestDTO.toEntity(member, diary, parentComment, commentDepth);
commentDAO.save(comment);

return CommentResponseDTO.from(comment);
Expand Down Expand Up @@ -125,6 +128,13 @@ private Comment getCommentById(Long id) {
.orElseThrow(() -> new SystemException(ClientErrorCode.COMMENT_NOT_FOUND_ERR));
}

private int calculateCommentDepth(Comment parentComment) {
if (parentComment == null) {
return 0;
}
return parentComment.getDepth() + 1;
}

private void validateCommentAuthor(Member member, Comment comment) {
if (!member.equals(comment.getMember())) {
throw new SystemException(ClientErrorCode.AUTHOR_MISMATCH_ERR);
Expand All @@ -136,4 +146,10 @@ private void validateNotDeleted(Comment comment) {
throw new SystemException(ClientErrorCode.COMMENT_ALREADY_DELETED_ERR);
}
}

private void validateCommentDepth(int depth) {
if (depth > 1) {
throw new SystemException(ClientErrorCode.COMMENT_DEPTH_EXCEEDED_ERR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public DiarySaveResponseDTO update(Long id, DiaryUpdateRequestDTO requestDto) {
}

/**
* diaryId를 이용해서 diaryTag, MemberTag 를 찾아내기
* diaryTag 삭제 -> deleteAllInBatch 고려해보기
* MemberTag 삭제
* 해당 일기의 좋아요 찾기 및 삭제
* diaryId를 이용해서 diaryTag, MemberTag 를 찾아내기 diaryTag 삭제 -> deleteAllInBatch 고려해보기 MemberTag 삭제 해당 일기의 좋아요 찾기 및 삭제
* 이미지 삭제
*/
@Transactional
Expand Down Expand Up @@ -218,7 +215,7 @@ private void updateTags(Diary diary, List<String> newHashtags) {
private int makeRewardPoint() {
long seed = System.currentTimeMillis();
Random random = new Random(seed);
return random.nextInt(10) + 1;
return 50 + (random.nextInt(6) * 10);
}

private void saveRewardPointAndHistory(Member member, int rewardPoint) {
Expand Down Expand Up @@ -276,6 +273,7 @@ private void decrementTagsCount(List<TagList> tags) {
}
}
}

private void removeDiaryLikes(Long diaryId) {
List<DiaryLike> diaryLikes = diaryLikeDAO.findAllByDiaryId(diaryId);
diaryLikeDAO.deleteAll(diaryLikes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum ClientErrorCode implements ErrorCodeModel {
IMAGE_MAPPING_NOT_FOUND_ERR(400, "IMAGE_MAPPING_NOT_FOUND_ERR", "해당 이미지를 찾을 수 없습니다.(이미지 등록 값 없음)"),
AUTHOR_MISMATCH_ERR(403, "AUTHOR_MISMATCH_ERR", "작성자가 아닙니다."),
COMMENT_ALREADY_DELETED_ERR(400, "COMMENT_ALREADY_DELETED_ERR", "이미 삭제된 댓글입니다."),
COMMENT_DEPTH_EXCEEDED_ERR(400, "COMMENT_DEPTH_EXCEEDED_ERR", "댓글은 대댓글까지만 허용합니다."),

VALIDATION_ERR(400, "VALIDATION_ERR", "잘못된 입력입니다. 올바른 값을 입력해주세요."),
PERMISSION_ERR(403, "PERMISSION_ERR", "접근 권한이 없습니다. 관리자에게 문의하세요."),
Expand Down

0 comments on commit dbc586f

Please sign in to comment.