|
|
@@ -1,18 +1,10 @@
|
|
|
package com.danielbohry.stocks.repository.stock;
|
|
|
|
|
|
-import com.danielbohry.stocks.client.StockClient;
|
|
|
import com.danielbohry.stocks.domain.Stock;
|
|
|
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
-import feign.FeignException;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
-import lombok.Data;
|
|
|
-import lombok.NoArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
@@ -20,22 +12,14 @@ import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.time.Instant.now;
|
|
|
-import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
@Slf4j
|
|
|
@Repository
|
|
|
+@AllArgsConstructor
|
|
|
public class StockRepository {
|
|
|
|
|
|
private final QuoteRepository repository;
|
|
|
- private final StockClient client;
|
|
|
- private final String key;
|
|
|
-
|
|
|
- public StockRepository(QuoteRepository repository, StockClient client, @Value("${clients.stock.key}") String key) {
|
|
|
- this.repository = repository;
|
|
|
- this.client = client;
|
|
|
- this.key = key;
|
|
|
- }
|
|
|
|
|
|
public List<Stock> findAll() {
|
|
|
return repository.findAll();
|
|
|
@@ -80,87 +64,12 @@ public class StockRepository {
|
|
|
|
|
|
public boolean isValid(String code) {
|
|
|
Stock quote = repository.findByCode(code).stream().findFirst().orElse(null);
|
|
|
-
|
|
|
- if (quote != null) return true;
|
|
|
-
|
|
|
- try {
|
|
|
- log.info("Current stock's name is null. Requesting latest information...");
|
|
|
- client.getStockInfo(code, key);
|
|
|
- return true;
|
|
|
- } catch (FeignException.NotFound e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return quote != null;
|
|
|
}
|
|
|
|
|
|
public Stock getStockQuote(String code) {
|
|
|
- Stock quote = repository.findByCode(code).stream().findFirst().orElse(new Stock(code, null, null, null, null, now()));
|
|
|
- quote.setPrice(getLastPrice(quote));
|
|
|
-
|
|
|
- if (quote.getName() == null || quote.getPrice() == null) {
|
|
|
- StockInfoResponse info = updateStockInformation(quote.getCode());
|
|
|
- quote.setName(info.getName());
|
|
|
-
|
|
|
- repository.save(quote);
|
|
|
- }
|
|
|
-
|
|
|
- return quote;
|
|
|
- }
|
|
|
-
|
|
|
- private StockInfoResponse updateStockInformation(String code) {
|
|
|
- return client.getStockInfo(code, key);
|
|
|
- }
|
|
|
-
|
|
|
- private BigDecimal getLastPrice(Stock quote) {
|
|
|
- if (quote.getPrice() == null && "USD".equals(quote.getCurrency())) {
|
|
|
- log.info("Current quote for [{}] is null. Requesting latest quote...", quote);
|
|
|
- return new BigDecimal(client.getStockQuote(quote.getCode(), key).get(0).getLastPrice());
|
|
|
- } else if (quote.getUpdatedAt().isBefore(now().minus(5, DAYS))) {
|
|
|
- log.info("Current quote for [{}] is older than 1 day. Requesting latest quote...", quote);
|
|
|
- return new BigDecimal(client.getStockQuote(quote.getCode(), key).get(0).getLastPrice());
|
|
|
- } else {
|
|
|
- return quote.getPrice();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- @AllArgsConstructor
|
|
|
- @NoArgsConstructor
|
|
|
- @JsonIgnoreProperties(ignoreUnknown = true)
|
|
|
- public static class StockQuoteResponse {
|
|
|
- @JsonProperty("adjClose")
|
|
|
- private String lastPrice;
|
|
|
- @JsonProperty("adjOpen")
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- @Data
|
|
|
- @AllArgsConstructor
|
|
|
- @NoArgsConstructor
|
|
|
- @JsonIgnoreProperties(ignoreUnknown = true)
|
|
|
- public static class StockMetadataResponse {
|
|
|
- private String ticker;
|
|
|
- private String name;
|
|
|
- private String sector;
|
|
|
- private String industry;
|
|
|
- private String location;
|
|
|
- private String companyWebsite;
|
|
|
- @JsonProperty("reportingCurrency")
|
|
|
- private String currency;
|
|
|
- @JsonProperty("secFilingWebsite")
|
|
|
- private String secWebsite;
|
|
|
+ return repository.findByCode(code).stream().findFirst()
|
|
|
+ .orElse(new Stock(code, null, null, null, null, now()));
|
|
|
}
|
|
|
|
|
|
}
|