Skip to content

Commit 867adb0

Browse files
committed
add utility functions to PagedResponse for easier instance creation
1 parent c2deb65 commit 867adb0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/web/response/PagedResponse.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.data.domain.Page;
55

66
import java.util.List;
7+
import java.util.function.Function;
78

89
public record PagedResponse<T>(
910
@NonNull
@@ -12,6 +13,34 @@ public record PagedResponse<T>(
1213
MetaWithPagination meta
1314
) {
1415

16+
public static <T> PagedResponse<T> of(@NonNull Page<T> page) {
17+
return new PagedResponse<>(
18+
page.getContent(),
19+
new MetaWithPagination(
20+
new MetaWithPagination.Pagination(
21+
page.getNumber(),
22+
page.getSize(),
23+
page.getTotalElements()
24+
)
25+
)
26+
);
27+
}
28+
29+
public static <I, O> PagedResponse<O> of(@NonNull Page<I> page, @NonNull Function<I, O> converter) {
30+
var mapped = page.map(converter);
31+
32+
return new PagedResponse<>(
33+
mapped.getContent(),
34+
new MetaWithPagination(
35+
new MetaWithPagination.Pagination(
36+
mapped.getNumber(),
37+
mapped.getSize(),
38+
mapped.getTotalElements()
39+
)
40+
)
41+
);
42+
}
43+
1544
public record MetaWithPagination(
1645
@NonNull
1746
Pagination pagination

0 commit comments

Comments
 (0)