diff --git a/src/main/java/side/onetime/global/config/SchedulerConfig.java b/src/main/java/side/onetime/global/config/SchedulerConfig.java new file mode 100644 index 0000000..2f18ecd --- /dev/null +++ b/src/main/java/side/onetime/global/config/SchedulerConfig.java @@ -0,0 +1,9 @@ +package side.onetime.global.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +public class SchedulerConfig { +} \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/EventRepository.java b/src/main/java/side/onetime/repository/EventRepository.java index f85dafa..627c915 100644 --- a/src/main/java/side/onetime/repository/EventRepository.java +++ b/src/main/java/side/onetime/repository/EventRepository.java @@ -4,10 +4,13 @@ import side.onetime.domain.Event; import side.onetime.repository.custom.EventRepositoryCustom; +import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import java.util.UUID; public interface EventRepository extends JpaRepository, EventRepositoryCustom { Optional findByEventId(UUID eventId); boolean existsByEventId(UUID eventId); + List findByCreatedDateBefore(LocalDateTime twoWeeksAgo); } \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/custom/EventRepositoryCustom.java b/src/main/java/side/onetime/repository/custom/EventRepositoryCustom.java index fac70a2..b3f2032 100644 --- a/src/main/java/side/onetime/repository/custom/EventRepositoryCustom.java +++ b/src/main/java/side/onetime/repository/custom/EventRepositoryCustom.java @@ -3,5 +3,5 @@ import side.onetime.domain.Event; public interface EventRepositoryCustom { - void deleteUserEvent(Event event); + void deleteEvent(Event event); } \ No newline at end of file diff --git a/src/main/java/side/onetime/repository/custom/EventRepositoryImpl.java b/src/main/java/side/onetime/repository/custom/EventRepositoryImpl.java index e5b0803..521d29d 100644 --- a/src/main/java/side/onetime/repository/custom/EventRepositoryImpl.java +++ b/src/main/java/side/onetime/repository/custom/EventRepositoryImpl.java @@ -16,7 +16,7 @@ public class EventRepositoryImpl implements EventRepositoryCustom { private final JPAQueryFactory queryFactory; @Override - public void deleteUserEvent(Event e) { + public void deleteEvent(Event e) { queryFactory.delete(selection) .where(selection.schedule.event.eq(e)) .execute(); diff --git a/src/main/java/side/onetime/service/EventCleanupScheduler.java b/src/main/java/side/onetime/service/EventCleanupScheduler.java new file mode 100644 index 0000000..5dd8d27 --- /dev/null +++ b/src/main/java/side/onetime/service/EventCleanupScheduler.java @@ -0,0 +1,28 @@ +package side.onetime.service; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import side.onetime.domain.Event; +import side.onetime.repository.EventRepository; + +import java.time.LocalDateTime; +import java.util.List; + +@Component +@RequiredArgsConstructor +public class EventCleanupScheduler { + + private final EventRepository eventRepository; + + @Scheduled(cron = "${scheduling.cron}") + @Transactional + public void deleteOldEvents() { + LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30); + + // 30일 이상 지난 이벤트를 찾은 후 삭제 + List oldEvents = eventRepository.findByCreatedDateBefore(thirtyDaysAgo); + oldEvents.forEach(eventRepository::deleteEvent); + } +} \ No newline at end of file diff --git a/src/main/java/side/onetime/service/EventService.java b/src/main/java/side/onetime/service/EventService.java index 2ebb2ba..fb442f8 100644 --- a/src/main/java/side/onetime/service/EventService.java +++ b/src/main/java/side/onetime/service/EventService.java @@ -294,6 +294,6 @@ public void removeUserCreatedEvent(String authorizationHeader, String eventId) { throw new EventParticipationException(EventParticipationErrorResult._IS_NOT_USERS_CREATED_EVENT_PARTICIPATION); } - eventRepository.deleteUserEvent(event); + eventRepository.deleteEvent(event); } } \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 11c2ce1..676a962 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -86,4 +86,7 @@ jwt: refresh-token: expiration-time: ${REFRESH_TOKEN_EXPIRATION_TIME} register-token: - expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME} \ No newline at end of file + expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME} + +scheduling: + cron: ${CRON} \ No newline at end of file