|
@@ -1,105 +1,24 @@
|
|
|
package com.danielbohry.stocks.service;
|
|
package com.danielbohry.stocks.service;
|
|
|
|
|
|
|
|
-import com.danielbohry.stocks.client.StockClient;
|
|
|
|
|
import com.danielbohry.stocks.domain.Quote;
|
|
import com.danielbohry.stocks.domain.Quote;
|
|
|
-import com.danielbohry.stocks.repository.QuoteRepository;
|
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
|
|
-import feign.FeignException;
|
|
|
|
|
|
|
+import com.danielbohry.stocks.repository.StockRepository;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
-import lombok.Data;
|
|
|
|
|
-import lombok.NoArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
|
|
-
|
|
|
|
|
-import static java.time.LocalDateTime.now;
|
|
|
|
|
-
|
|
|
|
|
-@Service
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@AllArgsConstructor
|
|
|
public class StockService {
|
|
public class StockService {
|
|
|
|
|
|
|
|
- private final QuoteRepository repository;
|
|
|
|
|
- private final StockClient client;
|
|
|
|
|
- private final String key;
|
|
|
|
|
-
|
|
|
|
|
- public StockService(QuoteRepository repository,
|
|
|
|
|
- StockClient client,
|
|
|
|
|
- @Value("${clients.stock.key}") String key) {
|
|
|
|
|
- this.repository = repository;
|
|
|
|
|
- this.client = client;
|
|
|
|
|
- this.key = key;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private StockRepository repository;
|
|
|
|
|
|
|
|
public boolean isValid(String code) {
|
|
public boolean isValid(String code) {
|
|
|
- Quote quote = repository.findByCode(code).orElse(null);
|
|
|
|
|
-
|
|
|
|
|
- if (quote != null) return true;
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- client.getStockInfo(code, key);
|
|
|
|
|
- return true;
|
|
|
|
|
- } catch (FeignException.NotFound e) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return repository.isValid(code);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Quote getStockQuote(String code) {
|
|
public Quote getStockQuote(String code) {
|
|
|
- Quote quote = repository.findByCode(code).orElse(new Quote(code, null, null, null));
|
|
|
|
|
- quote.setPrice(getLastPrice(quote));
|
|
|
|
|
- quote.setUpdatedAt(now());
|
|
|
|
|
-
|
|
|
|
|
- if (quote.getName() == null) {
|
|
|
|
|
- StockInfoResponse info = updateStockInformation(quote.getCode());
|
|
|
|
|
- quote.setName(info.getName());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- repository.save(quote);
|
|
|
|
|
-
|
|
|
|
|
- return quote;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private StockInfoResponse updateStockInformation(String code) {
|
|
|
|
|
- log.info("Current stock's name is null. Requesting latest information...");
|
|
|
|
|
- return client.getStockInfo(code, key);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private BigDecimal getLastPrice(Quote quote) {
|
|
|
|
|
- if (quote.getPrice() == null) {
|
|
|
|
|
- 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().minusDays(1))) {
|
|
|
|
|
- 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;
|
|
|
|
|
|
|
+ return repository.getStockQuote(code);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|