feat(queryAsPath): implement basic logic UNTESTED
This commit is contained in:
parent
a6916ba5da
commit
a3a5a80f74
1 changed files with 40 additions and 6 deletions
|
|
@ -6,6 +6,9 @@ import java.util.Arrays;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Service
|
||||
public class DroneInfoService {
|
||||
|
||||
|
|
@ -94,13 +97,44 @@ public class DroneInfoService {
|
|||
public int[] dronesWithAttribute(String attrName, String attrVal) {
|
||||
URI droneUrl = URI.create(baseUrl).resolve(dronesEndpoint);
|
||||
DroneDto[] drones = restTemplate.getForObject(
|
||||
droneUrl,
|
||||
DroneDto[].class
|
||||
);
|
||||
droneUrl,
|
||||
DroneDto[].class);
|
||||
|
||||
// TODO: Logic unimplemented
|
||||
return new int[] {};
|
||||
if (drones == null) {
|
||||
return new int[] {};
|
||||
}
|
||||
|
||||
// Use Jackson's ObjectMapper to convert DroneDto to JsonNode for dynamic
|
||||
// querying
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
return Arrays.stream(drones)
|
||||
.filter(drone -> {
|
||||
JsonNode node = mapper.valueToTree(drone);
|
||||
JsonNode attrNode = node.findValue(attrName);
|
||||
if (attrNode != null) {
|
||||
return isValueMatched(attrNode, attrVal);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.mapToInt(drone -> Integer.parseInt(drone.id()))
|
||||
.toArray();
|
||||
}
|
||||
|
||||
public int[] dronesMatchesRequirements()
|
||||
private boolean isValueMatched(JsonNode node, String attrVal) {
|
||||
if (node.isTextual()) {
|
||||
return node.asText().equals(attrVal);
|
||||
} else if (node.isNumber()) {
|
||||
return Double.compare(node.asDouble(), Double.parseDouble(attrVal)) == 0;
|
||||
} else if (node.isBoolean()) {
|
||||
return node.asText().equals(attrVal);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int[] dronesMatchesRequirements() {
|
||||
return new int[] {};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue