|
|
@@ -0,0 +1,33 @@
|
|
|
+package com.danielbohry.stocks.service;
|
|
|
+
|
|
|
+import com.danielbohry.stocks.repository.stock.StockHistory;
|
|
|
+import com.danielbohry.stocks.repository.stock.StockHistoryRepository;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static java.time.Instant.now;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class StockQuoteService {
|
|
|
+
|
|
|
+ private final StockService service;
|
|
|
+ private final StockHistoryRepository repository;
|
|
|
+
|
|
|
+ public static final String ONCE_PER_DAY_AT_11PM = "0 0 23 * * *";
|
|
|
+
|
|
|
+ @Scheduled(cron = ONCE_PER_DAY_AT_11PM)
|
|
|
+ @SchedulerLock(name = "StockQuoteService_saveQuotes", lockAtLeastFor = "PT5M", lockAtMostFor = "PT15M")
|
|
|
+ public void saveQuotes() {
|
|
|
+ List<StockHistory> stocks = service.getAll().stream()
|
|
|
+ .map(stock -> new StockHistory(stock.getCode(), stock.getCurrency(), stock.getPrice(), now()))
|
|
|
+ .toList();
|
|
|
+
|
|
|
+ repository.saveAll(stocks);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|