Skip to content

Commit

Permalink
Merge pull request #32 from Central-MakeUs/fix/recommendPopcorn
Browse files Browse the repository at this point in the history
[Fix]recommend popcorn
  • Loading branch information
AlmondBreez3 authored Jan 28, 2024
2 parents 4707a1a + c6e8d82 commit fcbc91d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public List<PopcornResponse> getPopcorn() {
return getPopcornUseCase.execute();
}

@Operation(summary = "지난 10분 간 만들어진 것들 중에서, 투표수 가장 높았던 3개 반환. 이번 주 상영작임", description = "이번 주 상영작 3개 가져오기")
@GetMapping("/test")
public List<PopcornResponse> getPopcornTest() {
return getPopcornUseCase.testExecute();
}

//TODO 5. 팝콘작 리뷰하기, 중복 금지
@Operation(summary = "특정 팝콘작에에 리뷰 달기", description = "popcorn id가져와서 리뷰하기")
@PostMapping("/review/{popcornId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public List<PopcornResponse> execute() {
return response;
}

public List<PopcornResponse> testExecute() {
List<Popcorn> popcornList = popcornAdaptor.findPastHoursPopcorns();
List<PopcornResponse> response = new ArrayList<>();

for(Popcorn popcorn : popcornList) {
final PopcornResponse popcornResponse = PopcornResponse.from(popcorn);
response.add(popcornResponse);
}
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
public class GetRecommendedPopocornUseCase {
private final RecommendedPopcornAdaptor recommendedPopcornAdaptor;
public List<RecommendedPopcorn> execute() {
return recommendedPopcornAdaptor.findAll();
return recommendedPopcornAdaptor.findAllThisWeek();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,8 +57,32 @@ public Popcorn convertToPopcorn(RecommendedPopcorn rp){
public List<RecommendedPopcorn> 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<RecommendedPopcorn> 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()
Expand Down Expand Up @@ -437,4 +462,31 @@ public PopcornKeywordResponseDto getTopRatedCounts(Long popcornId) {
// .orderBy(popcorn.popcornPostiveCount.desc())
// .limit(3)
}

public List<Popcorn> 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<RecommendedPopcorn> listRecommendedPopcorn = findTopThreeTest();
for (RecommendedPopcorn rp : listRecommendedPopcorn) {
Popcorn movie = convertToPopcorn(rp);
save(movie);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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분마다 실행되는 작업");
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public class PopcornService {
public void getTopRecommended() {
popcornAdaptor.saveToPopcorn();
}
public void getTopRecommendedTest(){
popcornAdaptor.saveToPopcornTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,8 +33,22 @@ public void checkExists(String movieId) {
throw DuplicateMovieId.EXCEPTION;
};
}
public List<RecommendedPopcorn> findAll() {
return recommendedPopcornRepository.findAll();
public List<RecommendedPopcorn> 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
Expand Down

0 comments on commit fcbc91d

Please sign in to comment.