From c6d2a04013a890852bce59263401dbd1175404cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A7=80=ED=9B=88?= Date: Mon, 24 Jan 2022 15:18:10 +0900 Subject: [PATCH] Feature/#163 (#164) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: spring schefuler config 등록 * feat: 스케줄러를 이용해 apply엔티티의 soft-delete 적용 * feat: entity delete scheduler 통합 * feat: deletable entity에 delete query 추가 (통합예정) ) --- .../apply/repository/ApplyRepository.java | 6 +++ .../project/apply/service/ApplyService.java | 6 +++ .../Scheduler/EntityDeletionScheduler.java | 47 +++++++++++++++++++ .../yapp/project/config/SchedulerConfig.java | 9 ++++ .../member/repository/MemberRepository.java | 5 ++ .../project/member/service/MemberService.java | 6 +++ .../post/repository/PostRepository.java | 7 +++ .../project/post/service/PostService.java | 6 +++ .../CodeReviewHistoryRepository.java | 6 +++ .../TextReviewHistoryRepository.java | 7 +++ .../service/CodeReviewHistoryService.java | 6 +++ .../service/TextReviewHistoryService.java | 6 +++ 12 files changed, 117 insertions(+) create mode 100644 src/main/java/com/yapp/project/common/Scheduler/EntityDeletionScheduler.java create mode 100644 src/main/java/com/yapp/project/config/SchedulerConfig.java diff --git a/src/main/java/com/yapp/project/apply/repository/ApplyRepository.java b/src/main/java/com/yapp/project/apply/repository/ApplyRepository.java index bc121837..c0aca9f7 100644 --- a/src/main/java/com/yapp/project/apply/repository/ApplyRepository.java +++ b/src/main/java/com/yapp/project/apply/repository/ApplyRepository.java @@ -5,8 +5,10 @@ import com.yapp.project.post.entity.Post; import com.yapp.project.post.entity.RecruitingPosition; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -33,4 +35,8 @@ public interface ApplyRepository extends JpaRepository { List findAllByPostId(long postId); List findAllByMemberId(long memberId); + + @Modifying + @Query(value = "DELETE FROM Apply a WHERE a.isDeleted = true and a.lastModifiedDate < :baseDeletionTime") + void deleteALlExpired(LocalDateTime baseDeletionTime); } diff --git a/src/main/java/com/yapp/project/apply/service/ApplyService.java b/src/main/java/com/yapp/project/apply/service/ApplyService.java index 6f718e95..81b9d8a2 100644 --- a/src/main/java/com/yapp/project/apply/service/ApplyService.java +++ b/src/main/java/com/yapp/project/apply/service/ApplyService.java @@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional; import java.text.MessageFormat; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -135,4 +136,9 @@ private void sendNotificationToReceiver(Member receiver, String title, String bo notificationService.save(receiver.getId(), title, body, NotificationType.APPLY_FOR_PROJECT.getCode(), relatedPost.getId()); } + + @Transactional + public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) { + applyRepository.deleteALlExpired(baseDeletionTime); + } } diff --git a/src/main/java/com/yapp/project/common/Scheduler/EntityDeletionScheduler.java b/src/main/java/com/yapp/project/common/Scheduler/EntityDeletionScheduler.java new file mode 100644 index 00000000..8b7e1ad6 --- /dev/null +++ b/src/main/java/com/yapp/project/common/Scheduler/EntityDeletionScheduler.java @@ -0,0 +1,47 @@ +package com.yapp.project.common.Scheduler; + +import com.yapp.project.apply.service.ApplyService; +import com.yapp.project.member.service.MemberService; +import com.yapp.project.post.service.PostService; +import com.yapp.project.review.service.CodeReviewHistoryService; +import com.yapp.project.review.service.TextReviewHistoryService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Slf4j +@RequiredArgsConstructor +@Component +public class EntityDeletionScheduler { + private final ApplyService applyService; + private final MemberService memberService; + private final PostService postService; + private final CodeReviewHistoryService codeReviewHistoryService; + private final TextReviewHistoryService textReviewHistoryService; + + /** + * cron : 초 분 시 일 월 년 + * 매일 자정(00:00:00)시에 스케줄러 실행 + */ + @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") + public void deletePermanently() { + LocalDate today = LocalDate.now(); + LocalDateTime baseDeletionTime = today.minusMonths(1).atStartOfDay(); + + applyService.deleteAllExpiredDate(baseDeletionTime); + + memberService.deleteAllExpiredDate(baseDeletionTime); + + postService.deleteAllExpiredDate(baseDeletionTime); + + codeReviewHistoryService.deleteAllExpiredDate(baseDeletionTime); + + textReviewHistoryService.deleteAllExpiredDate(baseDeletionTime); + + log.info("entity deletion completed in {} / base deletion date: {}", today, baseDeletionTime); + } +} diff --git a/src/main/java/com/yapp/project/config/SchedulerConfig.java b/src/main/java/com/yapp/project/config/SchedulerConfig.java new file mode 100644 index 00000000..9cb4cfbc --- /dev/null +++ b/src/main/java/com/yapp/project/config/SchedulerConfig.java @@ -0,0 +1,9 @@ +package com.yapp.project.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +public class SchedulerConfig { +} diff --git a/src/main/java/com/yapp/project/member/repository/MemberRepository.java b/src/main/java/com/yapp/project/member/repository/MemberRepository.java index 09c8e43c..7491c2b7 100644 --- a/src/main/java/com/yapp/project/member/repository/MemberRepository.java +++ b/src/main/java/com/yapp/project/member/repository/MemberRepository.java @@ -9,6 +9,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -31,4 +32,8 @@ public interface MemberRepository extends JpaRepository { Page findAllByBasePositionCode(Pageable pageable, int basePositionCode); Page findAllByPositionCodeContains(String position, Pageable pageable); + + @Modifying + @Query(value = "DELETE FROM Member m WHERE m.isDeleted = true and m.lastModifiedDate < :baseDeletionTime") + void deleteAllExpired(LocalDateTime baseDeletionTime); } diff --git a/src/main/java/com/yapp/project/member/service/MemberService.java b/src/main/java/com/yapp/project/member/service/MemberService.java index 8a2383a3..eb12971a 100644 --- a/src/main/java/com/yapp/project/member/service/MemberService.java +++ b/src/main/java/com/yapp/project/member/service/MemberService.java @@ -23,6 +23,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; @@ -156,4 +157,9 @@ public void updateFcmToken(long currentMemberId, String fcmToken) { currentMember.updateFcmTokenAndActiveStatus(fcmToken, null); } + + @Transactional + public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) { + memberRepository.deleteAllExpired(baseDeletionTime); + } } diff --git a/src/main/java/com/yapp/project/post/repository/PostRepository.java b/src/main/java/com/yapp/project/post/repository/PostRepository.java index 391657ea..e2579abd 100644 --- a/src/main/java/com/yapp/project/post/repository/PostRepository.java +++ b/src/main/java/com/yapp/project/post/repository/PostRepository.java @@ -4,7 +4,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -24,4 +27,8 @@ public interface PostRepository extends JpaRepository { Page findByTitleIgnoreCaseContains(Pageable pageable, String title); List findAllByOwnerId(long ownerId); + + @Modifying + @Query(value = "DELETE FROM Post p WHERE p.isDeleted = true and p.lastModifiedDate < :baseDeletionTime") + void deleteAllExpired(LocalDateTime baseDeletionTime); } diff --git a/src/main/java/com/yapp/project/post/service/PostService.java b/src/main/java/com/yapp/project/post/service/PostService.java index 3cfe68b2..0942e487 100644 --- a/src/main/java/com/yapp/project/post/service/PostService.java +++ b/src/main/java/com/yapp/project/post/service/PostService.java @@ -31,6 +31,7 @@ import org.springframework.transaction.annotation.Transactional; import java.text.MessageFormat; +import java.time.LocalDateTime; import java.util.*; @Slf4j @@ -231,4 +232,9 @@ public Optional> sendInvitationNotification(long senderId, l return Optional.of(Map.of("title", title, "body", body)); } + + @Transactional + public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) { + postRepository.deleteAllExpired(baseDeletionTime); + } } diff --git a/src/main/java/com/yapp/project/review/repository/CodeReviewHistoryRepository.java b/src/main/java/com/yapp/project/review/repository/CodeReviewHistoryRepository.java index 1813017c..d75671e6 100644 --- a/src/main/java/com/yapp/project/review/repository/CodeReviewHistoryRepository.java +++ b/src/main/java/com/yapp/project/review/repository/CodeReviewHistoryRepository.java @@ -5,9 +5,11 @@ import com.yapp.project.review.dto.response.CodeReviewResponse; import com.yapp.project.review.entity.CodeReviewHistory; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.time.LocalDateTime; import java.util.List; public interface CodeReviewHistoryRepository extends JpaRepository { @@ -25,4 +27,8 @@ public interface CodeReviewHistoryRepository extends JpaRepository findAllByTargetMemberIdAndPostId(long targetMemberId, long postId); + + @Modifying + @Query(value = "DELETE FROM CodeReviewHistory cr WHERE cr.isDeleted = true and cr.lastModifiedDate < :baseDeletionTime") + void deleteAllExpired(LocalDateTime baseDeletionTime); } diff --git a/src/main/java/com/yapp/project/review/repository/TextReviewHistoryRepository.java b/src/main/java/com/yapp/project/review/repository/TextReviewHistoryRepository.java index 3d94d0b7..5ce4051b 100644 --- a/src/main/java/com/yapp/project/review/repository/TextReviewHistoryRepository.java +++ b/src/main/java/com/yapp/project/review/repository/TextReviewHistoryRepository.java @@ -6,7 +6,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import java.time.LocalDateTime; import java.util.List; public interface TextReviewHistoryRepository extends JpaRepository { @@ -22,4 +25,8 @@ public interface TextReviewHistoryRepository extends JpaRepository findAllByTargetMemberIdAndPostId(long targetMemberId, long postId); boolean existsByReviewerIdAndTargetMemberIdAndPostId(long reviewerId, long targetMemberId, long postId); + + @Modifying + @Query(value = "DELETE FROM TextReviewHistory tr WHERE tr.isDeleted = true and tr.lastModifiedDate < :baseDeletionTime") + void deleteAllExpired(LocalDateTime baseDeletionTime); } diff --git a/src/main/java/com/yapp/project/review/service/CodeReviewHistoryService.java b/src/main/java/com/yapp/project/review/service/CodeReviewHistoryService.java index ba5808d7..ab52f1a5 100644 --- a/src/main/java/com/yapp/project/review/service/CodeReviewHistoryService.java +++ b/src/main/java/com/yapp/project/review/service/CodeReviewHistoryService.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.List; @Service @@ -99,5 +100,10 @@ public CodeReviewCountResponse findAllByMemberAndPost(long currentMemberId, long return converter.toCodeReviewCountResponse(codeReviews); } + + @Transactional + public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) { + codeReviewHistoryRepository.deleteAllExpired(baseDeletionTime); + } } diff --git a/src/main/java/com/yapp/project/review/service/TextReviewHistoryService.java b/src/main/java/com/yapp/project/review/service/TextReviewHistoryService.java index a380b9f0..cc72f828 100644 --- a/src/main/java/com/yapp/project/review/service/TextReviewHistoryService.java +++ b/src/main/java/com/yapp/project/review/service/TextReviewHistoryService.java @@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; import java.text.MessageFormat; +import java.time.LocalDateTime; import java.util.List; @Slf4j @@ -117,4 +118,9 @@ private void sendNotificationToReviewee(Member reviewee, Post post) { notificationService.save(reviewee.getId(), title, body, NotificationType.REVIEW_REGISTRATION.getCode(), post.getId()); } + + @Transactional + public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) { + textReviewHistoryRepository.deleteAllExpired(baseDeletionTime); + } }