From acffb476698887f5031af486fefb4d9181fa38c7 Mon Sep 17 00:00:00 2001 From: joonghyun Date: Tue, 19 Sep 2023 18:18:14 +0900 Subject: [PATCH] =?UTF-8?q?#3=20feat:=20=EC=A5=AC=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=ED=81=AC=EB=A1=A0=EC=9E=A1=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ewhatever/qna/QnaApplication.java | 2 ++ .../qna/post/repository/PostRepository.java | 4 +++ .../qna/post/service/PostService.java | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/main/java/com/ewhatever/qna/QnaApplication.java b/src/main/java/com/ewhatever/qna/QnaApplication.java index 964cb33..e812f9f 100644 --- a/src/main/java/com/ewhatever/qna/QnaApplication.java +++ b/src/main/java/com/ewhatever/qna/QnaApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableJpaAuditing +@EnableScheduling public class QnaApplication { public static void main(String[] args) { diff --git a/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java b/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java index 98b6dd3..d5e1a32 100644 --- a/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java +++ b/src/main/java/com/ewhatever/qna/post/repository/PostRepository.java @@ -9,6 +9,8 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface PostRepository extends JpaRepository { Page findAllByIsJuicyTrue(Pageable pageable); @@ -24,4 +26,6 @@ public interface PostRepository extends JpaRepository { @Query("SELECT CASE WHEN COUNT(p) > 0 THEN true ELSE false END FROM Post p WHERE (p.title LIKE CONCAT('%', :searchWord, '%') OR p.content LIKE CONCAT('%', :searchWord, '%')) AND p.isJuicy = true") boolean existsJuicyPosts(@Param("searchWord") String searchWord); + + List findAllByIsJuicyFalse(); } diff --git a/src/main/java/com/ewhatever/qna/post/service/PostService.java b/src/main/java/com/ewhatever/qna/post/service/PostService.java index 14a8cb5..84bac9d 100644 --- a/src/main/java/com/ewhatever/qna/post/service/PostService.java +++ b/src/main/java/com/ewhatever/qna/post/service/PostService.java @@ -17,9 +17,12 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -223,4 +226,27 @@ public void scrapPost(Long postIdx, Long userIdx) throws BaseException { throw new BaseException(DATABASE_ERROR); } } + + /** + * 매 시간 쥬시글 여부 확인해서 변환해주는 크론잡 + */ + @Scheduled(cron = "0 0 0/1 * * *") + @Transactional(rollbackFor = Exception.class) + public void checkJuicy() { + List postList = postRepository.findAllByIsJuicyFalse(); + for (Post post : postList) { + LocalDateTime currentDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.HOURS); +// System.out.println("현재 시간: " + currentDateTime); + LocalDateTime questionDateTime = post.getCreatedDate().truncatedTo(ChronoUnit.HOURS); +// System.out.println("질문 등록 시간: " + questionDateTime); +// int compareResult = currentDateTime.compareTo(questionDateTime); +// System.out.println("비교 결과: " + compareResult); +// Period period = Period.between(questionDate, currentDate); + long timeGap = ChronoUnit.HOURS.between(questionDateTime, currentDateTime); + if (timeGap == 72) { + post.setIsJuicy(true); + postRepository.save(post); + } + } + } } \ No newline at end of file