[03.02.20] More dynamic code and set up for BTC
This commit is contained in:
parent
51b154e6d4
commit
2406a79b66
5
pom.xml
5
pom.xml
@ -57,6 +57,11 @@
|
||||
<artifactId>graphiql-spring-boot-starter</artifactId>
|
||||
<version>5.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-extended-scalars</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Local database -->
|
||||
<dependency>
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
package cryptosky.me.graphql.jpa.entity;
|
||||
|
||||
import lombok.*;
|
||||
import javax.persistence.*;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@Data
|
||||
@Table(name="btc_usd")
|
||||
public class BtcPrice extends CryptoPrice {
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package cryptosky.me.graphql.jpa.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "btc_price")
|
||||
public class BtcPriceModel extends CryptoPriceModel {
|
||||
|
||||
}
|
||||
@ -7,24 +7,24 @@ import java.time.LocalDate;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
@Entity
|
||||
@Table(name = "crypto_price")
|
||||
public class CryptoPrice {
|
||||
@MappedSuperclass
|
||||
public class CryptoPriceModel {
|
||||
|
||||
@Id
|
||||
@Column(name = "ID", nullable = false)
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@GeneratedValue(strategy = GenerationType.TABLE)
|
||||
private int id;
|
||||
|
||||
@NonNull
|
||||
@Column(name = "timestamp", nullable = false)
|
||||
private LocalDate timestamp;
|
||||
private String timestamp;
|
||||
|
||||
@NonNull
|
||||
@Column(name = "symbol", nullable = false)
|
||||
private String type;
|
||||
|
||||
@NonNull
|
||||
@Column(name = "av_price", nullable = false)
|
||||
private float average_price;
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package cryptosky.me.graphql.jpa.repository;
|
||||
|
||||
import cryptosky.me.graphql.jpa.entity.CryptoPrice;
|
||||
import cryptosky.me.graphql.jpa.entity.BtcPriceModel;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CryptoPriceRepository extends JpaRepository<CryptoPrice, Integer> {
|
||||
public interface BtcPriceRepository extends JpaRepository<BtcPriceModel, Integer> {
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package cryptosky.me.graphql.mutation;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import cryptosky.me.graphql.jpa.entity.BtcPriceModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cryptosky.me.graphql.service.BtcPriceService;
|
||||
|
||||
@Component
|
||||
public class BtcPriceMutation implements GraphQLMutationResolver {
|
||||
|
||||
@Autowired
|
||||
private BtcPriceService btcPriceService;
|
||||
|
||||
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 ) {
|
||||
return this.btcPriceService.createBtc(createdDate, type, av_price, h_price, l_price, c_price, volume);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package cryptosky.me.graphql.mutation;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import cryptosky.me.graphql.jpa.entity.BtcPrice;
|
||||
import cryptosky.me.graphql.jpa.entity.CryptoPrice;
|
||||
import cryptosky.me.graphql.service.CryptoPriceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CryptoPriceMutation implements GraphQLMutationResolver {
|
||||
|
||||
@Autowired
|
||||
private CryptoPriceService cryptoPriceService;
|
||||
|
||||
// public CryptoPrice createPrice(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 ) {
|
||||
// return this.cryptoPriceService.createPrice(createdDate, type, av_price, h_price, l_price, c_price, volume);
|
||||
// }
|
||||
|
||||
public BtcPrice 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 ) {
|
||||
return this.cryptoPriceService.createBtc(createdDate, type, av_price, h_price, l_price, c_price, volume);
|
||||
}
|
||||
}
|
||||
39
src/main/java/cryptosky/me/graphql/query/BtcPriceQuery.java
Normal file
39
src/main/java/cryptosky/me/graphql/query/BtcPriceQuery.java
Normal file
@ -0,0 +1,39 @@
|
||||
package cryptosky.me.graphql.query;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
import cryptosky.me.graphql.jpa.entity.BtcPriceModel;
|
||||
import cryptosky.me.graphql.jpa.entity.CryptoPriceModel;
|
||||
import cryptosky.me.graphql.service.BtcPriceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class BtcPriceQuery implements GraphQLQueryResolver {
|
||||
|
||||
@Autowired
|
||||
private BtcPriceService btcPriceService;
|
||||
|
||||
public List<BtcPriceModel> getAllPrices( final int count ) {
|
||||
return this.btcPriceService.getAllPrices(count);
|
||||
}
|
||||
|
||||
public List<BtcPriceModel> getPricesBetweenCounts( final int startCount, final int endCount ) {
|
||||
return this.btcPriceService.getPricesBetweenCounts(startCount, endCount);
|
||||
}
|
||||
|
||||
public Optional<BtcPriceModel> getLatest() {
|
||||
return this.btcPriceService.getLatest();
|
||||
}
|
||||
|
||||
public Optional<BtcPriceModel> getPriceForCreatedDate( final String createdDate ) {
|
||||
return this.btcPriceService.getPriceForCreatedDate(createdDate);
|
||||
}
|
||||
|
||||
public List<BtcPriceModel> getPriceBetweenDates(final String startDate, final String endDate ) {
|
||||
return this.btcPriceService.getPriceBetweenDates(startDate, endDate);
|
||||
}
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
package cryptosky.me.graphql.query;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
import cryptosky.me.graphql.jpa.entity.CryptoPrice;
|
||||
import cryptosky.me.graphql.service.CryptoPriceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class CryptoPriceQuery implements GraphQLQueryResolver {
|
||||
|
||||
@Autowired
|
||||
private CryptoPriceService cryptoPriceService;
|
||||
|
||||
public List<CryptoPrice> getAllPrices(final int count) {
|
||||
return this.cryptoPriceService.getAllPrices(count);
|
||||
}
|
||||
|
||||
public List<CryptoPrice> getLimitPricesByType( final int count, final String type ) {
|
||||
return this.cryptoPriceService.getLimitPricesByType(count, type);
|
||||
}
|
||||
|
||||
public List<CryptoPrice> getAllByType( final String type ) {
|
||||
return this.cryptoPriceService.getAllByType(type);
|
||||
}
|
||||
|
||||
public Optional<CryptoPrice> getLatestByType( final String type ) {
|
||||
return this.cryptoPriceService.getLatestByType(type);
|
||||
}
|
||||
|
||||
public List<CryptoPrice> getAllLatest() {
|
||||
return this.cryptoPriceService.getAllLatest();
|
||||
}
|
||||
|
||||
public Optional<CryptoPrice> getPriceByCreatedDateForType( final String type, final String createdDate ) {
|
||||
return this.cryptoPriceService.getPriceByCreatedDateForType(type, createdDate);
|
||||
}
|
||||
|
||||
public Optional<CryptoPrice> getPriceBetweenCreatedDatesForType( final String type, final String startDate, final String endDate ) {
|
||||
return this.cryptoPriceService.getPriceBetweenCreatedDatesForType(type, startDate, endDate);
|
||||
}
|
||||
|
||||
}
|
||||
125
src/main/java/cryptosky/me/graphql/service/BtcPriceService.java
Normal file
125
src/main/java/cryptosky/me/graphql/service/BtcPriceService.java
Normal file
@ -0,0 +1,125 @@
|
||||
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<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(LocalDate.parse(createdDate, format)))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BtcPriceModel> 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<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();
|
||||
// }
|
||||
}
|
||||
@ -1,114 +0,0 @@
|
||||
package cryptosky.me.graphql.service;
|
||||
|
||||
import cryptosky.me.graphql.jpa.entity.BtcPrice;
|
||||
import cryptosky.me.graphql.jpa.entity.CryptoPrice;
|
||||
import cryptosky.me.graphql.jpa.repository.CryptoPriceRepository;
|
||||
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.functionalMapping.distinctByKey;
|
||||
|
||||
@Service
|
||||
public class CryptoPriceService {
|
||||
|
||||
private final CryptoPriceRepository cryptoPriceRepository;
|
||||
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yy");
|
||||
|
||||
public CryptoPriceService( final CryptoPriceRepository cryptoPriceRepository ) {
|
||||
this.cryptoPriceRepository = cryptoPriceRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BtcPrice 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 BtcPrice btcPrice = new BtcPrice();
|
||||
btcPrice.setTimestamp(LocalDate.parse(createdDate, format));
|
||||
btcPrice.setAverage_price(av_price);
|
||||
btcPrice.setType(type);
|
||||
btcPrice.setHigh_price(h_price);
|
||||
btcPrice.setLow_price(l_price);
|
||||
btcPrice.setClose_price(c_price);
|
||||
btcPrice.setVolume(volume);
|
||||
|
||||
return this.cryptoPriceRepository.save(btcPrice);
|
||||
}
|
||||
|
||||
// @Transactional
|
||||
// public CryptoPrice createPrice(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 CryptoPrice cryptoPrice = CryptoPrice.builder()
|
||||
// .timestamp(LocalDate.parse(createdDate, format))
|
||||
// .type(type)
|
||||
// .average_price(av_price)
|
||||
// .high_price(h_price)
|
||||
// .low_price(l_price)
|
||||
// .close_price(c_price)
|
||||
// .volume(volume)
|
||||
// .build();
|
||||
//
|
||||
// return this.cryptoPriceRepository.save(cryptoPrice);
|
||||
// }
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getAllPrices( final int count ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.limit(count)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getLimitPricesByType( final int count, final String type ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(typeList -> typeList.getType().equals(type))
|
||||
.limit(count)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getAllByType( final String type ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(typeList -> typeList.getType().equals(type))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<CryptoPrice> getLatestByType( final String type ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(typeList -> typeList.getType().equals(type))
|
||||
.limit(1)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getAllLatest() {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(distinctByKey(CryptoPrice::getType))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<CryptoPrice> 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<CryptoPrice> 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();
|
||||
}
|
||||
}
|
||||
@ -5,8 +5,9 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class functionalMapping {
|
||||
public class Utils {
|
||||
|
||||
// Utility Function to be able to get one of all types
|
||||
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
|
||||
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
|
||||
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
|
||||
@ -11,7 +11,10 @@ type BtcPrice {
|
||||
|
||||
type Query {
|
||||
allPrices(count: Int):[BtcPrice],
|
||||
allLatest:[BtcPrice]
|
||||
pricesBetweenCounts(startCount: Int, endCount: Int):[BtcPrice]
|
||||
latest:BtcPrice,
|
||||
priceForCreatedDate(createdDate: String):BtcPrice,
|
||||
priceBetweenDates(startDate: String, endDate: String):[BtcPrice]
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
Loading…
x
Reference in New Issue
Block a user