[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> <artifactId>postgresql</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <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; import java.time.LocalDate;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor
@ToString @ToString
@Builder
@Data @Data
@EqualsAndHashCode @EqualsAndHashCode
@Entity @Entity
@Table(name = "crypto_price")
public class CryptoPrice { public class CryptoPrice {
@Id @Id

View File

@ -1,6 +1,7 @@
package cryptosky.me.graphql.mutation; package cryptosky.me.graphql.mutation;
import com.coxautodev.graphql.tools.GraphQLMutationResolver; import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import cryptosky.me.graphql.jpa.entity.BtcPrice;
import cryptosky.me.graphql.jpa.entity.CryptoPrice; import cryptosky.me.graphql.jpa.entity.CryptoPrice;
import cryptosky.me.graphql.service.CryptoPriceService; import cryptosky.me.graphql.service.CryptoPriceService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -12,8 +13,13 @@ public class CryptoPriceMutation implements GraphQLMutationResolver {
@Autowired @Autowired
private CryptoPriceService cryptoPriceService; private CryptoPriceService cryptoPriceService;
public CryptoPrice createPrice(final String createdDate, final String type, final float av_price, // 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 ) { 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.createBtc(createdDate, type, av_price, h_price, l_price, c_price, volume);
} }
} }

View File

@ -1,5 +1,6 @@
package cryptosky.me.graphql.service; package cryptosky.me.graphql.service;
import cryptosky.me.graphql.jpa.entity.BtcPrice;
import cryptosky.me.graphql.jpa.entity.CryptoPrice; import cryptosky.me.graphql.jpa.entity.CryptoPrice;
import cryptosky.me.graphql.jpa.repository.CryptoPriceRepository; import cryptosky.me.graphql.jpa.repository.CryptoPriceRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -25,22 +26,38 @@ public class CryptoPriceService {
} }
@Transactional @Transactional
public CryptoPrice createPrice(final String createdDate, final String type, final float av_price, 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 float h_price, final float l_price, final float c_price, final float volume ) {
final CryptoPrice cryptoPrice = CryptoPrice.builder() final BtcPrice btcPrice = new BtcPrice();
.timestamp(LocalDate.parse(createdDate, format)) btcPrice.setTimestamp(LocalDate.parse(createdDate, format));
.type(type) btcPrice.setAverage_price(av_price);
.average_price(av_price) btcPrice.setType(type);
.high_price(h_price) btcPrice.setHigh_price(h_price);
.low_price(l_price) btcPrice.setLow_price(l_price);
.close_price(c_price) btcPrice.setClose_price(c_price);
.volume(volume) btcPrice.setVolume(volume);
.build();
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) @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()

View File

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

View File

@ -1,4 +1,4 @@
type CryptoPrice { type BtcPrice {
id: ID!, id: ID!,
timestamp: String!, timestamp: String!,
type: String, type: String,
@ -10,10 +10,10 @@ type CryptoPrice {
} }
type Query { type Query {
allPrices(count: Int):[CryptoPrice], allPrices(count: Int):[BtcPrice],
allLatest:[CryptoPrice] allLatest:[BtcPrice]
} }
type Mutation { 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
} }