Skip to content

Commit

Permalink
Add friend soft delete, Reflect soft delete when retrieve data
Browse files Browse the repository at this point in the history
  • Loading branch information
hyh1016 committed Aug 29, 2024
1 parent 403717e commit 6f97071
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<Comment> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public interface CustomFriendRepository {

List<FriendWalkStatus> getFriendWalkStatus(long userId, LocalDate date);

long deleteByUserId(long userId);

long restoreByUserId(long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public List<FriendWalkStatus> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<Reaction> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public List<WalkLogEntity> 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();
}
Expand All @@ -39,7 +40,7 @@ public List<WalkLog> getListOfMeAndMyFriend(long userId, long cursorId, int page
List<Long> friendIdList = jpaQueryFactory
.select(friendEntity.friendUserId)
.from(friendEntity)
.where(friendEntity.myUserId.eq(userId))
.where(friendEntity.myUserId.eq(userId), friendEntity.deleted.isFalse())
.fetch();
// ํ”ผ๋“œ์—๋Š” ๋‚˜๋„ ํฌํ•จ
friendIdList.add(userId);
Expand All @@ -61,7 +62,8 @@ public List<WalkLog> 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())
Expand All @@ -87,7 +89,8 @@ public List<WalkLog> 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())
Expand All @@ -100,7 +103,8 @@ public List<WalkLogEntity> 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();
Expand All @@ -109,7 +113,7 @@ public List<WalkLogEntity> 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();
}
Expand All @@ -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();

Expand Down

0 comments on commit 6f97071

Please sign in to comment.