|
@@ -33,22 +33,33 @@ public class StockService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Quote getStockQuote(String code) {
|
|
public Quote getStockQuote(String code) {
|
|
|
- Quote quote = repository.findByCode(code).orElse(new Quote(code, null, null));
|
|
|
|
|
|
|
+ Quote quote = repository.findByCode(code).orElse(new Quote(code, null, null, null));
|
|
|
quote.setPrice(getLastPrice(quote));
|
|
quote.setPrice(getLastPrice(quote));
|
|
|
quote.setUpdatedAt(now());
|
|
quote.setUpdatedAt(now());
|
|
|
|
|
|
|
|
|
|
+ if (quote.getName() == null) {
|
|
|
|
|
+ quote = updateStockInformation(quote);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
repository.save(quote);
|
|
repository.save(quote);
|
|
|
|
|
|
|
|
return quote;
|
|
return quote;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public Quote updateStockInformation(Quote quote) {
|
|
|
|
|
+ log.info("Current stock's name is null. Requesting latest information...");
|
|
|
|
|
+ StockInfoResponse info = client.getStockInfo(quote.getCode(), key);
|
|
|
|
|
+ quote.setName(info.getName());
|
|
|
|
|
+ return quote;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private BigDecimal getLastPrice(Quote quote) {
|
|
private BigDecimal getLastPrice(Quote quote) {
|
|
|
if (quote.getPrice() == null) {
|
|
if (quote.getPrice() == null) {
|
|
|
log.info("Current quote for [{}] is null. Requesting latest quote...", quote);
|
|
log.info("Current quote for [{}] is null. Requesting latest quote...", quote);
|
|
|
- return new BigDecimal(client.getStockInfo(quote.getCode(), key).get(0).getLastPrice());
|
|
|
|
|
|
|
+ return new BigDecimal(client.getStockQuote(quote.getCode(), key).get(0).getLastPrice());
|
|
|
} else if (quote.getUpdatedAt().isBefore(now().minusDays(1))) {
|
|
} else if (quote.getUpdatedAt().isBefore(now().minusDays(1))) {
|
|
|
log.info("Current quote for [{}] is older than 1 day. Requesting latest quote...", quote);
|
|
log.info("Current quote for [{}] is older than 1 day. Requesting latest quote...", quote);
|
|
|
- return new BigDecimal(client.getStockInfo(quote.getCode(), key).get(0).getLastPrice());
|
|
|
|
|
|
|
+ return new BigDecimal(client.getStockQuote(quote.getCode(), key).get(0).getLastPrice());
|
|
|
} else {
|
|
} else {
|
|
|
return quote.getPrice();
|
|
return quote.getPrice();
|
|
|
}
|
|
}
|
|
@@ -58,11 +69,24 @@ public class StockService {
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
|
@NoArgsConstructor
|
|
@NoArgsConstructor
|
|
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
|
|
- public static class StockResponse {
|
|
|
|
|
|
|
+ public static class StockQuoteResponse {
|
|
|
@JsonProperty("adjClose")
|
|
@JsonProperty("adjClose")
|
|
|
private String lastPrice;
|
|
private String lastPrice;
|
|
|
@JsonProperty("adjOpen")
|
|
@JsonProperty("adjOpen")
|
|
|
private String openPrice;
|
|
private String openPrice;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Data
|
|
|
|
|
+ @AllArgsConstructor
|
|
|
|
|
+ @NoArgsConstructor
|
|
|
|
|
+ @JsonIgnoreProperties(ignoreUnknown = true)
|
|
|
|
|
+ public static class StockInfoResponse {
|
|
|
|
|
+ @JsonProperty("ticker")
|
|
|
|
|
+ private String code;
|
|
|
|
|
+ @JsonProperty("name")
|
|
|
|
|
+ private String name;
|
|
|
|
|
+ @JsonProperty("exchangeCode")
|
|
|
|
|
+ private String exchange;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|