This commit is contained in:
@@ -9,7 +9,7 @@ import java.util.List;
|
||||
public class SearchApiToYandexResponseMapper {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public YandexSearchResponse mapToYandexResponse(String searchApiJson) throws Exception {
|
||||
public YandexSearchResponse mapToYandexResponse(String searchApiJson, int page, int pageSize) throws Exception {
|
||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
|
||||
YandexSearchResponse response = new YandexSearchResponse();
|
||||
@@ -18,9 +18,12 @@ public class SearchApiToYandexResponseMapper {
|
||||
JsonNode visualMatches = root.path("visual_matches");
|
||||
|
||||
if (visualMatches.isArray()) {
|
||||
for (JsonNode match : visualMatches) {
|
||||
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
|
||||
int startIndex = (page - 1) * pageSize;
|
||||
int endIndex = Math.min(startIndex + pageSize, visualMatches.size());
|
||||
|
||||
for (int i = startIndex; i < endIndex; i++) {
|
||||
JsonNode match = visualMatches.get(i);
|
||||
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
|
||||
|
||||
JsonNode imageNode = match.path("image");
|
||||
if (!imageNode.isMissingNode()) {
|
||||
@@ -30,7 +33,6 @@ public class SearchApiToYandexResponseMapper {
|
||||
}
|
||||
|
||||
result.setPageUrl(match.path("link").asText());
|
||||
|
||||
result.setPageTitle(match.path("title").asText());
|
||||
|
||||
String source = match.path("source").asText();
|
||||
@@ -40,6 +42,11 @@ public class SearchApiToYandexResponseMapper {
|
||||
imageResults.add(result);
|
||||
}
|
||||
}
|
||||
|
||||
response.setPage(page);
|
||||
response.setPageSize(pageSize);
|
||||
response.setTotalResults(visualMatches.size());
|
||||
response.setTotalPages((int) Math.ceil((double) visualMatches.size() / pageSize));
|
||||
}
|
||||
|
||||
response.setImages(imageResults);
|
||||
|
||||
Reference in New Issue
Block a user