Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short form api에 nextpage값 추가 #248

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}