-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 검색 결과 response에 총 검색 결과 개수 추가 (#107)
- Loading branch information
Showing
8 changed files
with
86 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Api/src/main/java/allchive/server/api/common/page/PageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package allchive.server.api.common.page; | ||
|
||
|
||
import java.util.List; | ||
import org.springframework.data.domain.Page; | ||
|
||
public record PageResponse<T>( | ||
List<T> content, | ||
int page, | ||
int size, | ||
long totalElements, | ||
int totalPages, | ||
boolean hasNextPage) { | ||
public static <T> PageResponse<T> of(Page<T> page) { | ||
return new PageResponse<>( | ||
page.getContent(), | ||
page.getNumber(), | ||
page.getNumberOfElements(), | ||
page.getTotalElements(), | ||
page.getTotalPages(), | ||
page.hasNext()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Domain/src/main/java/allchive/server/domain/common/util/PageUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package allchive.server.domain.common.util; | ||
|
||
|
||
import com.querydsl.core.QueryResults; | ||
import java.util.List; | ||
import org.springframework.data.domain.*; | ||
|
||
public class PageUtil { | ||
public static <T> Page<T> toPage(QueryResults<T> results, Pageable pageable) { | ||
boolean hasNext = hasNext(results.getResults(), pageable); | ||
return new PageImpl<>( | ||
hasNext ? getContent(results.getResults(), pageable) : results.getResults(), | ||
pageable, | ||
results.getTotal()); | ||
} | ||
|
||
// 다음 페이지 있는지 확인 | ||
private static <T> boolean hasNext(List<T> content, Pageable pageable) { | ||
return pageable.isPaged() && content.size() > pageable.getPageSize(); | ||
} | ||
|
||
// 데이터 1개 빼고 반환 | ||
private static <T> List<T> getContent(List<T> content, Pageable pageable) { | ||
return content.subList(0, pageable.getPageSize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters