Skip to content

Commit

Permalink
#3 feat: 쥬시글 변환 크론잡 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Sep 19, 2023
1 parent 08c93fa commit acffb47
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/ewhatever/qna/QnaApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {
Page<Post> findAllByIsJuicyTrue(Pageable pageable);
Expand All @@ -24,4 +26,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {

@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<Post> findAllByIsJuicyFalse();
}
26 changes: 26 additions & 0 deletions src/main/java/com/ewhatever/qna/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Post> 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);
}
}
}
}

0 comments on commit acffb47

Please sign in to comment.