[02.02.20] Messing with inheritance for GraphQL

This commit is contained in:
Andy Sotheran 2020-02-02 22:44:34 +00:00
parent 66b3bfa721
commit 51b154e6d4
7 changed files with 71 additions and 31 deletions

View File

@ -71,6 +71,11 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>

View File

@ -0,0 +1,12 @@
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 {
}

View File

@ -6,12 +6,12 @@ import javax.persistence.*;
import java.time.LocalDate;
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@ToString
@Builder
@Data
@EqualsAndHashCode
@Entity
@Table(name = "crypto_price")
public class CryptoPrice {
@Id

View File

@ -1,6 +1,7 @@
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;
@ -12,8 +13,13 @@ 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 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);
}
}

View File

@ -1,5 +1,6 @@
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;
@ -25,22 +26,38 @@ public class CryptoPriceService {
}
@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 ) {
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 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();
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(cryptoPrice);
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()

View File

@ -1,10 +1,10 @@
server.port=9090
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#server.port=9090
##spring.jpa.database=POSTGRESQL
##spring.datasource.platform=postgres
##spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
##spring.datasource.username=postgres
##spring.datasource.password=*****
##spring.jpa.show-sql=true
##spring.jpa.generate-ddl=true
##spring.jpa.hibernate.ddl-auto=create-drop
##spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

View File

@ -1,4 +1,4 @@
type CryptoPrice {
type BtcPrice {
id: ID!,
timestamp: String!,
type: String,
@ -10,10 +10,10 @@ type CryptoPrice {
}
type Query {
allPrices(count: Int):[CryptoPrice],
allLatest:[CryptoPrice]
allPrices(count: Int):[BtcPrice],
allLatest:[BtcPrice]
}
type Mutation {
createPrice(createdDate: String!, type: String!, average_price: Float!, high_price: Float, low_price: Float, close_price: Float, volume: Float):CryptoPrice
createBtc(createdDate: String!, type: String!, average_price: Float!, high_price: Float, low_price: Float, close_price: Float, volume: Float):BtcPrice
}