Ver código fonte

import marketcap

Daniel Bohry 8 meses atrás
pai
commit
815576c4ad

+ 20 - 8
src/main/java/com/danielbohry/stocks/api/stock/StockController.java

@@ -86,20 +86,32 @@ public class StockController {
 
     private Quote convert(String input, String currency) {
         String[] value = input.split(",");
-
         log.info("Importing [{}]", (Object) value);
 
-        if (value[3] != null && !value[3].equals("#N/A")) {
-            BigDecimal price = new BigDecimal(value[3]);
-            return price.compareTo(BigDecimal.ZERO) > 0
-                ? new Quote(value[0], value[1], currency, price, now())
-                : null;
-        } else if (value[0] != null && Objects.equals(value[3], "#N/A")) {
+        if (value.length < 5 || value[3] == null || value[3].equals("#N/A")) {
+            return null;
+        }
+
+        BigDecimal price;
+        try {
+            price = new BigDecimal(value[3]);
+        } catch (NumberFormatException e) {
             return null;
-        } else {
+        }
+
+        if (price.compareTo(BigDecimal.ZERO) <= 0) {
             return null;
         }
 
+        BigDecimal marketCap = null;
+        if (value[4] != null && !value[4].equals("#N/A")) {
+            try {
+                marketCap = new BigDecimal(value[4]);
+            } catch (NumberFormatException ignored) {
+            }
+        }
+
+        return new Quote(value[0], value[1], currency, price, marketCap, now());
     }
 
 }

+ 3 - 1
src/main/java/com/danielbohry/stocks/domain/Quote.java

@@ -19,13 +19,15 @@ public class Quote {
     private String name;
     private String currency;
     private BigDecimal price;
+    private BigDecimal marketCap;
     private LocalDateTime updatedAt;
 
-    public Quote(String code, String name, String currency, BigDecimal price, LocalDateTime updatedAt) {
+    public Quote(String code, String name, String currency, BigDecimal price, BigDecimal marketCap, LocalDateTime updatedAt) {
         this.code = code;
         this.name = name;
         this.currency = currency;
         this.price = price;
+        this.marketCap = marketCap;
         this.updatedAt = updatedAt;
     }
 }

+ 15 - 15
src/main/java/com/danielbohry/stocks/repository/StockRepository.java

@@ -14,7 +14,9 @@ import org.springframework.stereotype.Repository;
 
 import java.math.BigDecimal;
 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.stream.Collectors;
 
@@ -52,23 +54,22 @@ public class StockRepository {
     public List<Quote> update(List<Quote> quotes) {
         log.info("Updating [{}] quotes [{}]", quotes.size(), Instant.now());
         List<String> codes = quotes.stream()
-                .map(Quote::getCode)
-                .collect(toList());
+            .map(Quote::getCode)
+            .collect(toList());
         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());
 
         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());
         List<Quote> result = repository.saveAll(updatedQuotes);
@@ -92,7 +93,7 @@ public class StockRepository {
     }
 
     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));
 
         if (quote.getName() == null || quote.getPrice() == null) {
@@ -106,7 +107,6 @@ public class StockRepository {
     }
 
     private StockInfoResponse updateStockInformation(String code) {
-        log.info("Current stock's name is null. Requesting latest information...");
         return client.getStockInfo(code, key);
     }