Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#324 refactor: Comment CRUD 수정
Browse files Browse the repository at this point in the history
- 변경된 entity에 따른 로직들 수정
- recomment 주석처리 (comment와 연관된)
djlim2425 committed Jan 13, 2025
1 parent de3f558 commit 8c81be7
Showing 9 changed files with 220 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -54,30 +54,37 @@ public ApiResponse<String> createComment(
return ApiResponse.onSuccess("댓글 생성 완료");
}

@PatchMapping("/{commentId}")
@Operation(summary = "댓글 내용 수정", description = "댓글 수정 API")
public ApiResponse<String> updateComment(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@PathVariable("commentId") Long commentId,
@RequestBody String newContent
) {
commentService.updateComment(principalDetail, commentId, newContent);
return ApiResponse.onSuccess("댓글 수정 완료");
}



// 댓글 삭제 (대댓글 O)
@PatchMapping("/{commentId}")
@PatchMapping("/{commentId}/soft-delete")
@Operation(summary = "댓글 소프트 삭제", description = "댓글 id로 대댓글이 있는 댓글에 대해 소프트삭제(isDelete변경)")
public ApiResponse<String> updateCommentStatus(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@PathVariable("commentId") Long commentId) {

commentService.updateCommentStatus(principalDetail, commentId);
commentService.softDeleteComment(principalDetail, commentId);

return ApiResponse.onSuccess("댓글 삭제 완료");
}


// 댓글 삭제 (대댓글 X)
@DeleteMapping("/{commentId}")
@Operation(summary = "댓글 하드 삭제", description = "댓글 id로 하드삭제 - 대댓글이 없는 경우 사용")
public ApiResponse<String> deleteComment(
public ApiResponse<String> hardDeleteComment(
@AuthenticationPrincipal PrincipalDetail principalDetail,
@PathVariable("commentId") Long commentId) {

commentService.deleteComment(principalDetail, commentId);
commentService.hardDeleteComment(principalDetail, commentId);

return ApiResponse.onSuccess("댓글 삭제 완료");
}
Original file line number Diff line number Diff line change
@@ -15,9 +15,13 @@
public class CommentRequestDTO {
private Long partyId;
private String content;
private Long parentCommentId;


public static Comment toEntity(CommentRequestDTO commentRequest, Party party, Member member) {
return Comment.create(member, party, commentRequest.getContent());
public static Comment toEntity(CommentRequestDTO request, Party party, Member member, Comment parent) {
Comment newComment = Comment.create(member, party, request.getContent());
if (parent != null) {
parent.addChild(newComment);
}
return newComment;
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@@ -19,7 +20,7 @@ public class CommentResponseDTO {
private String memberName;
private String content;
private boolean isDeleted;
private List<RecommentResponseDTO> recomments;
private List<CommentResponseDTO> children = new ArrayList<>();
private String timeAgo;
private String createdDate;
//private String url;
@@ -33,25 +34,21 @@ public CommentResponseDTO(Long id, Long memberId, String memberName, String cont
this.isDeleted = isDeleted;
}

public static CommentResponseDTO fromEntity(Comment comment, String timeAgo, String createdDate, List<RecommentResponseDTO> recommentDTOs) {
public static CommentResponseDTO fromEntity(Comment comment, String timeAgo, String createdDate) {
// isDeleted일시 content로 삭제된 댓글입니다 전달
String finalContent = comment.isDeleted() ? "삭제된 댓글입니다." : comment.getContent();

return CommentResponseDTO.builder()
.id(comment.getId())
.memberId(comment.getMember().getId())
.memberName(comment.getMember().getUsername())
.content(comment.getContent())
.memberId(comment.getMember() != null ? comment.getMember().getId() : null)
.memberName(comment.getMember() != null ? comment.getMember().getUsername() : null)
.content(finalContent)
.isDeleted(comment.isDeleted())
.timeAgo(timeAgo)
.createdDate(createdDate)
.recomments(recommentDTOs)
.children(new ArrayList<>())
.build();
}

public static Comment setDeleted(Comment comment) {
comment = Comment.builder()
.party(comment.getParty())
.member(comment.getMember())
.content("삭제된 댓글입니다.")
.build();
return comment;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.drinkeg.drinkeg.domain.comment.repository;

import com.drinkeg.drinkeg.domain.comment.domain.Comment;
import com.drinkeg.drinkeg.domain.comment.domain.QComment;
import com.drinkeg.drinkeg.domain.recomment.domain.QRecomment;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@@ -19,36 +17,40 @@ public class CommentRepositoryImpl implements CommentRepositoryCustom {
@Override
public long countCommentsAndRecommentsByPartyId(Long partyId) {
QComment comment = QComment.comment;
QRecomment recomment = QRecomment.recomment;

// 댓글 수 카운트 (삭제되지 않은 댓글만)
Long commentCount = queryFactory.select(comment.count())
Long count = queryFactory
.select(comment.count())
.from(comment)
.where(comment.party.id.eq(partyId)
.and(comment.isDeleted.isFalse()))
.fetchOne();

// 대댓글 수 카운트 (부모 댓글의 삭제 여부와 관계없이 모든 대댓글)
Long recommentCount = queryFactory.select(recomment.count())
.from(recomment)
.where(recomment.comment.party.id.eq(partyId))
.where(
comment.party.id.eq(partyId),
comment.isDeleted.isFalse()
)
.fetchOne();

// null 방지
commentCount = commentCount != null ? commentCount : 0L;
recommentCount = recommentCount != null ? recommentCount : 0L;

return commentCount + recommentCount;
return count != null ? count : 0L;
}


// @Override
// public List<Comment> findCommentsWithRecomments(Long partyId) {
// QComment comment = QComment.comment;
// QRecomment recomment = QRecomment.recomment;
//
// return queryFactory.selectFrom(comment)
// .leftJoin(recomment).on(comment.id.eq(recomment.comment.id))
// .fetchJoin()
// .distinct()
// .fetch();
// }
@Override
public List<Comment> findCommentsWithRecomments(Long partyId) {
QComment comment = QComment.comment;
QRecomment recomment = QRecomment.recomment;

return queryFactory.selectFrom(comment)
.leftJoin(recomment).on(comment.id.eq(recomment.comment.id))
.fetchJoin()
return queryFactory
.selectFrom(comment)
.leftJoin(comment.children, comment).fetchJoin()
.leftJoin(comment.member).fetchJoin()
.where(
comment.party.id.eq(partyId),
comment.parent.isNull() // 루트댓글ㄹ
)
.distinct()
.fetch();
}
Original file line number Diff line number Diff line change
@@ -12,15 +12,17 @@ public interface CommentService {

Comment findByIdOrThrow(Long commentId);

List<CommentResponseDTO> getCommentsByPartyId(Long partyId);

long countCommentsAndRecommentsByPartyId(Long partyId);

void createComment(PrincipalDetail principalDetail, CommentRequestDTO commentRequest);

List<CommentResponseDTO> getCommentsByPartyId(Long partyId);
void updateComment(PrincipalDetail principalDetail, Long commentId, String newContent);

void deleteComment(PrincipalDetail principalDetail, Long commentId);
void softDeleteComment(PrincipalDetail principalDetail, Long commentId);

void updateCommentStatus(PrincipalDetail principalDetail, Long commentId);
void hardDeleteComment(PrincipalDetail principalDetail, Long commentId);

String calculateTimeAgo(LocalDateTime createdAt);

Loading

0 comments on commit 8c81be7

Please sign in to comment.