Эх сурвалжийг харах

add get all stock endpoint

Daniel Bohry 8 сар өмнө
parent
commit
280dc5c36b

+ 3 - 4
src/main/java/com/danielbohry/stocks/api/stock/StockController.java

@@ -30,11 +30,10 @@ public class StockController {
 
     @GetMapping
     public ResponseEntity<List<Quote>> find(@RequestParam(value = "q", required = false) String query) {
-        if (query == null) {
-            return ResponseEntity.ok(emptyList());
-        }
+        List<Quote> response = (query == null || query.isBlank())
+            ? service.getAll()
+            : service.get(query);
 
-        List<Quote> response = service.get(query);
         return ResponseEntity.ok(response);
     }
 

+ 14 - 4
src/main/java/com/danielbohry/stocks/config/CacheConfig.java

@@ -3,11 +3,15 @@ package com.danielbohry.stocks.config;
 import com.github.benmanes.caffeine.cache.Caffeine;
 import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.EnableCaching;
-import org.springframework.cache.caffeine.CaffeineCacheManager;
+import org.springframework.cache.caffeine.CaffeineCache;
+import org.springframework.cache.support.SimpleCacheManager;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import java.util.List;
+
 import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.MINUTES;
 
 @Configuration
 @EnableCaching
@@ -15,9 +19,15 @@ public class CacheConfig {
 
     @Bean
     public CacheManager cacheManager() {
-        CaffeineCacheManager cacheManager = new CaffeineCacheManager("exchangeRates");
-        cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(6, HOURS));
-        return cacheManager;
+        CaffeineCache exchangeRates = new CaffeineCache("exchangeRates",
+            Caffeine.newBuilder().expireAfterWrite(6, HOURS).build());
+
+        CaffeineCache stockQuotes = new CaffeineCache("stockQuotes",
+            Caffeine.newBuilder().expireAfterWrite(10, MINUTES).build());
+
+        SimpleCacheManager manager = new SimpleCacheManager();
+        manager.setCaches(List.of(exchangeRates, stockQuotes));
+        return manager;
     }
 
 }

+ 2 - 0
src/main/java/com/danielbohry/stocks/service/StockService.java

@@ -4,6 +4,7 @@ import com.danielbohry.stocks.domain.Quote;
 import com.danielbohry.stocks.repository.StockRepository;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
@@ -20,6 +21,7 @@ public class StockService {
 
     private StockRepository repository;
 
+    @Cacheable("stockQuotes")
     public List<Quote> getAll() {
         return repository.findAll();
     }