|
@@ -14,7 +14,9 @@ import org.springframework.stereotype.Repository;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
|
-import java.util.*;
|
|
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Optional;
|
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
@@ -52,23 +54,22 @@ public class StockRepository {
|
|
|
public List<Quote> update(List<Quote> quotes) {
|
|
public List<Quote> update(List<Quote> quotes) {
|
|
|
log.info("Updating [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
log.info("Updating [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
|
List<String> codes = quotes.stream()
|
|
List<String> codes = quotes.stream()
|
|
|
- .map(Quote::getCode)
|
|
|
|
|
- .collect(toList());
|
|
|
|
|
|
|
+ .map(Quote::getCode)
|
|
|
|
|
+ .collect(toList());
|
|
|
Map<String, Quote> existingQuotesMap = repository.findByCodeIn(codes)
|
|
Map<String, Quote> existingQuotesMap = repository.findByCodeIn(codes)
|
|
|
- .stream()
|
|
|
|
|
- .collect(Collectors.toMap(Quote::getCode, Function.identity()));
|
|
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .collect(Collectors.toMap(Quote::getCode, Function.identity()));
|
|
|
|
|
|
|
|
log.info("Found [{}] quotes by codes [{}]", existingQuotesMap.size(), Instant.now());
|
|
log.info("Found [{}] quotes by codes [{}]", existingQuotesMap.size(), Instant.now());
|
|
|
|
|
|
|
|
List<Quote> updatedQuotes = quotes.stream()
|
|
List<Quote> updatedQuotes = quotes.stream()
|
|
|
- .map(quote -> {
|
|
|
|
|
- Quote existingQuote = existingQuotesMap.get(quote.getCode());
|
|
|
|
|
- if (existingQuote != null) {
|
|
|
|
|
- quote.setId(existingQuote.getId());
|
|
|
|
|
- }
|
|
|
|
|
- return quote;
|
|
|
|
|
- })
|
|
|
|
|
- .collect(toList());
|
|
|
|
|
|
|
+ .peek(quote -> {
|
|
|
|
|
+ Quote existingQuote = existingQuotesMap.get(quote.getCode());
|
|
|
|
|
+ if (existingQuote != null) {
|
|
|
|
|
+ quote.setId(existingQuote.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .toList();
|
|
|
|
|
|
|
|
log.info("Updating [{}] quotes [{}]", updatedQuotes.size(), Instant.now());
|
|
log.info("Updating [{}] quotes [{}]", updatedQuotes.size(), Instant.now());
|
|
|
List<Quote> result = repository.saveAll(updatedQuotes);
|
|
List<Quote> result = repository.saveAll(updatedQuotes);
|
|
@@ -92,7 +93,7 @@ public class StockRepository {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Quote getStockQuote(String code) {
|
|
public Quote getStockQuote(String code) {
|
|
|
- Quote quote = repository.findByCode(code).stream().findFirst().orElse(new Quote(code, null, null, null, now()));
|
|
|
|
|
|
|
+ Quote quote = repository.findByCode(code).stream().findFirst().orElse(new Quote(code, null, null, null, null, now()));
|
|
|
quote.setPrice(getLastPrice(quote));
|
|
quote.setPrice(getLastPrice(quote));
|
|
|
|
|
|
|
|
if (quote.getName() == null || quote.getPrice() == null) {
|
|
if (quote.getName() == null || quote.getPrice() == null) {
|
|
@@ -106,7 +107,6 @@ public class StockRepository {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private StockInfoResponse updateStockInformation(String code) {
|
|
private StockInfoResponse updateStockInformation(String code) {
|
|
|
- log.info("Current stock's name is null. Requesting latest information...");
|
|
|
|
|
return client.getStockInfo(code, key);
|
|
return client.getStockInfo(code, key);
|
|
|
}
|
|
}
|
|
|
|
|
|