Skip to content

Commit

Permalink
Added count in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
mamartinezmejia committed Oct 9, 2024
1 parent 5b7034e commit 209e5a3
Showing 1 changed file with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,34 +308,36 @@ public Flux<ForestClientDto> searchContact(ContactSearchDto dto) {
client.clientNumber())
);
}

@SuppressWarnings("unchecked")
public Flux<ClientListDto> search(int page, int size, String keyword) {
log.info(
"Searching clients by keyword {} with page {} and size {}",
keyword,
page,
"Searching clients by keyword {} with page {} and size {}",
keyword,
page,
size
);

return legacyApi
.get()
.uri(builder ->
builder
.path("/api/search")
.queryParam("page", page)
.queryParam("size", size)
.queryParam("value", keyword)
.build(Map.of()))
.exchangeToFlux(response -> response.bodyToFlux(Map.class))
.collectList()
.flatMapMany(clients -> {
long totalCount = clients.size();
return Flux.fromIterable(clients)
.map(clientJson -> mapToClientListDto(clientJson, totalCount));
.path("/api/search")
.queryParam("page", page)
.queryParam("size", size)
.queryParam("value", keyword)
.build(Map.of())
)
.exchangeToFlux(response -> {
List<String> totalCountHeader = response.headers().header("X-Total-Count");
Long count = totalCountHeader.isEmpty() ? 0L : Long.valueOf(totalCountHeader.get(0));

return response
.bodyToFlux(Map.class)
.map(json -> mapToClientListDto(json, count));
})
.doOnNext(dto ->
log.info("Found client with clientNumber {} and total count: {}", dto.clientNumber(), dto.count()));
log.info("Found client with clientNumber {} and total count: {}", dto.clientNumber(), dto.count()));
}

private ClientListDto mapToClientListDto(Map<String, Object> json, Long totalCount) {
Expand Down

0 comments on commit 209e5a3

Please sign in to comment.