From 3c0676af5a8a629d1251bd8707b815721c6a2901 Mon Sep 17 00:00:00 2001 From: yeji Date: Mon, 27 Nov 2023 00:03:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20jpa=20query=20method=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=EA=B8=B0=20=ED=96=89=EC=82=AC=20=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../program/repository/ProgramRepository.java | 2 +- .../program/service/ProgramService.java | 23 +++---------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/efub/dhs/domain/program/repository/ProgramRepository.java b/src/main/java/com/efub/dhs/domain/program/repository/ProgramRepository.java index 637d236..df4a764 100644 --- a/src/main/java/com/efub/dhs/domain/program/repository/ProgramRepository.java +++ b/src/main/java/com/efub/dhs/domain/program/repository/ProgramRepository.java @@ -22,5 +22,5 @@ List findTop3ByCategoryAndIsOpenAndDeadlineAfterAndProgramIdNotOrderByD @Query(value = "select p from Program p join fetch Heart h on h.program=p where h.member=?1") Page findAllProgramLiked(Member member, Pageable pageable); - List findAllByIsOpenOrderByLikeNumberDesc(Boolean isOpen); + List findTop5ByIsOpenAndDeadlineAfterOrderByLikeNumberDesc(Boolean isOpen, LocalDateTime deadline); } diff --git a/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java b/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java index b71be94..f5942f3 100644 --- a/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java +++ b/src/main/java/com/efub/dhs/domain/program/service/ProgramService.java @@ -2,7 +2,6 @@ import java.time.LocalDateTime; import java.time.Period; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -192,25 +191,9 @@ public ProgramListResponseDto findProgramList(int page, ProgramListRequestDto re } public List findProgramPopular() { - List popularProgramList = programRepository.findAllByIsOpenOrderByLikeNumberDesc(true); - List filteredPopularProgramList = getProgramByRemainingDays(popularProgramList, 5); - return convertToProgramOutlineResponseDtoList(filteredPopularProgramList, null); - } - - public List getProgramByRemainingDays(List programList, int size) { - List filteredList = new ArrayList<>(); - - programList.forEach(program -> { - if (filteredList.size() == size) { - return; - } - Integer remainingDays = calculateRemainingDays(program.getDeadline()); - if (remainingDays >= 0) { - filteredList.add(program); - } - }); - - return filteredList; + List popularProgramList = + programRepository.findTop5ByIsOpenAndDeadlineAfterOrderByLikeNumberDesc(true, LocalDateTime.now()); + return convertToProgramOutlineResponseDtoList(popularProgramList, null); } public Member isLoggedIn(String username) {