Skip to content

Commit

Permalink
MSEARCH-794: refactor inventory clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mukhiddin-yusuf committed Aug 16, 2024
1 parent c208148 commit 1e55259
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 49 deletions.
17 changes: 11 additions & 6 deletions src/main/java/org/folio/search/client/InventoryHoldingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;

import java.util.List;
import org.folio.search.model.client.CqlQuery;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,14 +16,18 @@ public interface InventoryHoldingClient {
path = "/holdings",
consumes = APPLICATION_OCTET_STREAM_VALUE,
produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryHoldingDto> getHoldings(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);
InventoryHoldingDtoCollection getHoldings(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/holdings", produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryHoldingDto> getHoldings(@RequestParam("offset") int offset,
@RequestParam("limit") int limit,
@RequestParam("totalRecords") String totalRecords);
InventoryHoldingDtoCollection getHoldings(@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/holdings?limit=0&totalRecords=exact", produces = APPLICATION_JSON_VALUE)
InventoryRecordsCountDto getHoldingsCount();

record InventoryHoldingDto(String id) {}

record InventoryHoldingDtoCollection(List<InventoryHoldingDto> holdingsRecords, int totalRecords) {}
}
17 changes: 11 additions & 6 deletions src/main/java/org/folio/search/client/InventoryInstanceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;

import java.util.List;
import org.folio.search.model.client.CqlQuery;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,14 +16,18 @@ public interface InventoryInstanceClient {
path = "/instances",
consumes = APPLICATION_OCTET_STREAM_VALUE,
produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryInstanceDto> getInstances(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);
InventoryInstanceDtoCollection getInstances(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/instances", produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryInstanceDto> getInstances(@RequestParam("offset") int offset,
@RequestParam("limit") int limit,
@RequestParam("totalRecords") String totalRecords);
InventoryInstanceDtoCollection getInstances(@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/instances?limit=0&totalRecords=exact", produces = APPLICATION_JSON_VALUE)
InventoryRecordsCountDto getInstancesCount();

record InventoryInstanceDto(String id) {}

record InventoryInstanceDtoCollection(List<InventoryInstanceDto> instances, int totalRecords) {}
}
17 changes: 11 additions & 6 deletions src/main/java/org/folio/search/client/InventoryItemClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;

import java.util.List;
import org.folio.search.model.client.CqlQuery;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,14 +16,18 @@ public interface InventoryItemClient {
path = "/items",
consumes = APPLICATION_OCTET_STREAM_VALUE,
produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryItemDto> getItems(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);
InventoryItemDtoCollection getItems(@RequestParam("query") CqlQuery cql,
@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/items", produces = APPLICATION_JSON_VALUE)
InventoryRecordDtoCollection<InventoryItemDto> getItems(@RequestParam("offset") int offset,
@RequestParam("limit") int limit,
@RequestParam("totalRecords") String totalRecords);
InventoryItemDtoCollection getItems(@RequestParam("offset") int offset,
@RequestParam("limit") int limit);

@GetMapping(path = "/items?limit=0&totalRecords=exact", produces = APPLICATION_JSON_VALUE)
InventoryRecordsCountDto getItemsCount();

record InventoryItemDto(String id) {}

record InventoryItemDtoCollection(List<InventoryItemDto> items, int totalRecords) {}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.folio.search.client;

public record InventoryRecordsCountDto(int totalRecords) { }
16 changes: 0 additions & 16 deletions src/main/java/org/folio/search/client/TotalRecordsType.java

This file was deleted.

19 changes: 9 additions & 10 deletions src/main/java/org/folio/search/integration/InventoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.folio.search.client.InventoryInstanceClient;
import org.folio.search.client.InventoryItemClient;
import org.folio.search.client.InventoryReindexRecordsClient;
import org.folio.search.client.TotalRecordsType;
import org.folio.search.exception.FolioIntegrationException;
import org.folio.search.model.client.CqlQuery;
import org.folio.search.model.reindex.MergeRangeEntity;
Expand Down Expand Up @@ -62,9 +61,9 @@ public int fetchInventoryRecordCount(InventoryRecordType recordType) {

try {
var result = switch (recordType) {
case INSTANCE -> inventoryInstanceClient.getInstances(0, 0, TotalRecordsType.EXACT.getValue());
case ITEM -> inventoryItemClient.getItems(0, 0, TotalRecordsType.EXACT.getValue());
case HOLDING -> inventoryHoldingClient.getHoldings(0, 0, TotalRecordsType.EXACT.getValue());
case INSTANCE -> inventoryInstanceClient.getInstancesCount();
case ITEM -> inventoryItemClient.getItemsCount();
case HOLDING -> inventoryHoldingClient.getHoldingsCount();
};

if (result == null) {
Expand Down Expand Up @@ -105,14 +104,14 @@ public void publishReindexRecordsRange(MergeRangeEntity rangeEntity) {
private List<UUID> fetchInstances(CqlQuery cqlQuery, int offset, int limit) {
var result = Optional.ofNullable(cqlQuery)
.map(query -> inventoryInstanceClient.getInstances(query, offset, limit))
.orElseGet(() -> inventoryInstanceClient.getInstances(offset, limit, TotalRecordsType.AUTO.getValue()));
.orElseGet(() -> inventoryInstanceClient.getInstances(offset, limit));

if (result == null) {
log.warn("Failed to retrieve Inventory Instances, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit);
return Collections.emptyList();
}

return result.records().stream()
return result.instances().stream()
.map(InventoryInstanceClient.InventoryInstanceDto::id)
.map(UUID::fromString)
.toList();
Expand All @@ -121,14 +120,14 @@ private List<UUID> fetchInstances(CqlQuery cqlQuery, int offset, int limit) {
private List<UUID> fetchItems(CqlQuery cqlQuery, int offset, int limit) {
var result = Optional.ofNullable(cqlQuery)
.map(query -> inventoryItemClient.getItems(query, offset, limit))
.orElseGet(() -> inventoryItemClient.getItems(offset, limit, TotalRecordsType.AUTO.getValue()));
.orElseGet(() -> inventoryItemClient.getItems(offset, limit));

if (result == null) {
log.warn("Failed to retrieve Inventory Items, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit);
return Collections.emptyList();
}

return result.records().stream()
return result.items().stream()
.map(InventoryItemClient.InventoryItemDto::id)
.map(UUID::fromString)
.toList();
Expand All @@ -137,14 +136,14 @@ private List<UUID> fetchItems(CqlQuery cqlQuery, int offset, int limit) {
private List<UUID> fetchHoldings(CqlQuery cqlQuery, int offset, int limit) {
var result = Optional.ofNullable(cqlQuery)
.map(query -> inventoryHoldingClient.getHoldings(query, offset, limit))
.orElseGet(() -> inventoryHoldingClient.getHoldings(offset, limit, TotalRecordsType.AUTO.getValue()));
.orElseGet(() -> inventoryHoldingClient.getHoldings(offset, limit));

if (result == null) {
log.warn("Failed to retrieve Inventory Holdings, [query: {}, offset: {}, limit: {}]", cqlQuery, offset, limit);
return Collections.emptyList();
}

return result.records().stream()
return result.holdingsRecords().stream()
.map(InventoryHoldingClient.InventoryHoldingDto::id)
.map(UUID::fromString)
.toList();
Expand Down

0 comments on commit 1e55259

Please sign in to comment.