package cryptosky.me.graphql.service; import cryptosky.me.graphql.jpa.entity.BtcPriceModel; import cryptosky.me.graphql.jpa.repository.BtcPriceRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import static cryptosky.me.helpers.Utils.distinctByKey; @Service public class BtcPriceService { private final BtcPriceRepository btcPriceRepository; DateTimeFormatter format = DateTimeFormatter.ISO_DATE_TIME;//ofPattern("dd-MM-yy HH:mm"); public BtcPriceService(final BtcPriceRepository btcPriceRepository) { this.btcPriceRepository = btcPriceRepository; } @Transactional public BtcPriceModel createBtc(final String createdDate, final String type, final float av_price, final float h_price, final float l_price, final float c_price, final float volume ) { final BtcPriceModel btcPrice = new BtcPriceModel(); btcPrice.setTimestamp(LocalDate.parse(createdDate, format).toString()); btcPrice.setType(type); btcPrice.setAverage_price(av_price); btcPrice.setHigh_price(h_price); btcPrice.setLow_price(l_price); btcPrice.setClose_price(c_price); btcPrice.setVolume(volume); return this.btcPriceRepository.save(btcPrice); } @Transactional(readOnly = true) public List getAllPrices( final int count ) { return this.btcPriceRepository.findAll().stream() .limit(count) .collect(Collectors.toList()); } @Transactional(readOnly = true) public List getPricesBetweenCounts( final int startCount, final int endCount ) { return this.btcPriceRepository.findAll().stream() .skip(startCount) .limit(endCount - startCount) .collect(Collectors.toList()); } @Transactional(readOnly = true) public Optional getLatest() { return this.btcPriceRepository.findAll().stream().findFirst(); } @Transactional(readOnly = true) public Optional getPriceForCreatedDate( final String createdDate ) { return this.btcPriceRepository.findAll().stream() .filter(createdDateList -> createdDateList.getTimestamp().equals(LocalDate.parse(createdDate, format))) .findFirst(); } @Transactional(readOnly = true) public List getPriceBetweenDates( final String startDate, final String endDate ) { return this.btcPriceRepository.findAll().stream() .filter(createdDateList -> LocalDate.parse(createdDateList.getTimestamp(), format).isBefore(LocalDate.parse(endDate, format))) .filter(createdDateList -> LocalDate.parse(createdDateList.getTimestamp(), format).isAfter(LocalDate.parse(startDate, format))) .collect(Collectors.toList()); } // @Transactional(readOnly = true) // public List getAllLatest() { // return this.btcPriceRepository.findAll().stream() // .filter(distinctByKey(CryptoPriceModel::getType)) // .collect(Collectors.toList()); // } // @Transactional(readOnly = true) // public List getLimitPricesByType(final int count, final String type ) { // return this.btcPriceRepository.findAll().stream() // .filter(typeList -> typeList.getType().equals(type)) // .limit(count) // .collect(Collectors.toList()); // } // @Transactional(readOnly = true) // public List getAllByType(final String type ) { // return this.btcPriceRepository.findAll().stream() // .filter(typeList -> typeList.getType().equals(type)) // .collect(Collectors.toList()); // } // @Transactional(readOnly = true) // public Optional getLatestByType(final String type ) { // return this.cryptoPriceRepository.findAll().stream() // .filter(typeList -> typeList.getType().equals(type)) // .limit(1) // .findFirst(); // } // @Transactional(readOnly = true) // public Optional getPriceByCreatedDateForType( final String type, final String createdDate ) { // return this.cryptoPriceRepository.findAll().stream() // .filter(typeList -> typeList.getType().equals(type)) // .filter(createdDateList -> createdDateList.getTimestamp().equals(LocalDate.parse(createdDate))) // .findFirst(); // } // // @Transactional(readOnly = true) // public Optional getPriceBetweenCreatedDatesForType( final String type, final String startDate, final String endDate ) { // return this.cryptoPriceRepository.findAll().stream() // .filter(typeList -> typeList.getType().equals(type)) // .filter(createdDateList -> createdDateList.getTimestamp().isBefore(LocalDate.parse(endDate))) // .filter(createdDateList -> createdDateList.getTimestamp().isAfter(LocalDate.parse(startDate))) // .findFirst(); // } }