Skip to content

Commit

Permalink
Merge pull request #75 from lmw7414/feature/#66-child-comments
Browse files Browse the repository at this point in the history
[고도화] 댓글 기능 고도화 - 1차 대댓글
  • Loading branch information
lmw7414 authored Jan 15, 2023
2 parents 46bfb8c + 8365c61 commit 5600f21
Show file tree
Hide file tree
Showing 13 changed files with 808 additions and 397 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.core.annotation.Order;

import javax.persistence.*;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@ToString(callSuper = true)
Expand All @@ -29,21 +32,35 @@ public class ArticleComment extends AuditingFields {
@JoinColumn(name = "userId")
private UserAccount userAccount; // 유저 정보 (ID)

@Setter
@Column(updatable = false)
private Long parentCommentId; // 부모 댓글 ID

@ToString.Exclude
@OrderBy("createdAt ASC")
@OneToMany(mappedBy = "parentCommentId", cascade = CascadeType.ALL)
private Set<ArticleComment> childComments = new LinkedHashSet<>();

@Setter @Column(nullable = false, length = 500) private String content; // 본문


protected ArticleComment() {}

private ArticleComment(Article article, UserAccount userAccount, String content) {
private ArticleComment(Article article, UserAccount userAccount, Long parentCommentId, String content) {
this.article = article;
this.userAccount = userAccount;
this.parentCommentId = parentCommentId;
this.content = content;
}

public static ArticleComment of(Article article, UserAccount userAccount, String content) {
return new ArticleComment(article, userAccount, content);
return new ArticleComment(article, userAccount, null, content);
}

public void addChildComment(ArticleComment child) {
child.setParentCommentId(this.getId());
this.getChildComments().add(child);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public record ArticleCommentDto(
Long id,
Long articleId,
UserAccountDto userAccountDto,
Long parentCommentId,
String content,
LocalDateTime createdAt,
String createdBy,
Expand All @@ -18,17 +19,23 @@ public record ArticleCommentDto(
) {

public static ArticleCommentDto of(Long articleId, UserAccountDto userAccountDto, String content) {
return new ArticleCommentDto(null, articleId, userAccountDto, content, null, null, null, null);
return ArticleCommentDto.of( articleId, userAccountDto, null, content);
}
public static ArticleCommentDto of(Long id, Long articleId, UserAccountDto userAccountDto, String content, LocalDateTime createdAt, String createdBy, LocalDateTime modifiedAt, String modifiedBy) {
return new ArticleCommentDto(id, articleId, userAccountDto, content, createdAt, createdBy, modifiedAt, modifiedBy);

public static ArticleCommentDto of(Long articleId, UserAccountDto userAccountDto, Long parentCommentId, String content) {
return ArticleCommentDto.of(null, articleId, userAccountDto, parentCommentId, content, null, null, null, null);
}

public static ArticleCommentDto of(Long id, Long articleId, UserAccountDto userAccountDto,Long parentCommentId , String content, LocalDateTime createdAt, String createdBy, LocalDateTime modifiedAt, String modifiedBy) {
return new ArticleCommentDto(id, articleId, userAccountDto, parentCommentId, content, createdAt, createdBy, modifiedAt, modifiedBy);
}

public static ArticleCommentDto from(ArticleComment entity) {
return new ArticleCommentDto(
entity.getId(),
entity.getArticle().getId(),
UserAccountDto.from(entity.getUserAccount()),
entity.getParentCommentId(),
entity.getContent(),
entity.getCreatedAt(),
entity.getCreatedBy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
import com.fastcampus.projectboard.dto.ArticleCommentDto;
import com.fastcampus.projectboard.dto.UserAccountDto;

public record ArticleCommentRequest(Long articleId, String content) {
public record ArticleCommentRequest(
Long articleId,
Long parentCommentId,
String content
) {

public static ArticleCommentRequest of(Long articleId, String content) {
return new ArticleCommentRequest(articleId, content);
return ArticleCommentRequest.of(articleId, null ,content);
}

public static ArticleCommentRequest of(Long articleId,Long parentCommentId, String content) {
return new ArticleCommentRequest(articleId,parentCommentId ,content);
}

public ArticleCommentDto toDto(UserAccountDto userAccountDto) {
return ArticleCommentDto.of(
articleId,
userAccountDto,
parentCommentId,
content
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
import com.fastcampus.projectboard.dto.ArticleCommentDto;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public record ArticleCommentResponse(
Long id,
String content,
LocalDateTime createdAt,
String email,
String nickname,
String userId
String userId,
Long parentCommentId,
Set<ArticleCommentResponse> childComments
) {

public static ArticleCommentResponse of(Long id, String content, LocalDateTime createdAt, String email, String nickname, String userId) {
return new ArticleCommentResponse(id, content, createdAt, email, nickname, userId);
return ArticleCommentResponse.of(id, content, createdAt, email, nickname, userId, null);
}

public static ArticleCommentResponse of(Long id, String content, LocalDateTime createdAt, String email, String nickname, String userId, Long parentCommentId) {
Comparator<ArticleCommentResponse> childCommentComparator = Comparator
.comparing(ArticleCommentResponse::createdAt)
.thenComparingLong(ArticleCommentResponse::id);
return new ArticleCommentResponse(id, content, createdAt, email, nickname, userId, parentCommentId, new TreeSet<>(childCommentComparator));
}

public static ArticleCommentResponse from(ArticleCommentDto dto) {
Expand All @@ -23,14 +35,19 @@ public static ArticleCommentResponse from(ArticleCommentDto dto) {
nickname = dto.userAccountDto().userId();
}

return new ArticleCommentResponse(
return ArticleCommentResponse.of(
dto.id(),
dto.content(),
dto.createdAt(),
dto.userAccountDto().email(),
nickname,
dto.userAccountDto().userId()
dto.userAccountDto().userId(),
dto.parentCommentId()
);
}

public boolean hasParentComment() {
return parentCommentId != null;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.fastcampus.projectboard.dto.response;

import com.fastcampus.projectboard.dto.ArticleCommentDto;
import com.fastcampus.projectboard.dto.ArticleWithCommentsDto;
import com.fastcampus.projectboard.dto.HashtagDto;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public record ArticleWithCommentsResponse(
Expand Down Expand Up @@ -42,10 +43,31 @@ public static ArticleWithCommentsResponse from(ArticleWithCommentsDto dto) {
dto.userAccountDto().email(),
nickname,
dto.userAccountDto().userId(),
dto.articleCommentDtos().stream()
.map(ArticleCommentResponse::from)
.collect(Collectors.toCollection(LinkedHashSet::new))
organizeChildComments(dto.articleCommentDtos())
);
}

private static Set<ArticleCommentResponse> organizeChildComments(Set<ArticleCommentDto> dtos) {
Map<Long, ArticleCommentResponse> map = dtos.stream()
.map(ArticleCommentResponse::from)
.collect(Collectors.toMap(ArticleCommentResponse::id, Function.identity()));
map.values().stream()
.filter(ArticleCommentResponse::hasParentComment)
.forEach(comment -> {
ArticleCommentResponse parentComment = map.get(comment.parentCommentId());
parentComment.childComments().add(comment);
});

return map.values().stream()
.filter(comment -> !comment.hasParentComment())
.collect(Collectors.toCollection(() ->
new TreeSet<>(Comparator
.comparing(ArticleCommentResponse::createdAt)
.reversed()
.thenComparingLong(ArticleCommentResponse::id)
)

));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public void saveArticleComment(ArticleCommentDto dto) {
try {
Article article = articleRepository.getReferenceById(dto.articleId());
UserAccount userAccount = userAccountRepository.getReferenceById(dto.userAccountDto().userId());
articleCommentRepository.save(dto.toEntity(article, userAccount));
ArticleComment articleComment = dto.toEntity(article, userAccount);

if(dto.parentCommentId() != null) {
ArticleComment parentComment = articleCommentRepository.getReferenceById(dto.parentCommentId());
parentComment.addChildComment(articleComment);
} else {
articleCommentRepository.save(articleComment);
}
} catch (EntityNotFoundException e) {
log.warn("댓글 저장 샐패. 댓글 작성에 필요한 정보를 찾을 수 없습니다 - dto:{}", dto);
}
Expand Down
Loading

0 comments on commit 5600f21

Please sign in to comment.