From e6ef10e475ed49d84410ce0eaaa0006a27be35ed Mon Sep 17 00:00:00 2001 From: BK Date: Sun, 12 Nov 2023 22:09:03 +0900 Subject: [PATCH] =?UTF-8?q?Chore:=20=EC=8B=9C=EC=9E=91=EC=95=88=ED=95=9C?= =?UTF-8?q?=20=EC=B1=8C=EB=A6=B0=EC=A7=80=20=EC=A1=B0=ED=9A=8C=20-=20?= =?UTF-8?q?=EC=A3=BC=EC=A0=9C=20=EA=B2=80=EC=83=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BE/controller/ChallengeController.java | 5 +++-- .../repository/ChallengeCustomRepository.java | 1 + .../ChallengeCustomRepositoryImpl.java | 15 +++++++++++++-- .../igoMoney/BE/service/ChallengeService.java | 9 +++++++-- .../BE/service/ChallengeServiceTest.java | 18 ++++++++++-------- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/igoMoney/BE/controller/ChallengeController.java b/src/main/java/igoMoney/BE/controller/ChallengeController.java index ea47588..0bfd78c 100644 --- a/src/main/java/igoMoney/BE/controller/ChallengeController.java +++ b/src/main/java/igoMoney/BE/controller/ChallengeController.java @@ -26,9 +26,10 @@ public class ChallengeController { @GetMapping("notstarted") public ResponseEntity> getNotStartedChallengeList( @RequestParam(value="lastId", required=false, defaultValue="10") Long lastId, - @RequestParam(value="pageSize", required=false, defaultValue="10") int pageSize) { + @RequestParam(value="pageSize", required=false, defaultValue="10") int pageSize, + @RequestParam(value="categoryId", required=false, defaultValue="-1") Integer categoryId) { - List response = challengeService.getNotStartedChallengeList(lastId, pageSize); + List response = challengeService.getNotStartedChallengeList(lastId, pageSize, categoryId); return ResponseEntity.status(HttpStatus.OK).body(response); } diff --git a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java index 234160a..43546f3 100644 --- a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java +++ b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepository.java @@ -7,4 +7,5 @@ public interface ChallengeCustomRepository { List findAllNotStarted(int pageSize, Long lastId, LocalDate date); + List findAllNotStartedByCategory(int pageSize, Long lastId, LocalDate date, Integer categoryId); } diff --git a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java index 318f445..936e034 100644 --- a/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java +++ b/src/main/java/igoMoney/BE/repository/ChallengeCustomRepositoryImpl.java @@ -20,8 +20,19 @@ public ChallengeCustomRepositoryImpl(JPAQueryFactory jpaQueryFactory) { @Override public List findAllNotStarted(int pageSize, Long lastId, LocalDate date){ return jpaQueryFactory.selectFrom(challenge) - .where(ltChallengeId(lastId), - challenge.startDate.gt(date)) + .where(challenge.startDate.gt(date), + ltChallengeId(lastId)) + .orderBy(challenge.id.desc()) + .limit(pageSize) + .fetch(); + } + + @Override + public List findAllNotStartedByCategory(int pageSize, Long lastId, LocalDate date, Integer categoryId){ + return jpaQueryFactory.selectFrom(challenge) + .where( challenge.categoryId.eq(categoryId), + challenge.startDate.gt(date), + ltChallengeId(lastId)) .orderBy(challenge.id.desc()) .limit(pageSize) .fetch(); diff --git a/src/main/java/igoMoney/BE/service/ChallengeService.java b/src/main/java/igoMoney/BE/service/ChallengeService.java index aa74d2d..2717ddc 100644 --- a/src/main/java/igoMoney/BE/service/ChallengeService.java +++ b/src/main/java/igoMoney/BE/service/ChallengeService.java @@ -36,10 +36,15 @@ public class ChallengeService { DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM월 dd일"); // 시작 안 한 챌린지 목록 조회 - public List getNotStartedChallengeList(Long lastId, int pageSize) { + public List getNotStartedChallengeList(Long lastId, int pageSize, Integer categoryId) { List responseList = new ArrayList<>(); - List challengeList = challengeRepository.findAllNotStarted(pageSize, lastId, LocalDate.now()); + List challengeList; + if (categoryId == -1){ + challengeList = challengeRepository.findAllNotStarted(pageSize, lastId, LocalDate.now()); + } else{ + challengeList = challengeRepository.findAllNotStartedByCategory(pageSize, lastId, LocalDate.now(), categoryId); + } for (Challenge challenge: challengeList){ ChallengeResponse challengeResponse = ChallengeResponse.builder() .id(challenge.getId()) diff --git a/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java b/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java index 67d5191..0eee1f1 100644 --- a/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java +++ b/src/test/java/igoMoney/BE/service/ChallengeServiceTest.java @@ -14,6 +14,7 @@ import java.util.List; import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.*; @RunWith(SpringRunner.class) @SpringBootTest @@ -33,24 +34,25 @@ public class ChallengeServiceTest { .content("__") .targetAmount(10000) .startDate(LocalDate.now().plusDays(2)) + .categoryId(1) .build()); } //when List challenges = challengeRepository.findAllNotStarted(10, null, LocalDate.now()); // pageNo는 0부터 시작이라 1이면 두번째 페이지 조회 + List challenges2 = challengeRepository.findAllNotStarted(10, 21L, LocalDate.now()); // DESC. 20~11 + List challenges3 = challengeRepository.findAllNotStartedByCategory(10, 21L, LocalDate.now(), 2); //then assertEquals(challenges.size(),10); assertEquals(30,(long) challenges.get(0).getId()); assertEquals(21, (long) challenges.get(9).getId()); - // [NoOffset 2번째 페이지] -// //when -// List challenges = challengeRepository.findAllNotStarted(10, 21L); // DESC. 20~11 -// -// //then -// assertThat(challenges).hasSize(10); -// assertThat(challenges.get(0).getId()).isEqualTo(20); -// assertThat(challenges.get(9).getId()).isEqualTo(11); + assertThat(challenges2).hasSize(10); + assertThat(challenges2.get(0).getId()).isEqualTo(20); + assertThat(challenges2.get(9).getId()).isEqualTo(11); + + assertThat(challenges3).hasSize(0); + } }