133 lines
5.5 KiB
Java
133 lines
5.5 KiB
Java
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 a_price,
|
|
final float b_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.setAsk_price(a_price);
|
|
btcPrice.setBid_price(b_price);
|
|
btcPrice.setOpen_price(o_price);
|
|
btcPrice.setClose_price(c_price);
|
|
btcPrice.setVolume(volume);
|
|
|
|
return this.btcPriceRepository.save(btcPrice);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public List<BtcPriceModel> getAllPrices( final int count ) {
|
|
return this.btcPriceRepository.findAll().stream()
|
|
.limit(count)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public List<BtcPriceModel> 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<BtcPriceModel> getLatest() {
|
|
return this.btcPriceRepository.findAll().stream().findFirst();
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public Optional<BtcPriceModel> getPriceForCreatedDate( final String createdDate ) {
|
|
return this.btcPriceRepository.findAll().stream()
|
|
.filter(createdDateList -> createdDateList.getTimestamp().equals(format(createdDate)))
|
|
.findFirst();
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public List<BtcPriceModel> 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<BtcPriceModel> getAllLatest() {
|
|
// return this.btcPriceRepository.findAll().stream()
|
|
// .filter(distinctByKey(CryptoPriceModel::getType))
|
|
// .collect(Collectors.toList());
|
|
// }
|
|
|
|
// @Transactional(readOnly = true)
|
|
// public List<CryptoPriceModel> 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<CryptoPriceModel> getAllByType(final String type ) {
|
|
// return this.btcPriceRepository.findAll().stream()
|
|
// .filter(typeList -> typeList.getType().equals(type))
|
|
// .collect(Collectors.toList());
|
|
// }
|
|
|
|
// @Transactional(readOnly = true)
|
|
// public Optional<CryptoPriceModel> getLatestByType(final String type ) {
|
|
// return this.cryptoPriceRepository.findAll().stream()
|
|
// .filter(typeList -> typeList.getType().equals(type))
|
|
// .limit(1)
|
|
// .findFirst();
|
|
// }
|
|
|
|
// @Transactional(readOnly = true)
|
|
// public Optional<CryptoPriceModel> 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<CryptoPriceModel> 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();
|
|
// }
|
|
}
|