From 08c93fad42f6b63862daefb76b7766ad79016258 Mon Sep 17 00:00:00 2001 From: joonghyun Date: Tue, 19 Sep 2023 17:20:41 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#6=20fix:=20count=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ewhatever/qna/answer/repository/AnswerRepository.java | 2 +- .../java/com/ewhatever/qna/answer/service/AnswerService.java | 4 ++-- src/main/java/com/ewhatever/qna/post/service/PostService.java | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/ewhatever/qna/answer/repository/AnswerRepository.java b/src/main/java/com/ewhatever/qna/answer/repository/AnswerRepository.java index 89d956e..373f9be 100644 --- a/src/main/java/com/ewhatever/qna/answer/repository/AnswerRepository.java +++ b/src/main/java/com/ewhatever/qna/answer/repository/AnswerRepository.java @@ -15,5 +15,5 @@ public interface AnswerRepository extends JpaRepository { Long countByAnswerer_UserIdxAndStatusEquals(Long userIdx, String status); Page findByAnswerer_UserIdxAndPost_IsJuicyAndStatusEquals(Long userIdx, Boolean isJuicy, String status, Pageable pageable); Long countByPost_PostIdxAndStatusEquals(Long postIdx, String status); - int countByPostAndIsJuicyFalse(Post post); + Long countByPost(Post post); } \ No newline at end of file diff --git a/src/main/java/com/ewhatever/qna/answer/service/AnswerService.java b/src/main/java/com/ewhatever/qna/answer/service/AnswerService.java index 90910c3..8519311 100644 --- a/src/main/java/com/ewhatever/qna/answer/service/AnswerService.java +++ b/src/main/java/com/ewhatever/qna/answer/service/AnswerService.java @@ -26,7 +26,7 @@ public void addAnswer(Long userIdx, PostAnswerReq postAnswerReq) throws BaseExce try { User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER)); Post post = postRepository.findById(postAnswerReq.getPostIdx()).orElseThrow(() -> new BaseException(INVALID_POST_IDX)); - int currentAnswerCount = answerRepository.countByPostAndIsJuicyFalse(post); + Long currentAnswerCount = answerRepository.countByPost(post); if (user.getRole().equals(SINY)) { if (currentAnswerCount < 3) { // 크론잡 주기 사이에 답변이 등록되어 isJuicy 컬럼값에 아직 반영이 안된 경우를 위해 예외처리 Answer answer = Answer.builder() @@ -36,7 +36,7 @@ public void addAnswer(Long userIdx, PostAnswerReq postAnswerReq) throws BaseExce .build(); answerRepository.save(answer); - currentAnswerCount = answerRepository.countByPostAndIsJuicyFalse(post); //currentAnswerCount 최신화 + currentAnswerCount = answerRepository.countByPost(post); //currentAnswerCount 최신화 // 3번째 답변이면 쥬시글로 전환 if (currentAnswerCount == 3) { post.setIsJuicy(true); 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 f418cfb..14a8cb5 100644 --- a/src/main/java/com/ewhatever/qna/post/service/PostService.java +++ b/src/main/java/com/ewhatever/qna/post/service/PostService.java @@ -142,7 +142,6 @@ public GetPostRes getPost(Long postIdx, Long userIdx) throws BaseException { User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE).orElseThrow(() -> new BaseException(INVALID_USER)); return new GetPostRes(post.getCategory().toString(), getCardList(post), post.getLastModifiedDate(), post.getCommentCount(), post.getScrapCount(), isScrap(user, post), getCommentList(post, user)); - } catch (BaseException e) { throw e; } catch (Exception e) { From acffb476698887f5031af486fefb4d9181fa38c7 Mon Sep 17 00:00:00 2001 From: joonghyun Date: Tue, 19 Sep 2023 18:18:14 +0900 Subject: [PATCH 2/2] =?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