From 6f97071ead3c884b948a149c660043215f9ea2dd Mon Sep 17 00:00:00 2001 From: YIHYUN HA Date: Thu, 29 Aug 2024 23:32:23 +0900 Subject: [PATCH] Add friend soft delete, Reflect soft delete when retrieve data --- .../user/application/UserApplication.java | 9 +++++++-- .../persistence/entity/FriendEntity.java | 4 +++- .../CustomCommentRepositoryImpl.java | 2 +- .../repository/CustomFriendRepository.java | 4 ++++ .../CustomFriendRepositoryImpl.java | 18 ++++++++++++++++++ .../CustomReactionRepositoryImpl.java | 2 +- .../CustomWalkLogRepositoryImpl.java | 19 ++++++++++++------- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/com/nexters/gaetteok/user/application/UserApplication.java b/api/src/main/java/com/nexters/gaetteok/user/application/UserApplication.java index 31d4f17..4ebd137 100644 --- a/api/src/main/java/com/nexters/gaetteok/user/application/UserApplication.java +++ b/api/src/main/java/com/nexters/gaetteok/user/application/UserApplication.java @@ -29,6 +29,7 @@ public class UserApplication { private final UserRepository userRepository; private final UserPushNotificationRepository userPushNotificationRepository; + private final FriendRepository friendRepository; private final WalkLogRepository walkLogRepository; private final CommentRepository commentRepository; private final ReactionRepository reactionRepository; @@ -107,11 +108,13 @@ public void deleteUser(UserInfo userInfo) { long userId = userInfo.getUserId(); UserEntity userEntity = userRepository.getById(userId); userEntity.delete(); + long deletedFriendCount = friendRepository.deleteByUserId(userId); long deletedWalkLogCount = walkLogRepository.deleteByUserId(userId); long deletedCommentCount = commentRepository.deleteByUserId(userId); long deletedReactionCount = reactionRepository.deleteByUserId(userId); - log.info("사용자 {} 삭제 - 삭제된 산책 기록 수: {}, 댓글 수: {}, 리액션 수: {}", + log.info("사용자 {} 삭제 - 삭제된 친구 관계 수: {}, 산책 기록 수: {}, 댓글 수: {}, 리액션 수: {}", userInfo, + deletedFriendCount, deletedWalkLogCount, deletedCommentCount, deletedReactionCount @@ -122,11 +125,13 @@ public void deleteUser(UserInfo userInfo) { public void restoreUser(long userId) { UserEntity userEntity = userRepository.getById(userId); userEntity.restore(); + long restoreFriendCount = friendRepository.restoreByUserId(userId); long restoredWalkLogCount = walkLogRepository.restoreByUserId(userId); long restoredCommentCount = commentRepository.restoreByUserId(userId); long restoredReactionCount = reactionRepository.restoreByUserId(userId); - log.info("사용자 {} 복구 - 복구된 산책 기록 수: {}, 댓글 수: {}, 리액션 수: {}", + log.info("사용자 {} 복구 - 복구된 친구 관계 수: {}, 산책 기록 수: {}, 댓글 수: {}, 리액션 수: {}", userId, + restoreFriendCount, restoredWalkLogCount, restoredCommentCount, restoredReactionCount diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/entity/FriendEntity.java b/infra/src/main/java/com/nexters/gaetteok/persistence/entity/FriendEntity.java index 9a0587e..b9d3169 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/entity/FriendEntity.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/entity/FriendEntity.java @@ -39,12 +39,14 @@ public class FriendEntity { @Column(updatable = false) private LocalDateTime createdAt; + private boolean deleted; @Builder - public FriendEntity(long id, long myUserId, long friendUserId, LocalDateTime createdAt) { + public FriendEntity(long id, long myUserId, long friendUserId, LocalDateTime createdAt, boolean deleted) { this.id = id; this.myUserId = myUserId; this.friendUserId = friendUserId; this.createdAt = createdAt; + this.deleted = deleted; } } diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomCommentRepositoryImpl.java b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomCommentRepositoryImpl.java index 5e3f79c..cc6004b 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomCommentRepositoryImpl.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomCommentRepositoryImpl.java @@ -30,7 +30,7 @@ public List findByWalkLogIdInWithUser(long walkLogId) { )) .from(commentEntity) .join(userEntity).on(commentEntity.userId.eq(userEntity.id)) - .where(commentEntity.walkLogId.eq(walkLogId)) + .where(commentEntity.walkLogId.eq(walkLogId), commentEntity.deleted.isFalse()) .orderBy(commentEntity.id.asc()) .fetch(); } diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepository.java b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepository.java index 38deba6..45c999a 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepository.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepository.java @@ -9,4 +9,8 @@ public interface CustomFriendRepository { List getFriendWalkStatus(long userId, LocalDate date); + long deleteByUserId(long userId); + + long restoreByUserId(long userId); + } diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepositoryImpl.java b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepositoryImpl.java index 5686265..df84b79 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepositoryImpl.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomFriendRepositoryImpl.java @@ -47,4 +47,22 @@ public List getFriendWalkStatus(long userId, LocalDate date) { .fetch(); } + @Override + public long deleteByUserId(long userId) { + return jpaQueryFactory + .update(friendEntity) + .set(friendEntity.deleted, true) + .where(friendEntity.myUserId.eq(userId), friendEntity.friendUserId.eq(userId)) + .execute(); + } + + @Override + public long restoreByUserId(long userId) { + return jpaQueryFactory + .update(friendEntity) + .set(friendEntity.deleted, false) + .where(friendEntity.myUserId.eq(userId), friendEntity.friendUserId.eq(userId)) + .execute(); + } + } diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomReactionRepositoryImpl.java b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomReactionRepositoryImpl.java index 4240132..1d51167 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomReactionRepositoryImpl.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomReactionRepositoryImpl.java @@ -30,7 +30,7 @@ public List findByWalkLogIdInWithUser(long walkLogId) { )) .from(reactionEntity) .join(userEntity).on(reactionEntity.userId.eq(userEntity.id)) - .where(reactionEntity.walkLogId.eq(walkLogId)) + .where(reactionEntity.walkLogId.eq(walkLogId), reactionEntity.deleted.isFalse()) .orderBy(reactionEntity.id.asc()) .fetch(); } diff --git a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomWalkLogRepositoryImpl.java b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomWalkLogRepositoryImpl.java index e91ef03..231e02d 100644 --- a/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomWalkLogRepositoryImpl.java +++ b/infra/src/main/java/com/nexters/gaetteok/persistence/repository/CustomWalkLogRepositoryImpl.java @@ -29,7 +29,8 @@ public List getCalendar(long userId, int year, int month) { .where( walkLogEntity.userId.eq(userId), walkLogEntity.createdAt.after(LocalDateTime.of(year, month, 1, 0, 0)), - walkLogEntity.createdAt.before(LocalDateTime.of(year, month + 1, 1, 0, 0)) + walkLogEntity.createdAt.before(LocalDateTime.of(year, month + 1, 1, 0, 0)), + walkLogEntity.deleted.isFalse() ) .fetch(); } @@ -39,7 +40,7 @@ public List getListOfMeAndMyFriend(long userId, long cursorId, int page List friendIdList = jpaQueryFactory .select(friendEntity.friendUserId) .from(friendEntity) - .where(friendEntity.myUserId.eq(userId)) + .where(friendEntity.myUserId.eq(userId), friendEntity.deleted.isFalse()) .fetch(); // 피드에는 나도 포함 friendIdList.add(userId); @@ -61,7 +62,8 @@ public List getListOfMeAndMyFriend(long userId, long cursorId, int page .on(userEntity.id.eq(walkLogEntity.userId)) .where( cursorId > 0 ? walkLogEntity.id.lt(cursorId) : null, - userEntity.id.in(friendIdList) + userEntity.id.in(friendIdList), + walkLogEntity.deleted.isFalse() ) .limit(pageSize) .orderBy(walkLogEntity.id.desc()) @@ -87,7 +89,8 @@ public List getListOnlyMe(long userId, long cursorId, int pageSize) { .on(userEntity.id.eq(walkLogEntity.userId)) .where( cursorId > 0 ? walkLogEntity.id.lt(cursorId) : null, - userEntity.id.eq(userId) + userEntity.id.eq(userId), + walkLogEntity.deleted.isFalse() ) .limit(pageSize) .orderBy(walkLogEntity.id.desc()) @@ -100,7 +103,8 @@ public List getListByUserIdAndMonth(long userId, int year, int mo .where( walkLogEntity.userId.eq(userId), walkLogEntity.createdAt.year().eq(year), - walkLogEntity.createdAt.month().eq(month) + walkLogEntity.createdAt.month().eq(month), + walkLogEntity.deleted.isFalse() ) .orderBy(walkLogEntity.id.desc()) .fetch(); @@ -109,7 +113,7 @@ public List getListByUserIdAndMonth(long userId, int year, int mo @Override public WalkLogEntity getMaxIdLessThan(long walkLogId, long userId) { return jpaQueryFactory.selectFrom(walkLogEntity) - .where(walkLogEntity.id.lt(walkLogId), walkLogEntity.userId.eq(userId)) + .where(walkLogEntity.id.lt(walkLogId), walkLogEntity.userId.eq(userId), walkLogEntity.deleted.isFalse()) .orderBy(walkLogEntity.id.desc()) .fetchFirst(); } @@ -121,7 +125,8 @@ public boolean isTodayWalkLogExists(long userId, LocalDate date) { .from(walkLogEntity) .where( walkLogEntity.userId.eq(userId), - dateTimeOperation(LocalDate.class, Ops.DateTimeOps.DATE, walkLogEntity.createdAt).eq(date) + dateTimeOperation(LocalDate.class, Ops.DateTimeOps.DATE, walkLogEntity.createdAt).eq(date), + walkLogEntity.deleted.isFalse() ) .fetchFirst();