Skip to content

Commit

Permalink
Merge pull request #51 from Team-Wable/feat/#50
Browse files Browse the repository at this point in the history
[FEAT] 대댓글 목록 조회 및 삭제 API 구현
  • Loading branch information
Hong0329 authored Nov 19, 2024
2 parents ccfd546 + d38ed90 commit 04ac82f
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ public ResponseEntity<ApiResponse<Object>> postCommentWithParentChildComment(Pri
commentCommendService.postCommentWithParentChildComment(MemberUtil.getMemberId(principal),contentId, commentPostRequestDtoVer2);
return ApiResponse.success(POST_COMMENT_SUCCESS);
}

@Operation(summary = "멤버에 해당하는 답글 리스트 조회 API(+블라인드) 입니다.", description = "Comments By Member With Blind")
@GetMapping("v3/member/{memberId}/comments")
public ResponseEntity<ApiResponse<Object>> getCommentAllByMemberWithBlind(Principal principal, @PathVariable Long memberId, @RequestParam(value = "cursor") Long cursor){
Long usingMemberId = MemberUtil.getMemberId(principal);
return ApiResponse.success(GET_MEMBER_COMMENT_SECCESS, commentQueryService.getCommentAllByMemberWithBlind(usingMemberId,memberId,cursor));
}

@Operation(summary = "게시물에 해당하는 답글 리스트 조회 API(대댓글) 입니다.", description = "Comments By Content With hierarchy")
@GetMapping("v3/content/{contentId}/comments")
public ResponseEntity<ApiResponse<Object>> getCommentAllWithHierarchy(Principal principal, @PathVariable Long contentId, @RequestParam(value = "cursor") Long cursor){
Long memberId = MemberUtil.getMemberId(principal);
return ApiResponse.success(GET_COMMENT_ALL_SUCCESS, commentQueryService.getCommentsWithHierarchy(memberId, contentId, cursor));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.wable.www.WableServer.api.comment.dto.response;

import com.wable.www.WableServer.api.comment.domain.Comment;
import com.wable.www.WableServer.api.member.domain.Member;
import com.wable.www.WableServer.common.util.TimeUtilCustom;

public record CommentAllByMemberResponseDtoVer3(
long memberId, // 답글 작성자 Id
String memberProfileUrl, // 작성자 프로필 사진url
String memberNickname, // 답글 작성자 닉네임
Boolean isLiked, // 유저가 답글에 대해 좋아요를 눌렀는지
Boolean isGhost, //답글 작성자를 투명도 처리 했는지
int memberGhost, // 답글 작성자의 전체 투명도
int commentLikedNumber, // 답글 좋아요 개수
String commentText, // 답글 내용
String time, //답글이 작성된 시간 (년-월-일 시:분:초)
long commentId, //댓글 Id
long contentId , // 해당 댓글이 적힌 게시물 Id
String commentImageUrl,
String memberFanTeam,
Boolean isBlind
) {
public static CommentAllByMemberResponseDtoVer3 of(Member writerMember, boolean isLiked, boolean isGhost,
int memberGhost, int commentLikedNumber, Comment comment){
return new CommentAllByMemberResponseDtoVer3(
writerMember.getId() ,
writerMember.getProfileUrl(),
writerMember.getNickname(),
isLiked,
isGhost,
memberGhost,
commentLikedNumber,
comment.getCommentText(),
TimeUtilCustom.refineTime(comment.getCreatedAt()),
comment.getId(),
comment.getContent().getId(),
comment.getCommentImage(),
writerMember.getMemberFanTeam(),
comment.isBlind()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.wable.www.WableServer.api.comment.dto.response;

import com.wable.www.WableServer.api.member.domain.Member;

import java.util.List;

public record CommentAllResponseDtoVer4(
Long commentId, //답글 고유 id
Long memberId, // 댓글 작성자 Id
String memberProfileUrl, //작성자 프로필 사진url
String memberNickname, //댓글 작성자 닉네임
Boolean isGhost, //현재 유저가 작성자에 대해 투명도 처리를 했는지
int memberGhost, //작성자의 전체 투명도
Boolean isLiked, //유저가 게시물에 대해 좋아요를 눌렀는지
int commentLikedNumber, //댓글의 좋아요 개수
String commentText, //댓글 내용
String time, //답글이 작성된 시간을 (년-월-일 시:분:초)
Boolean isDeleted, // 댓글 작성자가 탈퇴한 회원인지 아닌지
String commentImageUrl,
String memberFanTeam,
Long parentCommentId,
Boolean isBlind,
List<CommentAllResponseDtoVer4> childComments // 대댓글 리스트 추가

) {
public static CommentAllResponseDtoVer4 of(
Long commentId, Member writerMember, boolean isGhost, int memberGhost,
boolean isLiked, String time, int likedNumber, String commentText,
String commentImageUrl,Long parentCommentId, Boolean isBlind, List<CommentAllResponseDtoVer4> childComments
) {
return new CommentAllResponseDtoVer4(
commentId,
writerMember.getId(),
writerMember.getProfileUrl(),
writerMember.getNickname(),
isGhost,
memberGhost,
isLiked,
likedNumber,
commentText,
time,
writerMember.isDeleted(),
commentImageUrl,
writerMember.getMemberFanTeam(),
parentCommentId,
isBlind,
childComments
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ default Comment findCommentByIdOrThrow(Long commentId) {

List<Comment> findAllByMember(Member member);

@Query("""
SELECT c
FROM Comment c
WHERE c.parentCommentId = -1 AND c.content.id = :contentId AND (:cursor = -1 OR c.id > :cursor)
ORDER BY c.createdAt ASC
""")
Slice<Comment> findParentCommentsWithPaginationAfterCursor(Long cursor, Long contentId, PageRequest pageRequest);

@Query("""
SELECT c
FROM Comment c
WHERE c.parentCommentId = :parentId
ORDER BY c.createdAt ASC
""")
List<Comment> findChildComments(Long parentId);

@Transactional
@Modifying
@Query("DELETE FROM Comment c WHERE c.isDeleted = true AND c.deleteAt < :currentDate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.wable.www.WableServer.external.fcm.service.FcmService;
import com.wable.www.WableServer.external.s3.service.S3Service;
import java.io.IOException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -163,11 +165,27 @@ public void postCommentVer2(Long memberId, Long contentId, MultipartFile comment
public void deleteComment(Long memberId, Long commentId) {
deleteValidate(memberId, commentId);

notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentLiked",commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("comment",commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentGhost", commentId);

Comment deleteComment = commentRepository.findCommentByIdOrThrow(commentId);
if(deleteComment.getParentCommentId().equals(-1L)) {
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentLiked", commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("comment", commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentGhost", commentId);

List<Comment> childComment = commentRepository.findChildComments(commentId);

for (Comment comment : childComment) {
Long childCommentId = comment.getId();
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("childCommentLiked", childCommentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("childComment", childCommentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentGhost", childCommentId);
comment.softDelete();
}
} else {
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("childCommentLiked", commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("childComment", commentId);
notificationRepository.deleteByNotificationTriggerTypeAndNotificationTriggerId("commentGhost", commentId);
}

deleteComment.softDelete();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.wable.www.WableServer.api.comment.service;

import com.wable.www.WableServer.api.comment.domain.Comment;
import com.wable.www.WableServer.api.comment.dto.response.CommentAllByMemberResponseDto;
import com.wable.www.WableServer.api.comment.dto.response.CommentAllByMemberResponseDtoVer2;
import com.wable.www.WableServer.api.comment.dto.response.CommentAllResponseDto;
import com.wable.www.WableServer.api.comment.dto.response.CommentAllResponseDtoVer2;
import com.wable.www.WableServer.api.comment.dto.response.CommentAllResponseDtoVer3;
import com.wable.www.WableServer.api.comment.dto.response.*;
import com.wable.www.WableServer.api.comment.repository.CommentLikedRepository;
import com.wable.www.WableServer.api.comment.repository.CommentRepository;
import com.wable.www.WableServer.api.content.repository.ContentRepository;
Expand All @@ -19,6 +15,8 @@
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

Expand Down Expand Up @@ -154,6 +152,78 @@ public List<CommentAllByMemberResponseDtoVer2> getCommentAllByMemberWithImage(Lo
).collect(Collectors.toList());
}

public List<CommentAllByMemberResponseDtoVer3> getCommentAllByMemberWithBlind(Long principalId, Long memberId, Long cursor) {
memberRepository.findMemberByIdOrThrow(memberId);

PageRequest pageRequest = PageRequest.of(0, 10);
Slice<Comment> commentList;

if (cursor==-1) {
commentList = commentRepository.findCommentsTop15ByMemberIdOrderByCreatedAtDesc(memberId, pageRequest);
} else {
commentList = commentRepository.findCommentsByMemberNextPage(cursor, memberId, pageRequest);
}

return commentList.stream()
.map(oneComment -> CommentAllByMemberResponseDtoVer3.of(
memberRepository.findMemberByIdOrThrow(memberId),
checkLikedComment(principalId, oneComment.getId()),
checkGhost(principalId, oneComment.getId()),
checkMemberGhost(oneComment.getId()),
likedNumber(oneComment.getId()),
oneComment)
).collect(Collectors.toList());
}

public List<CommentAllResponseDtoVer4> getCommentsWithHierarchy(Long memberId, Long contentId, Long cursor) {
PageRequest pageRequest = PageRequest.of(0, COMMENT_DEFAULT_PAGE_SIZE);
Slice<Comment> parentComments = commentRepository.findParentCommentsWithPaginationAfterCursor(cursor, contentId, pageRequest);

// 결과 리스트 초기화
List<CommentAllResponseDtoVer4> result = new ArrayList<>();

for (Comment parent : parentComments) {
// 대댓글 조회 및 변환
List<Comment> childComments = commentRepository.findChildComments(parent.getId());

List<CommentAllResponseDtoVer4> childDtos = childComments.stream()
.map(child -> CommentAllResponseDtoVer4.of(
child.getId(),
memberRepository.findMemberByIdOrThrow(child.getMember().getId()),
checkGhost(memberId, child.getId()),
checkMemberGhost(child.getId()),
checkLikedComment(memberId, child.getId()),
TimeUtilCustom.refineTime(child.getCreatedAt()),
likedNumber(child.getId()),
child.getCommentText(),
child.getCommentImage(),
child.getParentCommentId(),
child.isBlind(),
null // 대댓글의 대댓글은 없으므로 null로 설정
))
.collect(Collectors.toList());

// 부모 댓글 DTO 변환 (대댓글 포함)
CommentAllResponseDtoVer4 parentDto = CommentAllResponseDtoVer4.of(
parent.getId(),
memberRepository.findMemberByIdOrThrow(parent.getMember().getId()),
checkGhost(memberId, parent.getId()),
checkMemberGhost(parent.getId()),
checkLikedComment(memberId, parent.getId()),
TimeUtilCustom.refineTime(parent.getCreatedAt()),
likedNumber(parent.getId()),
parent.getCommentText(),
parent.getCommentImage(),
parent.getParentCommentId(),
parent.isBlind(),
childDtos // 대댓글 추가
);

result.add(parentDto);
}
return result;
}

private boolean checkGhost(Long usingMemberId, Long commentId) {
Member writerMember = commentRepository.findCommentByIdOrThrow(commentId).getMember();
return ghostRepository.existsByGhostTargetMemberIdAndGhostTriggerMemberId(writerMember.getId(), usingMemberId);
Expand Down

0 comments on commit 04ac82f

Please sign in to comment.