[01.02.20] Additional functions and mapping for endpoints
This commit is contained in:
parent
5afa063e75
commit
f57fa33522
@ -8,8 +8,6 @@ import java.time.LocalDate;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@ToString
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
@ -28,21 +26,21 @@ public class CryptoPrice {
|
||||
private String type;
|
||||
|
||||
@Column(name = "av_price", nullable = false)
|
||||
private Long average_price;
|
||||
private float average_price;
|
||||
|
||||
@Column(name = "h_price", nullable = false)
|
||||
private Long high_price;
|
||||
private float high_price;
|
||||
|
||||
@Column(name = "l_price", nullable = false)
|
||||
private Long low_price;
|
||||
private float low_price;
|
||||
|
||||
@Column(name = "c_price")
|
||||
private Long close_price;
|
||||
private float close_price;
|
||||
|
||||
@Column(name = "volume")
|
||||
private Long volume;
|
||||
private float volume;
|
||||
|
||||
private transient String formattedDate;
|
||||
private transient String formattedDate;
|
||||
|
||||
public String getFormattedDate() {
|
||||
return getTimestamp().toString();
|
||||
|
||||
@ -12,8 +12,8 @@ public class CryptoPriceMutation implements GraphQLMutationResolver {
|
||||
@Autowired
|
||||
private CryptoPriceService cryptoPriceService;
|
||||
|
||||
public CryptoPrice createPrice( final String createdDate, final String type, final Long av_price,
|
||||
final Long h_price, final Long l_price, final Long c_price, final Long volume ) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,13 +13,34 @@ import java.util.Optional;
|
||||
public class CryptoPriceQuery implements GraphQLQueryResolver {
|
||||
|
||||
@Autowired
|
||||
private CryptoPriceService vehicleService;
|
||||
private CryptoPriceService cryptoPriceService;
|
||||
|
||||
public List<CryptoPrice> getVehicles(final int count) {
|
||||
return this.vehicleService.getAllPrices(count);
|
||||
public List<CryptoPrice> getAllPrices(final int count) {
|
||||
return this.cryptoPriceService.getAllPrices(count);
|
||||
}
|
||||
|
||||
public Optional<CryptoPrice> getVehicle(final int id) {
|
||||
return this.vehicleService.getVehicle(id);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -6,25 +6,30 @@ 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;
|
||||
|
||||
public CryptoPriceService(final CryptoPriceRepository cryptoPriceRepository) {
|
||||
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yy");
|
||||
|
||||
public CryptoPriceService( final CryptoPriceRepository cryptoPriceRepository ) {
|
||||
this.cryptoPriceRepository = cryptoPriceRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CryptoPrice createPrice( final String createdDate, final String type, final Long av_price,
|
||||
final Long h_price, final Long l_price, final Long c_price, final Long volume ) {
|
||||
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))
|
||||
.timestamp(LocalDate.parse(createdDate, format))
|
||||
.type(type)
|
||||
.average_price(av_price)
|
||||
.high_price(h_price)
|
||||
@ -37,14 +42,14 @@ public class CryptoPriceService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getAllPrices(final int count) {
|
||||
public List<CryptoPrice> getAllPrices( final int count ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.limit(count)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<CryptoPrice> getAllPricesByType(final int count, final String type) {
|
||||
public List<CryptoPrice> getLimitPricesByType( final int count, final String type ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(typeList -> typeList.getType().equals(type))
|
||||
.limit(count)
|
||||
@ -52,18 +57,41 @@ public class CryptoPriceService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<CryptoPrice> getVehicle(final int id) {
|
||||
return this.cryptoPriceRepository.findById(id);
|
||||
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> getPriceByCreatedDate(final String type, final String createdDate) {
|
||||
public Optional<CryptoPrice> getLatestByType( final String type ) {
|
||||
return this.cryptoPriceRepository.findAll().stream()
|
||||
.filter(typeList -> typeList.getType().equals(type))
|
||||
.filter(createdDateList -> createdDateList.getTimestamp().equals(createdDate))
|
||||
.limit(1)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
// @Transactional(readOnly = true)
|
||||
// public Optional<CryptoPrice> getprice
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
14
src/main/java/cryptosky/me/helpers/functionalMapping.java
Normal file
14
src/main/java/cryptosky/me/helpers/functionalMapping.java
Normal file
@ -0,0 +1,14 @@
|
||||
package cryptosky.me.helpers;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class functionalMapping {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -10,10 +10,10 @@ type CryptoPrice {
|
||||
}
|
||||
|
||||
type Query {
|
||||
vehicles(count: Int):[CryptoPrice]
|
||||
vehicle(id: ID):CryptoPrice
|
||||
allPrices(count: Int):[CryptoPrice],
|
||||
allLatest:[CryptoPrice]
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createVehicle(type: String!, average_price: Float!, high_price: Float, low_price: Float, close_price: Float, volume: Float):CryptoPrice
|
||||
createPrice(createdDate: String!, type: String!, average_price: Float!, high_price: Float, low_price: Float, close_price: Float, volume: Float):CryptoPrice
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user