diff --git a/Api/src/main/java/com/example/api/Popcorn/controller/PopcornController.java b/Api/src/main/java/com/example/api/Popcorn/controller/PopcornController.java index d1af5e4..b6a9a03 100644 --- a/Api/src/main/java/com/example/api/Popcorn/controller/PopcornController.java +++ b/Api/src/main/java/com/example/api/Popcorn/controller/PopcornController.java @@ -36,6 +36,12 @@ public List getPopcorn() { return getPopcornUseCase.execute(); } + @Operation(summary = "지난 10분 간 만들어진 것들 중에서, 투표수 가장 높았던 3개 반환. 이번 주 상영작임", description = "이번 주 상영작 3개 가져오기") + @GetMapping("/test") + public List getPopcornTest() { + return getPopcornUseCase.testExecute(); + } + //TODO 5. 팝콘작 리뷰하기, 중복 금지 @Operation(summary = "특정 팝콘작에에 리뷰 달기", description = "popcorn id가져와서 리뷰하기") @PostMapping("/review/{popcornId}") diff --git a/Api/src/main/java/com/example/api/Popcorn/service/GetPopcornUseCase.java b/Api/src/main/java/com/example/api/Popcorn/service/GetPopcornUseCase.java index ac9eab8..6dcc249 100644 --- a/Api/src/main/java/com/example/api/Popcorn/service/GetPopcornUseCase.java +++ b/Api/src/main/java/com/example/api/Popcorn/service/GetPopcornUseCase.java @@ -24,4 +24,14 @@ public List execute() { return response; } + public List testExecute() { + List popcornList = popcornAdaptor.findPastHoursPopcorns(); + List response = new ArrayList<>(); + + for(Popcorn popcorn : popcornList) { + final PopcornResponse popcornResponse = PopcornResponse.from(popcorn); + response.add(popcornResponse); + } + return response; + } } diff --git a/Api/src/main/java/com/example/api/recommendedPopcorn/service/GetRecommendedPopocornUseCase.java b/Api/src/main/java/com/example/api/recommendedPopcorn/service/GetRecommendedPopocornUseCase.java index eb66668..046a2ba 100644 --- a/Api/src/main/java/com/example/api/recommendedPopcorn/service/GetRecommendedPopocornUseCase.java +++ b/Api/src/main/java/com/example/api/recommendedPopcorn/service/GetRecommendedPopocornUseCase.java @@ -13,6 +13,6 @@ public class GetRecommendedPopocornUseCase { private final RecommendedPopcornAdaptor recommendedPopcornAdaptor; public List execute() { - return recommendedPopcornAdaptor.findAll(); + return recommendedPopcornAdaptor.findAllThisWeek(); } } diff --git a/Domain/src/main/java/com/example/domains/popcorn/adaptor/PopcornAdaptor.java b/Domain/src/main/java/com/example/domains/popcorn/adaptor/PopcornAdaptor.java index a595bbb..6381762 100644 --- a/Domain/src/main/java/com/example/domains/popcorn/adaptor/PopcornAdaptor.java +++ b/Domain/src/main/java/com/example/domains/popcorn/adaptor/PopcornAdaptor.java @@ -18,6 +18,7 @@ import java.lang.reflect.Field; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.temporal.TemporalField; import java.time.temporal.WeekFields; import java.util.ArrayList; @@ -56,8 +57,32 @@ public Popcorn convertToPopcorn(RecommendedPopcorn rp){ public List findTopThree() { QRecommendedPopcorn recommendedPopcorn = QRecommendedPopcorn.recommendedPopcorn; + LocalDate today = LocalDate.now(); + LocalDate startOfLastWeek = today.minusDays(today.getDayOfWeek().getValue() + 6); // 지난 주의 월요일 + LocalDate endOfLastWeek = startOfLastWeek.plusDays(6); // 지난 주의 일요일 + return jpaQueryFactory .selectFrom(recommendedPopcorn) + .where(recommendedPopcorn.createdAt.between(startOfLastWeek.atStartOfDay(), endOfLastWeek.atTime(23, 59, 59))) + .orderBy( + recommendedPopcorn.recommendationCount.desc(), + recommendedPopcorn.createdAt.desc() + ) + .limit(3) + .fetch(); + } + + public List findTopThreeTest() { + QRecommendedPopcorn recommendedPopcorn = QRecommendedPopcorn.recommendedPopcorn; + + LocalDateTime tenMinutesAgo = LocalDateTime.now().minusMinutes(10); + + return jpaQueryFactory + .selectFrom(recommendedPopcorn) + .where( + recommendedPopcorn.createdAt.after(tenMinutesAgo), + recommendedPopcorn.recommendationCount.gt(0) + ) .orderBy( recommendedPopcorn.recommendationCount.desc(), recommendedPopcorn.createdAt.desc() @@ -437,4 +462,31 @@ public PopcornKeywordResponseDto getTopRatedCounts(Long popcornId) { // .orderBy(popcorn.popcornPostiveCount.desc()) // .limit(3) } + + public List findPastHoursPopcorns() { + QPopcorn popcorn = QPopcorn.popcorn; + + LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1); + + return jpaQueryFactory + .selectFrom(popcorn) + .where( + popcorn.createdAt.after(oneHourAgo), + popcorn.recommendationCount.gt(0) + ) + .orderBy( + popcorn.recommendationCount.desc(), + popcorn.createdAt.desc() + ) + .limit(3) + .fetch(); + } + + public void saveToPopcornTest() { + List listRecommendedPopcorn = findTopThreeTest(); + for (RecommendedPopcorn rp : listRecommendedPopcorn) { + Popcorn movie = convertToPopcorn(rp); + save(movie); + } + } } diff --git a/Domain/src/main/java/com/example/domains/popcorn/schedule/PopcornSchedule.java b/Domain/src/main/java/com/example/domains/popcorn/schedule/PopcornSchedule.java index daeb72d..0c0b29b 100644 --- a/Domain/src/main/java/com/example/domains/popcorn/schedule/PopcornSchedule.java +++ b/Domain/src/main/java/com/example/domains/popcorn/schedule/PopcornSchedule.java @@ -14,15 +14,15 @@ public class PopcornSchedule { private final PopcornService popcornService; // 매 주 일요일 0시 0분에 실행되도록 cron 설정-ㄱㄷ미 - @Scheduled(cron = "0 0 0 * * MON") + @Scheduled(cron = "0 0 0 * * SUN") public void scheduledMethod() { popcornService.getTopRecommended(); - System.out.println("매 주 월요일에 실행되는 작업"); + System.out.println("매 주 일요일에 실행되는 작업"); } // @Scheduled(cron = "0 * * * * *") // public void scheduledMethod() { -// popcornService.getTopRecommended(); +// popcornService.getTopRecommendedTest(); // System.out.println("1분마다 실행되는 작업"); // } } diff --git a/Domain/src/main/java/com/example/domains/popcorn/service/PopcornService.java b/Domain/src/main/java/com/example/domains/popcorn/service/PopcornService.java index 1c9a9cd..587ffb7 100644 --- a/Domain/src/main/java/com/example/domains/popcorn/service/PopcornService.java +++ b/Domain/src/main/java/com/example/domains/popcorn/service/PopcornService.java @@ -15,4 +15,7 @@ public class PopcornService { public void getTopRecommended() { popcornAdaptor.saveToPopcorn(); } + public void getTopRecommendedTest(){ + popcornAdaptor.saveToPopcornTest(); + } } diff --git a/Domain/src/main/java/com/example/domains/recommendedPopcorn/adaptor/RecommendedPopcornAdaptor.java b/Domain/src/main/java/com/example/domains/recommendedPopcorn/adaptor/RecommendedPopcornAdaptor.java index 5b404ba..c1b123a 100644 --- a/Domain/src/main/java/com/example/domains/recommendedPopcorn/adaptor/RecommendedPopcornAdaptor.java +++ b/Domain/src/main/java/com/example/domains/recommendedPopcorn/adaptor/RecommendedPopcornAdaptor.java @@ -13,6 +13,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; +import java.time.DayOfWeek; +import java.time.LocalDate; import java.util.*; @Adaptor @@ -31,8 +33,22 @@ public void checkExists(String movieId) { throw DuplicateMovieId.EXCEPTION; }; } - public List findAll() { - return recommendedPopcornRepository.findAll(); + public List findAllThisWeek() { + QRecommendedPopcorn recommendedPopcorn = QRecommendedPopcorn.recommendedPopcorn; + + LocalDate today = LocalDate.now(); + LocalDate startOfThisWeek = today.with(DayOfWeek.MONDAY); + LocalDate endOfThisWeek = today.with(DayOfWeek.SUNDAY); + + return jpaQueryFactory + .selectFrom(recommendedPopcorn) + .where( + recommendedPopcorn.createdAt.between(startOfThisWeek.atStartOfDay(), endOfThisWeek.atTime(23, 59, 59)) + ) + .orderBy( + recommendedPopcorn.createdAt.asc() + ) + .fetch(); } @Transactional