Skip to content

Commit

Permalink
Merge pull request #77 from Team-Shaka/feat/71
Browse files Browse the repository at this point in the history
Feat/71 대댓글 관련 로직 및 조회, 작성 API 구현
  • Loading branch information
HyoBN authored Jul 9, 2024
2 parents e7d9e3b + aff3a73 commit 599232c
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import treehouse.server.api.member.business.MemberMapper;
import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.comment.CommentType;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;

Expand All @@ -12,8 +13,21 @@
public class CommentMapper {


public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList) {
public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList,
List<CommentResponseDTO.ReplyInfoDto> replyInfoDtoList) {
return CommentResponseDTO.CommentInfoDto.builder()
.commentedAt(comment.getCreatedAt().toString())
.commentId(comment.getId())
.context(comment.getContent())
.reactionList(reactionList)
.replyList(replyInfoDtoList)
.memberProfile(MemberMapper.toGetWriterProfile(comment.getWriter()))
.build();

}

public static CommentResponseDTO.ReplyInfoDto toReplyInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList) {
return CommentResponseDTO.ReplyInfoDto.builder()
.commentedAt(comment.getCreatedAt().toString())
.commentId(comment.getId())
.context(comment.getContent())
Expand All @@ -29,11 +43,13 @@ public static CommentResponseDTO.CommentListDto toCommentListDto(List<CommentRes
.build();
}

public static Comment toComment(Member writer, Post post, String content) {
public static Comment toComment(Member writer, Post post, String content, CommentType type, Long parentId) {
return Comment.builder()
.writer(writer)
.post(post)
.content(content)
.type(type)
.parentId(parentId)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import treehouse.server.api.user.implement.UserQueryAdapter;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.comment.CommentType;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.reaction.Reaction;
Expand Down Expand Up @@ -87,13 +88,17 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long
Member member = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Pageable pageable = PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
List<Comment> commentListByPostId = commentQueryAdapter.getCommentListByPostId(postId, pageable);
// List<Comment> commentListByPostId = commentQueryAdapter.getCommentListByPostId(postId, pageable);
List<Comment> commentListByPostId = commentQueryAdapter.getParentCommentListByPostId(postId, pageable);

log.error("PO size : {}", commentListByPostId.size());
// 신고된 댓글을 필터링
List<Comment> filteredComments = commentListByPostId.stream()
.filter(comment -> !reportQueryAdapter.isReportedComment(comment))
.collect(Collectors.toList());

log.error("size : {}", filteredComments.size());

// 각 댓글마다 reactionList를 생성
List<CommentResponseDTO.CommentInfoDto> commentInfoDtoList = filteredComments.stream()
.map(comment -> {
Expand All @@ -111,10 +116,42 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long
));
ReactionResponseDTO.getReactionList reactionDtoList = ReactionMapper.toGetReactionList(reactionMap);

return CommentMapper.toCommentInfoDto(comment, reactionDtoList);
// 여기서 comment 에 대한 reply 목록을 조회해서 List<ReplyInfoDto> 를 만든 다음, mapper에 매개변수로 넣고, mapper 코드 업데이트 하자.

// 1. queryAdapter 에 parentId 가지고 childList 조회하는 메서드 만들기
List<Comment> childCommentList = commentQueryAdapter.getChildCommentListByPostIdAndParentId(postId, comment.getId(), pageable);

// 2. mapper 에 childList를 각각 comment 에서 ReplyInfoDto 로 바꾸는 메서드 만들기
// 각 답글마다 reactionList를 생성
List<CommentResponseDTO.ReplyInfoDto> replyInfoDtoList = childCommentList.stream()
.map(reply -> {
List<Reaction> replyReactions = reactionQueryAdapter.findAllByComment(reply);
Map<String, ReactionResponseDTO.getReaction> replyReactionMap = replyReactions.stream()
.collect(Collectors.toMap(
Reaction::getReactionName,
reaction -> {
String reactionName = reaction.getReactionName();
Integer reactionCount = reactionQueryAdapter.countReactionsByReactionNameAndCommentId(reactionName, reply.getId());
Boolean isPushed = reactionQueryAdapter.existByMemberAndCommentAndReactionName(member, reply, reactionName);
return ReactionMapper.toGetReaction(reaction, reactionCount, isPushed);
},
(existing, replacement) -> existing // 중복되는 경우 기존 값을 사용
));
ReactionResponseDTO.getReactionList replyReactionDtoList = ReactionMapper.toGetReactionList(replyReactionMap);

return CommentMapper.toReplyInfoDto(reply, replyReactionDtoList);
})
.collect(Collectors.toList());


// 3. mapper 에 이렇게 만든 childList 를 포함해서 최종 응답 형태인 CommentList 로 바꾸는 메서드 만들고 호출하기

return CommentMapper.toCommentInfoDto(comment, reactionDtoList,replyInfoDtoList);
})
.collect(Collectors.toList());

log.error("commentListDto size : {}", commentListByPostId.size());

CommentResponseDTO.CommentListDto commentListDto = CommentMapper.toCommentListDto(commentInfoDtoList);
return commentListDto;
}
Expand All @@ -125,11 +162,22 @@ public CommentResponseDTO.CommentIdResponseDto createComment(User user, Long tre
Post post = postQueryAdapter.findById(postId);
Member writer = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Comment comment = CommentMapper.toComment(writer, post, request.getContext());
Comment comment = CommentMapper.toComment(writer, post, request.getContext(), CommentType.PARENT, -1L);
Long commentId = commentCommandAdapter.createComment(comment).getId();
return CommentMapper.toIdResponseDto(commentId);
}

public CommentResponseDTO.CommentIdResponseDto createReply(User user, Long treehouseId, Long postId, Long parentId, CommentRequestDTO.createComment request){

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Post post = postQueryAdapter.findById(postId);
Member writer = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Comment comment = CommentMapper.toComment(writer, post, request.getContext(), CommentType.CHILD, parentId);
Long replyId = commentCommandAdapter.createComment(comment).getId();
return CommentMapper.toIdResponseDto(replyId);
}

public void deleteComment(User user, Long treehouseId, Long postId, Long commentId) {

Comment comment = commentQueryAdapter.getCommentById(commentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import treehouse.server.api.comment.business.CommentMapper;
import treehouse.server.api.comment.persistence.CommentRepository;
import treehouse.server.api.comment.presentation.dto.CommentResponseDTO;
import treehouse.server.api.reaction.business.ReactionMapper;
import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.comment.CommentType;
import treehouse.server.global.entity.reaction.Reaction;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.CommentException;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Adapter
@RequiredArgsConstructor
Expand All @@ -27,4 +35,15 @@ public Comment getCommentById(Long commentId){
public List<Comment> getCommentListByPostId(Long postId, Pageable pageable) {
return commentRepository.findAllByPostId(postId, pageable);
}

public List<Comment> getParentCommentListByPostId(Long postId, Pageable pageable) {
log.error("post id : {}",postId);
return commentRepository.findAllByPostIdAndType(postId, CommentType.PARENT, pageable);
}

public List<Comment> getChildCommentListByPostIdAndParentId(Long postId, Long parentId, Pageable pageable) {
return commentRepository.findAllByPostIdAndTypeAndParentId(postId, CommentType.CHILD, parentId, pageable);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.comment.CommentType;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findAllByPostId(Long postId, Pageable pageable);

List<Comment> findAllByPostIdAndType(Long postId, CommentType type, Pageable pageable);

List<Comment> findAllByPostIdAndTypeAndParentId(Long postId, CommentType type, Long parentId, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public CommonResponse<CommentResponseDTO.CommentIdResponseDto> createComment(
return CommonResponse.onSuccess(commentService.createComment(user, treehouseId, postId, request));
}

@PostMapping("/{commentId}")
@Operation(summary = "대댓글 작성 API 🔑", description = "특정 Comment에 대해서 대댓글을 작성하는 API 입니다.")
public CommonResponse<CommentResponseDTO.CommentIdResponseDto> createReply(
@PathVariable(name = "treehouseId")Long treehouseId,
@PathVariable(name = "postId")Long postId,
@PathVariable(name = "commentId")Long commentId,
@AuthMember @Parameter(hidden = true) User user,
@RequestBody CommentRequestDTO.createComment request
)
{
return CommonResponse.onSuccess(commentService.createReply(user, treehouseId, postId, commentId, request));
}

@DeleteMapping("/{commentId}")
@Operation(summary = "댓글 삭제 API 🔑", description = "댓글을 삭제하는 API 입니다.")
public CommonResponse deleteComment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,23 @@ public static class CommentInfoDto{
ReactionResponseDTO.getReactionList reactionList;
Long commentId;
String context;
List<ReplyInfoDto> replyList;
String commentedAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ReplyInfoDto{
MemberResponseDTO.getWriterProfile memberProfile;
ReactionResponseDTO.getReactionList reactionList;
Long commentId;
String context;
String commentedAt;
}


@Builder
@Getter
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public class Comment extends BaseDateTimeEntity {
@JoinColumn(name = "postId")
@ManyToOne(fetch = FetchType.LAZY)
private Post post;

@JoinColumn(name = "parentId")
private Long parentId;

@JoinColumn(name = "type")
@Enumerated(EnumType.STRING)
private CommentType type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package treehouse.server.global.entity.comment;

public enum CommentType {

PARENT, CHILD;
}

0 comments on commit 599232c

Please sign in to comment.