Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Feb 29, 2024
1 parent caa7b1a commit 7182267
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
@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;

@Override
@Cacheable("salesAndTrafficByDatesBetween")
public List<SalesAndTrafficByDateDto> 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)))
Expand All @@ -36,7 +37,7 @@ public List<SalesAndTrafficByDateDto> 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))
Expand All @@ -50,7 +51,7 @@ public SalesAndTrafficByDateDto findBySpecifiedDate(LocalDate localDate) {
@Override
@Cacheable("salesAndTrafficBySpecifiedAsins")
public List<SalesAndTrafficByAsinDto> findAllBySpecifiedAsins(List<String> asins) {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
SalesAndTrafficReport salesAndTrafficReport = findReport();
return salesAndTrafficReport.getSalesAndTrafficByAsin().stream()
.filter(s -> asins.contains(s.getParentAsin()))
.map(salesAndTrafficByAsinMapper::toDto)
Expand All @@ -60,7 +61,7 @@ public List<SalesAndTrafficByAsinDto> findAllBySpecifiedAsins(List<String> asins
@Override
@Cacheable("salesAndTrafficByAsins")
public List<SalesAndTrafficByAsinDto> findAllByAsins() {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
SalesAndTrafficReport salesAndTrafficReport = findReport();
return salesAndTrafficReport.getSalesAndTrafficByAsin().stream()
.map(salesAndTrafficByAsinMapper::toDto)
.toList();
Expand All @@ -69,7 +70,7 @@ public List<SalesAndTrafficByAsinDto> findAllByAsins() {
@Override
@Cacheable("salesAndTrafficByDates")
public List<SalesAndTrafficByDateDto> findAllByDates() {
SalesAndTrafficReport salesAndTrafficReport = repository.findAll().get(0);
SalesAndTrafficReport salesAndTrafficReport = findReport();
return salesAndTrafficReport.getSalesAndTrafficByDate().stream()
.map(salesAndTrafficByDateMapper::toDto)
.toList();
Expand All @@ -88,4 +89,8 @@ public List<SalesAndTrafficByDateDto> findAllByDates() {
@Scheduled(fixedRateString = "${caching.spring.TTL}")
public void emptyHotelsCache() {
}

private SalesAndTrafficReport findReport() {
return repository.findAll().get(FIRST_ELEMENT_INDEX);
}
}

0 comments on commit 7182267

Please sign in to comment.