Skip to content

Commit

Permalink
feat : 매시 30분 마다 스케줄러 작동하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
minseok1015 committed Aug 19, 2024
1 parent d96f7b3 commit bfda473
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public BaseResponse<?> deleteDebate(@PreAuthorize long userId, @PathVariable Lon

@GetMapping("/trend")
public BaseResponse<List<DebateByKeywordDTO>> getTrendDebate(){
List<DebateByKeywordDTO> debateResponse = debateService.updateHotDebate();
List<DebateByKeywordDTO> debateResponse = debateService.getHotDebate();

return new BaseResponse<>(debateResponse);
}
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void deleteDebate(Long debateId, long userId) {
}

@Transactional
public List<DebateByKeywordDTO> updateHotDebate() {
public void updateHotDebate() {
// 현재 시간 및 48시간 전 시간 계산
Timestamp endTime = new Timestamp(System.currentTimeMillis());
Timestamp startTime = new Timestamp(endTime.getTime() - 3 * 24 * 60 * 60 * 1000); // 3일 전 시간
Expand All @@ -308,7 +308,6 @@ public List<DebateByKeywordDTO> updateHotDebate() {

// 48시간 동안 조회수가 가장 많이 오른 상위 3개의 Debate 조회
List<Debate> debateList = debateRepository.findTop3DebatesCreatedInLast3Days(startTime, pageRequest);
List<DebateByKeywordDTO> debates = new ArrayList<>();

// 새로운 TrendDebate 엔트리 삽입
for (Debate debate : debateList) {
Expand All @@ -319,9 +318,33 @@ public List<DebateByKeywordDTO> updateHotDebate() {

trendDebateRepository.save(trendDebate);

debates.add(new DebateByKeywordDTO(debate.getTitle(), debate.getDebateId(), debate.getImageUrl(), debate.getHits(), (long) debate.getComment().size()));
}

}

@Transactional
public List<DebateByKeywordDTO> getHotDebate() {

// TrendDebate 테이블에서 현재 저장된 Debate 3개를 가져옵니다.
List<TrendDebate> trendDebates = trendDebateRepository.findAll();

// DebateByKeywordDTO 리스트 초기화
List<DebateByKeywordDTO> debates = new ArrayList<>();

// TrendDebate에 있는 Debate를 DebateByKeywordDTO로 변환하여 리스트에 추가
for (TrendDebate trendDebate : trendDebates) {
Debate debate = trendDebate.getDebate();
DebateByKeywordDTO debateDTO = new DebateByKeywordDTO(
debate.getTitle(),
debate.getDebateId(),
debate.getImageUrl(),
debate.getHits(),
(long) debate.getComment().size()
);
debates.add(debateDTO);
}

// 최종 리스트 반환
return debates;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SchedulerService {

private final SeleniumService seleniumService;
private final KeywordService keywordService;
private final DebateService debateService;
private final Redis redis;


Expand Down Expand Up @@ -147,4 +148,9 @@ public void performWeeklyTasks() {
log.info("Weekly task completed.");
}

@Scheduled(cron = "0 30 * * * *")
public void updateTrendDebate(){
debateService.updateHotDebate();
}

}

0 comments on commit bfda473

Please sign in to comment.