package cryptosky.me.graphql.pricing.service; import cryptosky.me.graphql.pricing.models.entities.BtcPriceModel; import cryptosky.me.graphql.pricing.models.repositories.BtcPriceRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.time.LocalDateTime; import static cryptosky.me.helpers.Utils.format; @Service public class BtcPriceService { private final BtcPriceRepository btcPriceRepository; 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 o_price, final float c_price, final float volume ) { final BtcPriceModel btcPrice = new BtcPriceModel(); btcPrice.setTimestamp(LocalDateTime.parse((createdDate))); btcPrice.setType(type); btcPrice.setAverage_price(av_price); btcPrice.setHigh_price(h_price); btcPrice.setLow_price(l_price); btcPrice.setOpen_price(o_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(format(createdDate))) .findFirst(); } @Transactional(readOnly = true) public List getPriceBetweenDates( final String startDate, final String endDate ) { return this.btcPriceRepository.findAll().stream() .filter(createdDateList -> format(createdDateList.getTimestamp().toString()).isBefore(format(endDate))) .filter(createdDateList -> format(createdDateList.getTimestamp().toString()).isAfter(format(startDate))) .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(); // } }