|
|
@@ -1,6 +1,7 @@
|
|
|
package com.danielbohry.stocks.service.stock;
|
|
|
|
|
|
import com.danielbohry.stocks.domain.StockHistory;
|
|
|
+import com.danielbohry.stocks.exception.BadRequestException;
|
|
|
import com.danielbohry.stocks.repository.stock.StockHistoryRepository;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
|
|
|
@@ -8,9 +9,12 @@ import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.Instant;
|
|
|
import java.util.List;
|
|
|
|
|
|
import static java.time.Instant.now;
|
|
|
+import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
+import static java.time.temporal.ChronoUnit.MONTHS;
|
|
|
|
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
|
@@ -31,9 +35,18 @@ public class StockHistoryService {
|
|
|
repository.saveAll(stocks);
|
|
|
}
|
|
|
|
|
|
- @Cacheable(value = "stockHistory", key = "#code")
|
|
|
- public List<StockHistory> get(String code) {
|
|
|
- return repository.findAllByCode(code);
|
|
|
+ @Cacheable(value = "stockHistory", key = "#code + '-' + #range")
|
|
|
+ public List<StockHistory> get(String code, String range) {
|
|
|
+ Instant end = Instant.now();
|
|
|
+ Instant start = switch (range) {
|
|
|
+ case "5d" -> end.minus(5, DAYS);
|
|
|
+ case "30d" -> end.minus(30, DAYS);
|
|
|
+ case "6m" -> end.minus(6, MONTHS);
|
|
|
+ case "1y" -> end.minus(12, MONTHS);
|
|
|
+ default -> throw new BadRequestException("Unsupported range: " + range);
|
|
|
+ };
|
|
|
+
|
|
|
+ return repository.findByCodeAndCreatedAtBetween(code, start, end);
|
|
|
}
|
|
|
|
|
|
}
|