Skip to content

Commit

Permalink
Merge pull request #316 from depromeet/fix/#315-withdraw-comment
Browse files Browse the repository at this point in the history
fix: 회원 탈퇴 시 탈퇴자 댓글 예외 처리
  • Loading branch information
char-yb authored Oct 22, 2024
2 parents 86387c2 + f383837 commit ec66a63
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.depromeet.stonebed.domain.auth.dto.response.AuthTokenResponse;
import com.depromeet.stonebed.domain.auth.dto.response.SocialClientResponse;
import com.depromeet.stonebed.domain.auth.dto.response.TokenPairResponse;
import com.depromeet.stonebed.domain.comment.dao.CommentRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmNotificationRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmTokenRepository;
import com.depromeet.stonebed.domain.member.dao.MemberRepository;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class AuthService {
private final MissionRecordRepository missionRecordRepository;
private final MissionRecordBoostRepository missionRecordBoostRepository;
private final FcmTokenRepository fcmTokenRepository;
private final CommentRepository commentRepository;

private final AppleClient appleClient;
private final KakaoClient kakaoClient;
Expand Down Expand Up @@ -178,6 +180,7 @@ private void updateMemberNormalStatus(Member member) {
}

private void withdrawMemberRelationByMemberId(List<Long> recordIds, Long memberId) {
commentRepository.updateEmptyMemberAllByMember(memberId);
missionRecordBoostRepository.deleteAllByMember(recordIds);
missionRecordRepository.deleteAllByMember(memberId);
fcmNotificationRepository.deleteAllByMember(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ private Comment createAndSaveComment(
final Comment comment =
request.parentId() != null
? Comment.createComment(
missionRecord,
missionRecord.getId(),
member,
request.content(),
findCommentById(request.parentId()))
: Comment.createComment(missionRecord, member, request.content(), null);
: Comment.createComment(
missionRecord.getId(), member, request.content(), null);
return commentRepository.save(comment);
}

Expand Down Expand Up @@ -200,6 +201,7 @@ private List<CommentFindOneResponse> convertToCommentFindOneResponses(

private CommentFindOneResponse convertToCommentFindOneResponse(
Comment comment, Map<Long, List<Comment>> commentsByParentId) {

List<CommentFindOneResponse> replyCommentsResponses =
commentsByParentId.getOrDefault(comment.getId(), List.of()).stream()
.map(
Expand All @@ -208,13 +210,24 @@ private CommentFindOneResponse convertToCommentFindOneResponse(
childComment, commentsByParentId))
.collect(Collectors.toList());

// 작성자가 null인지 확인
Long writerId = comment.getWriter() != null ? comment.getWriter().getId() : null;
String writerNickname =
comment.getWriter() != null
? comment.getWriter().getProfile().getNickname()
: "탈퇴한 회원";
String writerProfileImageUrl =
comment.getWriter() != null
? comment.getWriter().getProfile().getProfileImageUrl()
: null;

return CommentFindOneResponse.of(
comment.getParent() != null ? comment.getParent().getId() : null,
comment.getId(),
comment.getContent(),
comment.getWriter().getId(),
comment.getWriter().getProfile().getNickname(),
comment.getWriter().getProfile().getProfileImageUrl(),
writerId,
writerNickname,
writerProfileImageUrl,
comment.getCreatedAt().toString(),
replyCommentsResponses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface CommentRepositoryCustom {
List<Comment> findAllCommentsByMissionRecord(MissionRecord missionRecord);

void updateEmptyMemberAllByMember(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.depromeet.stonebed.domain.comment.domain.QComment.comment;

import com.depromeet.stonebed.domain.comment.domain.Comment;
import com.depromeet.stonebed.domain.member.domain.Member;
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
Expand All @@ -21,7 +22,16 @@ public List<Comment> findAllCommentsByMissionRecord(MissionRecord missionRecord)
.fetchJoin()
.leftJoin(comment.replyComments)
.fetchJoin()
.where(comment.missionRecord.eq(missionRecord))
.where(comment.recordId.eq(missionRecord.getId()))
.fetch();
}

@Override
public void updateEmptyMemberAllByMember(Long memberId) {
queryFactory
.update(comment)
.set(comment.writer, (Member) null)
.where(comment.writer.id.eq(memberId))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.depromeet.stonebed.domain.common.BaseTimeEntity;
import com.depromeet.stonebed.domain.member.domain.Member;
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -33,13 +32,13 @@ public class Comment extends BaseTimeEntity {
@Column(name = "comment_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "record_id", nullable = false)
private MissionRecord missionRecord;
@Schema(description = "미션 기록 ID", example = "1")
@Column(name = "record_id", nullable = false)
private Long recordId;

// 작성자
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "writer_id", nullable = false)
@JoinColumn(name = "writer_id")
private Member writer;

@Schema(description = "댓글 내용", example = "너무 이쁘자나~")
Expand All @@ -56,17 +55,17 @@ public class Comment extends BaseTimeEntity {
private List<Comment> replyComments = new ArrayList<>();

@Builder(access = AccessLevel.PRIVATE)
public Comment(MissionRecord missionRecord, Member writer, String content, Comment parent) {
this.missionRecord = missionRecord;
public Comment(Long recordId, Member writer, String content, Comment parent) {
this.recordId = recordId;
this.writer = writer;
this.content = content;
this.parent = parent;
}

public static Comment createComment(
MissionRecord missionRecord, Member writer, String content, @Nullable Comment parent) {
Long recordId, Member writer, String content, @Nullable Comment parent) {
return Comment.builder()
.missionRecord(missionRecord)
.recordId(recordId)
.writer(writer)
.content(content)
.parent(parent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public FeedContentGetResponse findFeedOne(Long recordId) {
return FeedContentGetResponse.from(feedOne);
}

@Transactional(readOnly = true)
public FeedGetResponseV2 findFeedV2(FeedGetRequest request) {
List<FindFeedDto> feeds = getFeeds(request.cursor(), request.memberId(), request.limit());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.stonebed.domain.feed.dao;

import static com.depromeet.stonebed.domain.comment.domain.QComment.*;
import static com.depromeet.stonebed.domain.member.domain.QMember.*;
import static com.depromeet.stonebed.domain.mission.domain.QMission.*;
import static com.depromeet.stonebed.domain.missionHistory.domain.QMissionHistory.*;
Expand Down Expand Up @@ -61,7 +62,7 @@ private JPAQuery<FindFeedDto> getBaseSelectQuery() {
mission,
missionRecord,
member,
Expressions.asNumber(missionRecord.comments.size()).as("totalCommentCount"),
comment.id.count().as("commentCount"),
Expressions.asNumber(missionRecordBoost.count.sumLong().coalesce(0L))
.as("totalBoostCount")));
}
Expand All @@ -75,7 +76,9 @@ private JPAQuery<FindFeedDto> applyJoinsAndConditions(JPAQuery<FindFeedDto> quer
.leftJoin(missionHistory)
.on(missionRecord.missionHistory.eq(missionHistory))
.leftJoin(mission)
.on(missionHistory.mission.eq(mission));
.on(missionHistory.mission.eq(mission))
.leftJoin(comment)
.on(comment.recordId.eq(missionRecord.id)); // Join Comment entity
}

private BooleanExpression ltMissionRecordId(Long missionRecordId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public record FindFeedDto(
Mission mission,
MissionRecord missionRecord,
Member author,
Integer totalCommentCount,
Long totalCommentCount,
Long totalBoostCount) {
public static FindFeedDto from(
Mission mission,
MissionRecord missionRecord,
Member author,
Integer totalCommentCount,
Long totalCommentCount,
Long totalBoostCount) {
return new FindFeedDto(mission, missionRecord, author, totalCommentCount, totalBoostCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public record FeedContentGetResponse(
String missionRecordImageUrl,
@Schema(description = "미션 기록 생성일") LocalDate createdDate,
@Schema(description = "부스트") Long totalBoostCount,
@Schema(description = "댓글 수", example = "12") Integer totalCommentCount,
@Schema(description = "댓글 수", example = "12") Long totalCommentCount,
@Schema(description = "미션 기록 컨텐츠") String content) {
public static FeedContentGetResponse from(FindFeedDto missionRecord) {
return new FeedContentGetResponse(
Expand All @@ -29,7 +29,7 @@ public static FeedContentGetResponse from(FindFeedDto missionRecord) {
missionRecord.author().getProfile().getNickname(),
missionRecord.author().getProfile().getProfileImageUrl(),
missionRecord.missionRecord().getImageUrl(),
missionRecord.missionRecord().getCreatedAt().toLocalDate(),
missionRecord.missionRecord().getUpdatedAt().toLocalDate(),
missionRecord.totalBoostCount(),
missionRecord.totalCommentCount(),
missionRecord.missionRecord().getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public record FeedContentGetResponseV2(
String missionRecordImageUrl,
@Schema(description = "미션 기록 생성일") LocalDate createdDate,
@Schema(description = "부스트") Long totalBoostCount,
@Schema(description = "댓글 수", example = "12") Integer totalCommentCount,
@Schema(description = "댓글 수", example = "12") Long totalCommentCount,
@Schema(description = "미션 기록 컨텐츠") String content) {
public static FeedContentGetResponseV2 from(FindFeedDto missionRecord) {
return new FeedContentGetResponseV2(
Expand All @@ -29,7 +29,7 @@ public static FeedContentGetResponseV2 from(FindFeedDto missionRecord) {
missionRecord.author().getProfile().getNickname(),
missionRecord.author().getProfile().getProfileImageUrl(),
missionRecord.missionRecord().getImageUrl(),
missionRecord.missionRecord().getCreatedAt().toLocalDate(),
missionRecord.missionRecord().getUpdatedAt().toLocalDate(),
missionRecord.totalBoostCount(),
missionRecord.totalCommentCount(),
missionRecord.missionRecord().getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MissionRecord extends BaseTimeEntity {
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@OneToMany(mappedBy = "missionRecord", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

@Schema(description = "미션 이미지 URL", example = "./missionRecord.jpg")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.depromeet.stonebed.domain.auth.domain.OAuthProvider;
import com.depromeet.stonebed.domain.auth.dto.response.AuthTokenResponse;
import com.depromeet.stonebed.domain.auth.dto.response.TokenPairResponse;
import com.depromeet.stonebed.domain.comment.dao.CommentRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmNotificationRepository;
import com.depromeet.stonebed.domain.fcm.dao.FcmTokenRepository;
import com.depromeet.stonebed.domain.member.dao.MemberRepository;
Expand Down Expand Up @@ -40,6 +41,7 @@ class AuthServiceTest extends FixtureMonkeySetUp {
@Mock private MissionRecordRepository missionRecordRepository;
@Mock private MissionRecordBoostRepository missionRecordBoostRepository;
@Mock private FcmTokenRepository fcmTokenRepository;
@Mock private CommentRepository commentRepository;

@Mock private MemberUtil memberUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.transaction.annotation.Isolation.*;

import com.depromeet.stonebed.FixtureMonkeySetUp;
import com.depromeet.stonebed.domain.comment.dao.CommentRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class FeedServiceTest extends FixtureMonkeySetUp {
int DEFAULT_LIMIT = 5;
Long DEFAULT_TOTAL_BOOST_COUNT = 100L;
Integer DEFAULT_TOTAL_COMMENT_COUNT = 13;
Long DEFAULT_TOTAL_COMMENT_COUNT = 13L;
String DEFAULT_CURSOR = "5";
String INVALID_CURSOR = "2024-08-01";

Expand Down

0 comments on commit ec66a63

Please sign in to comment.