From 209e5a3d4d7e16aaac5164672618621e404792ed Mon Sep 17 00:00:00 2001 From: Maria Martinez Date: Wed, 9 Oct 2024 11:05:49 -0700 Subject: [PATCH] Added count in the API --- .../service/client/ClientLegacyService.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/ca/bc/gov/app/service/client/ClientLegacyService.java b/backend/src/main/java/ca/bc/gov/app/service/client/ClientLegacyService.java index 096fe43b6c..78fcadce9c 100644 --- a/backend/src/main/java/ca/bc/gov/app/service/client/ClientLegacyService.java +++ b/backend/src/main/java/ca/bc/gov/app/service/client/ClientLegacyService.java @@ -308,34 +308,36 @@ public Flux searchContact(ContactSearchDto dto) { client.clientNumber()) ); } - + @SuppressWarnings("unchecked") public Flux 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 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 json, Long totalCount) {