[01.02.20] Additional functions and mapping for endpoints

This commit is contained in:
andyjk15 2020-02-01 00:29:53 +00:00
parent 5afa063e75
commit f57fa33522
6 changed files with 91 additions and 30 deletions

View File

@ -8,8 +8,6 @@ import java.time.LocalDate;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
@ToString @ToString
@Getter
@Setter
@Builder @Builder
@Data @Data
@EqualsAndHashCode @EqualsAndHashCode
@ -28,21 +26,21 @@ public class CryptoPrice {
private String type; private String type;
@Column(name = "av_price", nullable = false) @Column(name = "av_price", nullable = false)
private Long average_price; private float average_price;
@Column(name = "h_price", nullable = false) @Column(name = "h_price", nullable = false)
private Long high_price; private float high_price;
@Column(name = "l_price", nullable = false) @Column(name = "l_price", nullable = false)
private Long low_price; private float low_price;
@Column(name = "c_price") @Column(name = "c_price")
private Long close_price; private float close_price;
@Column(name = "volume") @Column(name = "volume")
private Long volume; private float volume;
private transient String formattedDate; private transient String formattedDate;
public String getFormattedDate() { public String getFormattedDate() {
return getTimestamp().toString(); return getTimestamp().toString();

View File

@ -12,8 +12,8 @@ public class CryptoPriceMutation implements GraphQLMutationResolver {
@Autowired @Autowired
private CryptoPriceService cryptoPriceService; private CryptoPriceService cryptoPriceService;
public CryptoPrice createPrice( final String createdDate, final String type, final Long av_price, public CryptoPrice createPrice(final String createdDate, final String type, final float av_price,
final Long h_price, final Long l_price, final Long c_price, final Long volume ) { 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); return this.cryptoPriceService.createPrice(createdDate, type, av_price, h_price, l_price, c_price, volume);
} }
} }

View File

@ -13,13 +13,34 @@ import java.util.Optional;
public class CryptoPriceQuery implements GraphQLQueryResolver { public class CryptoPriceQuery implements GraphQLQueryResolver {
@Autowired @Autowired
private CryptoPriceService vehicleService; private CryptoPriceService cryptoPriceService;
public List<CryptoPrice> getVehicles(final int count) { public List<CryptoPrice> getAllPrices(final int count) {
return this.vehicleService.getAllPrices(count); return this.cryptoPriceService.getAllPrices(count);
} }
public Optional<CryptoPrice> getVehicle(final int id) { public List<CryptoPrice> getLimitPricesByType( final int count, final String type ) {
return this.vehicleService.getVehicle(id); 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);
}
} }

View File

@ -6,25 +6,30 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cryptosky.me.helpers.functionalMapping.distinctByKey;
@Service @Service
public class CryptoPriceService { public class CryptoPriceService {
private final CryptoPriceRepository cryptoPriceRepository; private final CryptoPriceRepository cryptoPriceRepository;
public CryptoPriceService(final CryptoPriceRepository cryptoPriceRepository) { DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yy");
public CryptoPriceService( final CryptoPriceRepository cryptoPriceRepository ) {
this.cryptoPriceRepository = cryptoPriceRepository; this.cryptoPriceRepository = cryptoPriceRepository;
} }
@Transactional @Transactional
public CryptoPrice createPrice( final String createdDate, final String type, final Long av_price, public CryptoPrice createPrice(final String createdDate, final String type, final float av_price,
final Long h_price, final Long l_price, final Long c_price, final Long volume ) { final float h_price, final float l_price, final float c_price, final float volume ) {
final CryptoPrice cryptoPrice = CryptoPrice.builder() final CryptoPrice cryptoPrice = CryptoPrice.builder()
.timestamp(LocalDate.parse(createdDate)) .timestamp(LocalDate.parse(createdDate, format))
.type(type) .type(type)
.average_price(av_price) .average_price(av_price)
.high_price(h_price) .high_price(h_price)
@ -37,14 +42,14 @@ public class CryptoPriceService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<CryptoPrice> getAllPrices(final int count) { public List<CryptoPrice> getAllPrices( final int count ) {
return this.cryptoPriceRepository.findAll().stream() return this.cryptoPriceRepository.findAll().stream()
.limit(count) .limit(count)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Transactional(readOnly = true) @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() return this.cryptoPriceRepository.findAll().stream()
.filter(typeList -> typeList.getType().equals(type)) .filter(typeList -> typeList.getType().equals(type))
.limit(count) .limit(count)
@ -52,18 +57,41 @@ public class CryptoPriceService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Optional<CryptoPrice> getVehicle(final int id) { public List<CryptoPrice> getAllByType( final String type ) {
return this.cryptoPriceRepository.findById(id); return this.cryptoPriceRepository.findAll().stream()
.filter(typeList -> typeList.getType().equals(type))
.collect(Collectors.toList());
} }
@Transactional(readOnly = true) @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() return this.cryptoPriceRepository.findAll().stream()
.filter(typeList -> typeList.getType().equals(type)) .filter(typeList -> typeList.getType().equals(type))
.filter(createdDateList -> createdDateList.getTimestamp().equals(createdDate)) .limit(1)
.findFirst(); .findFirst();
} }
// @Transactional(readOnly = true) @Transactional(readOnly = true)
// public Optional<CryptoPrice> getprice 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();
}
} }

View 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;
}
}

View File

@ -10,10 +10,10 @@ type CryptoPrice {
} }
type Query { type Query {
vehicles(count: Int):[CryptoPrice] allPrices(count: Int):[CryptoPrice],
vehicle(id: ID):CryptoPrice allLatest:[CryptoPrice]
} }
type Mutation { 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
} }