Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 목표 기간이 지나면 씨앗 잠기도록 스케줄링 구현 #88

Merged
merged 6 commits into from
Jan 23, 2024
1 change: 1 addition & 0 deletions growthookServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.testng:testng:7.1.0'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class ActionPlanGetResponseDto {
private Boolean isFinished;

private Boolean hasReview;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class DoingActionPlanGetResponseDto {
private Long seedId;

private Boolean hasReview;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public class FinishedActionPlanGetResponseDto {
private Long seedId;

private Boolean hasReview;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface SeedRepository extends JpaRepository<Seed, Long> {
Optional<Seed> findSeedById(Long seedId);
Expand All @@ -18,6 +20,9 @@ default Seed findSeedByIdOrThrow(Long seedId) {
return findSeedById(seedId)
.orElseThrow(()-> new NotFoundException(ErrorStatus.NOT_FOUND_SEED.getMessage()));
}

List<Seed> findByCave_MemberIdAndLockDateBetween(Long memberId, LocalDate now, LocalDate threeDaysLater);

@Modifying(clearAutomatically=true)
@Query("UPDATE Seed s SET s.isLocked = true WHERE s.lockDate < CURRENT_DATE AND s.isLocked = false")
int updateLockStatusForExpiredSeeds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
import com.example.growthookserver.api.seed.dto.response.SeedMoveResponseDto;
import com.example.growthookserver.api.seed.repository.SeedRepository;
import com.example.growthookserver.api.seed.service.SeedService;
import java.time.Clock;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;
import java.util.List;


@Service
@RequiredArgsConstructor
Expand All @@ -38,6 +37,7 @@ public class SeedServiceImpl implements SeedService {
private final SeedRepository seedRepository;
private final ActionPlanRepository actionPlanRepository;
private final MemberRepository memberRepository;
private final Clock clock;

@Override
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.growthookserver.common.config;

import java.time.Clock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TimeConfig {

@Bean
public Clock clock() {
return Clock.systemDefaultZone();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.growthookserver.common.util;

import com.example.growthookserver.api.seed.repository.SeedRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@EnableScheduling
@RequiredArgsConstructor
public class SeedScheduler {

private final SeedRepository seedRepository;
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
@Transactional
public void updateLockStatusForExpiredSeeds() {
int updatedCount = seedRepository.updateLockStatusForExpiredSeeds();
}
}