Skip to content

Commit

Permalink
Feature/#163 (#164)
Browse files Browse the repository at this point in the history
* feat: spring schefuler config 등록

* feat: 스케줄러를 이용해 apply엔티티의  soft-delete 적용

* feat: entity delete scheduler 통합

* feat: deletable entity에 delete query 추가 (통합예정) )
  • Loading branch information
지훈 authored Jan 24, 2022
1 parent c67c368 commit c6d2a04
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,4 +35,8 @@ public interface ApplyRepository extends JpaRepository<Apply, Long> {
List<Apply> findAllByPostId(long postId);

List<Apply> findAllByMemberId(long memberId);

@Modifying
@Query(value = "DELETE FROM Apply a WHERE a.isDeleted = true and a.lastModifiedDate < :baseDeletionTime")
void deleteALlExpired(LocalDateTime baseDeletionTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/yapp/project/config/SchedulerConfig.java
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,4 +32,8 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
Page<Member> findAllByBasePositionCode(Pageable pageable, int basePositionCode);

Page<Member> findAllByPositionCodeContains(String position, Pageable pageable);

@Modifying
@Query(value = "DELETE FROM Member m WHERE m.isDeleted = true and m.lastModifiedDate < :baseDeletionTime")
void deleteAllExpired(LocalDateTime baseDeletionTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,4 +157,9 @@ public void updateFcmToken(long currentMemberId, String fcmToken) {

currentMember.updateFcmTokenAndActiveStatus(fcmToken, null);
}

@Transactional
public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) {
memberRepository.deleteAllExpired(baseDeletionTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,4 +27,8 @@ public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findByTitleIgnoreCaseContains(Pageable pageable, String title);

List<Post> findAllByOwnerId(long ownerId);

@Modifying
@Query(value = "DELETE FROM Post p WHERE p.isDeleted = true and p.lastModifiedDate < :baseDeletionTime")
void deleteAllExpired(LocalDateTime baseDeletionTime);
}
6 changes: 6 additions & 0 deletions src/main/java/com/yapp/project/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
Expand Down Expand Up @@ -231,4 +232,9 @@ public Optional<Map<String, String>> sendInvitationNotification(long senderId, l

return Optional.of(Map.of("title", title, "body", body));
}

@Transactional
public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) {
postRepository.deleteAllExpired(baseDeletionTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeReviewHistory, Long> {
Expand All @@ -25,4 +27,8 @@ public interface CodeReviewHistoryRepository extends JpaRepository<CodeReviewHis
void deleteAllByPost(Post post);

List<CodeReviewHistory> findAllByTargetMemberIdAndPostId(long targetMemberId, long postId);

@Modifying
@Query(value = "DELETE FROM CodeReviewHistory cr WHERE cr.isDeleted = true and cr.lastModifiedDate < :baseDeletionTime")
void deleteAllExpired(LocalDateTime baseDeletionTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextReviewHistory, Long> {
Expand All @@ -22,4 +25,8 @@ public interface TextReviewHistoryRepository extends JpaRepository<TextReviewHis
List<TextReviewHistory> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
Expand Down Expand Up @@ -99,5 +100,10 @@ public CodeReviewCountResponse findAllByMemberAndPost(long currentMemberId, long

return converter.toCodeReviewCountResponse(codeReviews);
}

@Transactional
public void deleteAllExpiredDate(LocalDateTime baseDeletionTime) {
codeReviewHistoryRepository.deleteAllExpired(baseDeletionTime);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -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);
}
}

0 comments on commit c6d2a04

Please sign in to comment.