Daniel Bohry 1 жил өмнө
parent
commit
262aa05a92

+ 8 - 3
src/main/java/com/danielbohry/stocks/client/StockClient.java

@@ -1,8 +1,10 @@
 package com.danielbohry.stocks.client;
 
-import com.danielbohry.stocks.service.StockService.StockResponse;
+import com.danielbohry.stocks.service.StockService.StockInfoResponse;
+import com.danielbohry.stocks.service.StockService.StockQuoteResponse;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.List;
@@ -11,8 +13,11 @@ import java.util.List;
 public interface StockClient {
 
     @GetMapping("daily/{symbol}/prices")
-    List<StockResponse> getStockInfo(@RequestParam("symbol") String symbol,
-                                     @RequestParam("token") String apiKey);
+    List<StockQuoteResponse> getStockQuote(@PathVariable("symbol") String symbol,
+                                           @RequestParam("token") String apiKey);
 
+    @GetMapping("daily/{symbol}")
+    StockInfoResponse getStockInfo(@PathVariable("symbol") String symbol,
+                                   @RequestParam("token") String apiKey);
 
 }

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

@@ -15,6 +15,7 @@ public class Quote {
 
     @Id
     private String code;
+    private String name;
     private BigDecimal price;
     private LocalDateTime updatedAt;
 

+ 1 - 1
src/main/java/com/danielbohry/stocks/service/PortfolioService.java

@@ -34,7 +34,7 @@ public class PortfolioService {
         List<Stock> updatedStocks = portfolio.getStocks().stream()
                 .peek(stock -> {
                     Quote quote = stockService.getStockQuote(stock.getCode());
-                    stock.setName(quote.getCode());
+                    stock.setName(quote.getName());
                     stock.setPrice(quote.getPrice());
                     stock.setTotal(stock.getPrice().multiply(new BigDecimal(stock.getQuantity())));
                 }).toList();

+ 28 - 4
src/main/java/com/danielbohry/stocks/service/StockService.java

@@ -33,22 +33,33 @@ public class StockService {
     }
 
     public Quote getStockQuote(String code) {
-        Quote quote = repository.findByCode(code).orElse(new Quote(code, null, null));
+        Quote quote = repository.findByCode(code).orElse(new Quote(code, null, null, null));
         quote.setPrice(getLastPrice(quote));
         quote.setUpdatedAt(now());
 
+        if (quote.getName() == null) {
+            quote = updateStockInformation(quote);
+        }
+
         repository.save(quote);
 
         return quote;
     }
 
+    public Quote updateStockInformation(Quote quote) {
+        log.info("Current stock's name is null. Requesting latest information...");
+        StockInfoResponse info = client.getStockInfo(quote.getCode(), key);
+        quote.setName(info.getName());
+        return quote;
+    }
+
     private BigDecimal getLastPrice(Quote quote) {
         if (quote.getPrice() == null) {
             log.info("Current quote for [{}] is null. Requesting latest quote...", quote);
-            return new BigDecimal(client.getStockInfo(quote.getCode(), key).get(0).getLastPrice());
+            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.getStockInfo(quote.getCode(), key).get(0).getLastPrice());
+            return new BigDecimal(client.getStockQuote(quote.getCode(), key).get(0).getLastPrice());
         } else {
             return quote.getPrice();
         }
@@ -58,11 +69,24 @@ public class StockService {
     @AllArgsConstructor
     @NoArgsConstructor
     @JsonIgnoreProperties(ignoreUnknown = true)
-    public static class StockResponse {
+    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;
+    }
+
 }