Skip to content

Commit

Permalink
Type: 시작안한 챌린지 조회 - 주제 검색 추가 (#72)
Browse files Browse the repository at this point in the history
[Feat] 시작안한 챌린지 조회 - 주제 검색 추가
  • Loading branch information
pingowl authored Nov 12, 2023
2 parents b94e4d7 + e6ef10e commit e9030ac
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

Expand All @@ -21,6 +22,8 @@ protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
chain.doFilter(req, res); // go to 'JwtAuthFilter'
} catch (JwtException ex) {
setErrorResponse(HttpStatus.UNAUTHORIZED, res, ex);
} catch (UsernameNotFoundException ex){
setErrorResponse(HttpStatus.UNAUTHORIZED, res, ex);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/igoMoney/BE/controller/ChallengeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public class ChallengeController {
@GetMapping("notstarted")
public ResponseEntity<List<ChallengeResponse>> 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<ChallengeResponse> response = challengeService.getNotStartedChallengeList(lastId, pageSize);
List<ChallengeResponse> response = challengeService.getNotStartedChallengeList(lastId, pageSize, categoryId);

return ResponseEntity.status(HttpStatus.OK).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface ChallengeCustomRepository {
List<Challenge> findAllNotStarted(int pageSize, Long lastId, LocalDate date);
List<Challenge> findAllNotStartedByCategory(int pageSize, Long lastId, LocalDate date, Integer categoryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@ public ChallengeCustomRepositoryImpl(JPAQueryFactory jpaQueryFactory) {
@Override
public List<Challenge> 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<Challenge> 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();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/igoMoney/BE/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ public class ChallengeService {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM월 dd일");

// 시작 안 한 챌린지 목록 조회
public List<ChallengeResponse> getNotStartedChallengeList(Long lastId, int pageSize) {
public List<ChallengeResponse> getNotStartedChallengeList(Long lastId, int pageSize, Integer categoryId) {

List<ChallengeResponse> responseList = new ArrayList<>();
List<Challenge> challengeList = challengeRepository.findAllNotStarted(pageSize, lastId, LocalDate.now());
List<Challenge> 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())
Expand Down
18 changes: 10 additions & 8 deletions src/test/java/igoMoney/BE/service/ChallengeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,24 +34,25 @@ public class ChallengeServiceTest {
.content("__")
.targetAmount(10000)
.startDate(LocalDate.now().plusDays(2))
.categoryId(1)
.build());
}

//when
List<Challenge> challenges = challengeRepository.findAllNotStarted(10, null, LocalDate.now()); // pageNo는 0부터 시작이라 1이면 두번째 페이지 조회
List<Challenge> challenges2 = challengeRepository.findAllNotStarted(10, 21L, LocalDate.now()); // DESC. 20~11
List<Challenge> 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<Challenge> 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);

}
}

0 comments on commit e9030ac

Please sign in to comment.