Skip to content

Commit

Permalink
Short form api에 nextpage값 추가
Browse files Browse the repository at this point in the history
Short form api에 nextpage값 추가
  • Loading branch information
xGreenNarae authored Nov 6, 2023
2 parents c1bbd04 + b93cc65 commit 8a71f6f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
public record CategoryShortFormPage(

List<ShortFormDto> shortForms,
boolean hasNext
boolean hasNext,
Integer nextPage
) {

public static CategoryShortFormPage of(final List<PetVideo> petVideos,
final boolean hasNext) {
final boolean hasNext,
final Integer nextPage) {
return new CategoryShortFormPage(
petVideos.stream()
.map(ShortFormDto::of)
.toList(),
hasNext
hasNext,
nextPage
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

public record HomeShortFormPage(
List<ShortFormDto> shortForms,
boolean hasNext
boolean hasNext,
Integer nextPage
) {

public static HomeShortFormPage of(final List<PetVideo> petVideos,
final boolean hasNext) {
final boolean hasNext,
final Integer nextPage) {
return new HomeShortFormPage(
petVideos.stream()
.map(ShortFormDto::of)
.toList(),
hasNext
hasNext,
nextPage
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,28 @@ public class ShortFormService {
public HomeShortFormPage getHomeShortFormPage(final Pageable pageable) {

// Fetch Join + Pageable 동시에 수행하는 경우 발생하는 문제(HHH000104) 해결을 위해 쿼리를 두 개로 분할하였습니다.
Slice<Integer> petVideoIdSlice = petVideoJpaRepository.findPetVideoIdsBy(pageable);
List<PetVideo> petVideos = petVideoJpqlRepository.findAllByIds(petVideoIdSlice.getContent());
final Slice<Integer> petVideoIdSlice = petVideoJpaRepository.findPetVideoIdsBy(pageable);
final List<PetVideo> petVideos = petVideoJpqlRepository.findAllByIds(petVideoIdSlice.getContent());

// LikeCount DESC 순서로 조회하고, 반환된 페이지(Slice)를 랜덤으로 섞는다.
return HomeShortFormPage.of(
shuffleVideos(petVideos),
petVideoIdSlice.hasNext()
petVideoIdSlice.hasNext(),
getNextPage(petVideoIdSlice.hasNext(), pageable)
);
}

public CategoryShortFormPage getCategoryShortFormPage(final ShortFormSearchCondition searchCondition,
final Pageable pageable) {
Slice<Integer> petVideoIds = petVideoJpqlRepository
final Slice<Integer> petVideoIds = petVideoJpqlRepository
.findPetVideoIdsBy(searchCondition.type(), searchCondition.area(), pageable);

List<PetVideo> petVideos = petVideoJpqlRepository.findAllByIds(petVideoIds.getContent());
final List<PetVideo> petVideos = petVideoJpqlRepository.findAllByIds(petVideoIds.getContent());

return CategoryShortFormPage.of(
shuffleVideos(petVideos),
petVideoIds.hasNext()
petVideoIds.hasNext(),
getNextPage(petVideoIds.hasNext(), pageable)
);
}

Expand All @@ -56,4 +58,9 @@ private List<PetVideo> shuffleVideos(final List<PetVideo> petVideos) {
Collections.shuffle(petVideos);
return petVideos;
}

private Integer getNextPage(final boolean hasNext,
final Pageable pageable) {
return hasNext ? pageable.getPageNumber() + 1 : null;
}
}

0 comments on commit 8a71f6f

Please sign in to comment.