|
|
@@ -1,7 +1,7 @@
|
|
|
package com.danielbohry.stocks.repository.stock;
|
|
|
|
|
|
import com.danielbohry.stocks.client.StockClient;
|
|
|
-import com.danielbohry.stocks.domain.Quote;
|
|
|
+import com.danielbohry.stocks.domain.Stock;
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
import feign.FeignException;
|
|
|
@@ -13,14 +13,14 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.time.Instant;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-import static java.time.LocalDateTime.now;
|
|
|
+import static java.time.Instant.now;
|
|
|
+import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -37,49 +37,49 @@ public class StockRepository {
|
|
|
this.key = key;
|
|
|
}
|
|
|
|
|
|
- public List<Quote> findAll() {
|
|
|
+ public List<Stock> findAll() {
|
|
|
return repository.findAll();
|
|
|
}
|
|
|
|
|
|
- public List<Quote> findLike(String query) {
|
|
|
+ public List<Stock> findLike(String query) {
|
|
|
return repository.findByNameOrCode(query);
|
|
|
}
|
|
|
|
|
|
- public Quote findByCode(String code) {
|
|
|
- Optional<Quote> quote = repository.findByCode(code)
|
|
|
+ public Stock findByCode(String code) {
|
|
|
+ Optional<Stock> quote = repository.findByCode(code)
|
|
|
.or(() -> repository.findByNameOrCode(code).stream().findFirst());
|
|
|
return quote.orElseGet(() -> repository.save(getStockQuote(code)));
|
|
|
}
|
|
|
|
|
|
- public List<Quote> update(List<Quote> quotes) {
|
|
|
- log.info("Updating [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
|
+ public List<Stock> update(List<Stock> quotes) {
|
|
|
+ log.info("Updating [{}] quotes [{}]", quotes.size(), now());
|
|
|
List<String> codes = quotes.stream()
|
|
|
- .map(Quote::getCode)
|
|
|
+ .map(Stock::getCode)
|
|
|
.collect(toList());
|
|
|
- Map<String, Quote> existingQuotesMap = repository.findByCodeIn(codes)
|
|
|
+ Map<String, Stock> existingQuotesMap = repository.findByCodeIn(codes)
|
|
|
.stream()
|
|
|
- .collect(Collectors.toMap(Quote::getCode, Function.identity()));
|
|
|
+ .collect(Collectors.toMap(Stock::getCode, Function.identity()));
|
|
|
|
|
|
- log.info("Found [{}] quotes by codes [{}]", existingQuotesMap.size(), Instant.now());
|
|
|
+ log.info("Found [{}] quotes by codes [{}]", existingQuotesMap.size(), now());
|
|
|
|
|
|
- List<Quote> updatedQuotes = quotes.stream()
|
|
|
+ List<Stock> updatedQuotes = quotes.stream()
|
|
|
.peek(quote -> {
|
|
|
- Quote existingQuote = existingQuotesMap.get(quote.getCode());
|
|
|
+ Stock existingQuote = existingQuotesMap.get(quote.getCode());
|
|
|
if (existingQuote != null) {
|
|
|
quote.setId(existingQuote.getId());
|
|
|
}
|
|
|
})
|
|
|
.toList();
|
|
|
|
|
|
- log.info("Updating [{}] quotes [{}]", updatedQuotes.size(), Instant.now());
|
|
|
- List<Quote> result = repository.saveAll(updatedQuotes);
|
|
|
- log.info("Update complete for [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
|
+ log.info("Updating [{}] quotes [{}]", updatedQuotes.size(), now());
|
|
|
+ List<Stock> result = repository.saveAll(updatedQuotes);
|
|
|
+ log.info("Update complete for [{}] quotes [{}]", quotes.size(), now());
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public boolean isValid(String code) {
|
|
|
- Quote quote = repository.findByCode(code).stream().findFirst().orElse(null);
|
|
|
+ Stock quote = repository.findByCode(code).stream().findFirst().orElse(null);
|
|
|
|
|
|
if (quote != null) return true;
|
|
|
|
|
|
@@ -92,8 +92,8 @@ public class StockRepository {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Quote getStockQuote(String code) {
|
|
|
- Quote quote = repository.findByCode(code).stream().findFirst().orElse(new Quote(code, null, null, null, null, now()));
|
|
|
+ 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) {
|
|
|
@@ -110,11 +110,11 @@ public class StockRepository {
|
|
|
return client.getStockInfo(code, key);
|
|
|
}
|
|
|
|
|
|
- private BigDecimal getLastPrice(Quote quote) {
|
|
|
+ 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().minusDays(5))) {
|
|
|
+ } 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 {
|