diff --git a/src/main/java/com/app/sellerretailreports/entity/report/SalesAndTrafficReport.java b/src/main/java/com/app/sellerretailreports/entity/report/SalesAndTrafficReport.java index 64c156f..5adf559 100644 --- a/src/main/java/com/app/sellerretailreports/entity/report/SalesAndTrafficReport.java +++ b/src/main/java/com/app/sellerretailreports/entity/report/SalesAndTrafficReport.java @@ -6,7 +6,7 @@ import org.springframework.data.mongodb.core.mapping.Document; @Data -@Document(collection = "objects") +@Document(collection = "sales_and_traffic_reports") public class SalesAndTrafficReport { @Id private String id; diff --git a/src/main/java/com/app/sellerretailreports/service/sales/SalesAndTrafficByDateServiceImpl.java b/src/main/java/com/app/sellerretailreports/service/sales/SalesAndTrafficByDateServiceImpl.java index f8eceef..c782ec9 100644 --- a/src/main/java/com/app/sellerretailreports/service/sales/SalesAndTrafficByDateServiceImpl.java +++ b/src/main/java/com/app/sellerretailreports/service/sales/SalesAndTrafficByDateServiceImpl.java @@ -18,6 +18,7 @@ @Service @RequiredArgsConstructor public class SalesAndTrafficByDateServiceImpl implements SalesAndTrafficByDateService { + private static final int FIRST_ELEMENT_INDEX = 0; private final ReportRepository repository; private final SalesAndTrafficByDateMapper salesAndTrafficByDateMapper; private final SalesAndTrafficByAsinMapper salesAndTrafficByAsinMapper; @@ -25,7 +26,7 @@ public class SalesAndTrafficByDateServiceImpl implements SalesAndTrafficByDateSe @Override @Cacheable("salesAndTrafficByDatesBetween") public List findAllBetweenDates(LocalDate start, LocalDate end) { - SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0); + SalesAndTrafficReport salesAndTrafficReport = findReport(); return salesAndTrafficReport.getSalesAndTrafficByDate().stream() .filter(s -> (s.getDate().isAfter(start) && s.getDate().isBefore(end)) || (s.getDate().equals(start) || s.getDate().equals(end))) @@ -36,7 +37,7 @@ public List findAllBetweenDates(LocalDate start, Local @Override @Cacheable("salesAndTrafficBySpecifiedDate") public SalesAndTrafficByDateDto findBySpecifiedDate(LocalDate localDate) { - SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0); + SalesAndTrafficReport salesAndTrafficReport = findReport(); SalesAndTrafficByDate salesAndTrafficByDate = salesAndTrafficReport .getSalesAndTrafficByDate().stream() .filter(s -> s.getDate().isEqual(localDate)) @@ -50,7 +51,7 @@ public SalesAndTrafficByDateDto findBySpecifiedDate(LocalDate localDate) { @Override @Cacheable("salesAndTrafficBySpecifiedAsins") public List findAllBySpecifiedAsins(List asins) { - SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0); + SalesAndTrafficReport salesAndTrafficReport = findReport(); return salesAndTrafficReport.getSalesAndTrafficByAsin().stream() .filter(s -> asins.contains(s.getParentAsin())) .map(salesAndTrafficByAsinMapper::toDto) @@ -60,7 +61,7 @@ public List findAllBySpecifiedAsins(List asins @Override @Cacheable("salesAndTrafficByAsins") public List findAllByAsins() { - SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0); + SalesAndTrafficReport salesAndTrafficReport = findReport(); return salesAndTrafficReport.getSalesAndTrafficByAsin().stream() .map(salesAndTrafficByAsinMapper::toDto) .toList(); @@ -69,7 +70,7 @@ public List findAllByAsins() { @Override @Cacheable("salesAndTrafficByDates") public List findAllByDates() { - SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0); + SalesAndTrafficReport salesAndTrafficReport = findReport(); return salesAndTrafficReport.getSalesAndTrafficByDate().stream() .map(salesAndTrafficByDateMapper::toDto) .toList(); @@ -88,4 +89,8 @@ public List findAllByDates() { @Scheduled(fixedRateString = "${caching.spring.TTL}") public void emptyHotelsCache() { } + + private SalesAndTrafficReport findReport() { + return repository.findAll().get(FIRST_ELEMENT_INDEX); + } }