Skip to content

Commit

Permalink
feat: PageInfo에 다음 커서 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
shb03323 committed Oct 5, 2023
1 parent 41c7f27 commit 80b28f3
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package touch.baton.domain.common.response;

public interface IdExtractable {

Long extractId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@

import java.util.List;

public record PageResponse<T>(List<T> data, PageInfo pageInfo) {
public record PageResponse<T extends IdExtractable>(List<T> data, PageInfo pageInfo) {

private static final int NEXT_INDEX = 1;

public static <T> PageResponse<T> of(final List<T> responses, final PageParams pageParams) {
public static <T extends IdExtractable> PageResponse<T> of(final List<T> responses, final PageParams pageParams) {
final int limit = pageParams.limit();
if (isLastPage(responses, limit)) {
return new PageResponse<>(responses, PageInfo.Last());
return new PageResponse<>(responses, PageInfo.last());
}
return new PageResponse<>(responses.subList(0, limit + NEXT_INDEX), PageInfo.Normal());
final List<T> limitResponses = responses.subList(0, pageParams.getLimitForQuery());
return new PageResponse<>(limitResponses, PageInfo.normal(getLastElementId(limitResponses)));
}

public record PageInfo(boolean isLast) {
public record PageInfo(boolean isLast, Long nextCursor) {

public static PageInfo Last() {
return new PageInfo(true);
public static PageInfo last() {
return new PageInfo(true, null);
}

public static PageInfo Normal() {
return new PageInfo(false);
public static PageInfo normal(final Long nextCursor) {
return new PageInfo(false, nextCursor);
}
}

private static <T> boolean isLastPage(final List<T> responses, final int limit) {
return responses.size() <= limit;
}

private static <T extends IdExtractable> Long getLastElementId(final List<T> responses) {
final int lastIndex = responses.size() - 1;
return responses.get(lastIndex).extractId();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package touch.baton.domain.runnerpost.query.controller.response;

import touch.baton.domain.common.response.IdExtractable;
import touch.baton.domain.member.query.controller.response.RunnerResponse;
import touch.baton.domain.runnerpost.command.RunnerPost;
import touch.baton.domain.runnerpost.command.vo.ReviewStatus;
Expand Down Expand Up @@ -59,7 +60,7 @@ public record Simple(Long runnerPostId,
String reviewStatus,
RunnerResponse.Simple runnerProfile,
List<String> tags
) {
) implements IdExtractable {

public static Simple of(final RunnerPost runnerPost,
final long applicantCount,
Expand All @@ -76,6 +77,11 @@ public static Simple of(final RunnerPost runnerPost,
convertToTags(runnerPost, runnerPostTags)
);
}

@Override
public Long extractId() {
return runnerPostId;
}
}

public record Mine(Long runnerPostId,
Expand Down Expand Up @@ -106,7 +112,7 @@ public record SimpleByRunner(Long runnerPostId,
Long supporterId,
List<String> tags

) {
) implements IdExtractable {

public static SimpleByRunner of(final RunnerPost runnerPost,
final long applicantCount,
Expand All @@ -131,6 +137,11 @@ private static Long getSupporterIdByRunnerPost(final RunnerPost runnerPost) {
}
return runnerPost.getSupporter().getId();
}

@Override
public Long extractId() {
return runnerPostId;
}
}

private static List<String> convertToTags(final RunnerPost runnerPost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -76,7 +76,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -112,7 +112,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -149,7 +149,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -185,7 +185,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -222,7 +222,7 @@ class RunnerPostPageAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.Simple> 기대된_러너_게시글_전체_Simple_페이징_응답 = 러너_게시글_전체_Simple_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RunnerPostPageRunnerAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.SimpleByRunner> 기대된_러너_게시글_페이징_응답 = 러너와_연관된_러너_게시글_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down Expand Up @@ -76,7 +76,7 @@ class RunnerPostPageRunnerAssuredTest extends AssuredTestConfig {
ReviewStatus.NOT_STARTED,
List.of("자바", "스프링")
);
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.Last();
final PageResponse.PageInfo 기대된_페이징_정보 = PageResponse.PageInfo.last();
final PageResponse<RunnerPostResponse.SimpleByRunner> 기대된_러너_게시글_페이징_응답 = 러너와_연관된_러너_게시글_페이징_응답(
List.of(기대된_러너_게시글_Simple_응답),
기대된_페이징_정보
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RunnerPostPageSupporterAssuredTest extends AssuredTestConfig {
final int 페이지_크기 = 10;
final PageResponse<RunnerPostResponse.Simple> 서포터가_리뷰를_지원한_대기중인_러너_게시글_페이징_응답 = 서포터와_연관된_러너_게시글_페이징_응답(
List.of(서포터가_리뷰를_지원한_대기중인_러너_게시글_응답),
PageResponse.PageInfo.Last()
PageResponse.PageInfo.last()
);

// then
Expand Down Expand Up @@ -96,7 +96,7 @@ class RunnerPostPageSupporterAssuredTest extends AssuredTestConfig {
final int 페이지_크기 = 10;
final PageResponse<RunnerPostResponse.Simple> 서포터가_리뷰를_지원한_진행중인_러너_게시글_페이징_응답 = 서포터와_연관된_러너_게시글_페이징_응답(
List.of(서포터가_리뷰를_지원한_진행중인_러너_게시글_응답),
PageResponse.PageInfo.Last()
PageResponse.PageInfo.last()
);

// then
Expand Down Expand Up @@ -139,7 +139,7 @@ class RunnerPostPageSupporterAssuredTest extends AssuredTestConfig {
final int 페이지_크기 = 10;
final PageResponse<RunnerPostResponse.Simple> 서포터가_리뷰를_완료한_러너_게시글_페이징_응답 = 서포터와_연관된_러너_게시글_페이징_응답(
List.of(서포터가_리뷰를_완료한_러너_게시글_응답),
PageResponse.PageInfo.Last()
PageResponse.PageInfo.last()
);

// then
Expand Down Expand Up @@ -186,7 +186,7 @@ class RunnerPostPageSupporterAssuredTest extends AssuredTestConfig {
final int 페이지_크기 = 10;
final PageResponse<RunnerPostResponse.Simple> 서포터가_리뷰를_완료한_러너_게시글_페이징_응답 = 서포터와_연관된_러너_게시글_페이징_응답(
List.of(서포터가_리뷰를_완료한_러너_게시글_응답),
PageResponse.PageInfo.Last()
PageResponse.PageInfo.last()
);

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ void readRunnerPostBySupporterIdAndReviewStatus() throws Exception {
fieldWithPath("data.[].runnerProfile.name").type(STRING).description("러너 게시글의 러너 프로필 이름"),
fieldWithPath("data.[].runnerProfile.imageUrl").type(STRING).description("러너 게시글의 러너 프로필 이미지"),
fieldWithPath("data.[].tags.[]").type(ARRAY).description("러너 게시글의 태그 목록"),
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부")
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부"),
fieldWithPath("pageInfo.nextCursor").type(NUMBER).optional().description("다음 커서")
))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void readRunnerPostsByTagNamesAndReviewStatus() throws Exception {
fieldWithPath("data.[].runnerProfile.name").type(STRING).description("러너 게시글의 러너 프로필 이름"),
fieldWithPath("data.[].runnerProfile.imageUrl").type(STRING).description("러너 게시글의 러너 프로필 이미지"),
fieldWithPath("data.[].tags.[]").type(ARRAY).description("러너 게시글의 태그 목록"),
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부")
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부"),
fieldWithPath("pageInfo.nextCursor").type(NUMBER).optional().description("다음 커서")
))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ void readRunnerPostByLoginedRunnerAndReviewStatus() throws Exception {
.optional()
.description("서포터 id (서포터가 존재할 때 NUMBER, 아닌 경우에 NULL)"),
fieldWithPath("data.[].tags.[]").type(ARRAY).description("러너 게시글의 태그 목록"),
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부")
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부"),
fieldWithPath("pageInfo.nextCursor").type(NUMBER).optional().description("다음 커서")
))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ void readRunnerPostByLoginedSupporterAndReviewStatus() throws Exception {
fieldWithPath("data.[].runnerProfile.name").type(STRING).description("러너 게시글의 러너 프로필 이름"),
fieldWithPath("data.[].runnerProfile.imageUrl").type(STRING).description("러너 게시글의 러너 프로필 이미지"),
fieldWithPath("data.[].tags.[]").type(ARRAY).description("러너 게시글의 태그 목록"),
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부")
fieldWithPath("pageInfo.isLast").type(BOOLEAN).description("마지막 페이지 여부"),
fieldWithPath("pageInfo.nextCursor").type(NUMBER).optional().description("다음 커서")
))
);
}
Expand Down

0 comments on commit 80b28f3

Please sign in to comment.