[31.01.20] Start of GraphQL creation and messing
This commit is contained in:
parent
88bdd6c7eb
commit
ea49e080a3
39
pom.xml
39
pom.xml
@ -8,6 +8,7 @@
|
||||
<version>2.2.4.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>cryptosky.me</groupId>
|
||||
<artifactId>db-gateway</artifactId>
|
||||
<version>0.0.1</version>
|
||||
@ -81,6 +82,24 @@
|
||||
<version>5.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.graphql-java</groupId>
|
||||
<artifactId>graphql-java-servlet</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@ -107,11 +126,31 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<finalName>Cryptosky DB Gateway</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.4.6.v20170531</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
13
src/main/java/cryptosky/me/dbgateway/Application.java
Normal file
13
src/main/java/cryptosky/me/dbgateway/Application.java
Normal file
@ -0,0 +1,13 @@
|
||||
package cryptosky.me.dbgateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package cryptosky.me.dbgateway.exceptions.data;
|
||||
|
||||
import graphql.ErrorType;
|
||||
import graphql.GraphQLError;
|
||||
import graphql.language.SourceLocation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class cryptoPriceNotFoundException extends RuntimeException implements GraphQLError {
|
||||
private Map<String, Object> extensions = new HashMap<>();
|
||||
|
||||
public cryptoPriceNotFoundException(String message, Long invalidId) {
|
||||
super(message);
|
||||
extensions.put("invalidId", invalidId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SourceLocation> getLocations() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getExtensions() {
|
||||
return extensions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorType getErrorType() {
|
||||
return ErrorType.DataFetchingException;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package cryptosky.me.dbgateway.exceptions.service;
|
||||
|
||||
import graphql.ErrorType;
|
||||
import graphql.GraphQLError;
|
||||
import graphql.language.SourceLocation;
|
||||
import graphql.ExceptionWhileDataFetching;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class GraphQLAdapterError implements GraphQLError {
|
||||
|
||||
private GraphQLError error;
|
||||
|
||||
public GraphQLAdapterError(GraphQLError error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getExtensions() {
|
||||
return error.getExtensions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SourceLocation> getLocations() {
|
||||
return error.getLocations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorType getErrorType() {
|
||||
return error.getErrorType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> getPath() {
|
||||
return error.getPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toSpecification() {
|
||||
return error.toSpecification();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return (error instanceof ExceptionWhileDataFetching) ? ((ExceptionWhileDataFetching) error).getException().getMessage() : error.getMessage();
|
||||
}
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
package cryptosky.me.dbgateway.jpa.entities;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
@Entity
|
||||
public class BtcEntity implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(name = "ID", nullable = false)
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Column(name = "type", nullable = false)
|
||||
private String type;
|
||||
|
||||
@Column(name = "model_code", nullable = false)
|
||||
private String modelCode;
|
||||
|
||||
@Column(name = "brand_name")
|
||||
private String brandName;
|
||||
|
||||
@Column(name = "launch_date")
|
||||
private LocalDate launchDate;
|
||||
|
||||
private transient String formattedDate;
|
||||
|
||||
// Getter and setter
|
||||
public String getFormattedDate() {
|
||||
return getLaunchDate().toString();
|
||||
}
|
||||
}
|
||||
47
src/main/java/cryptosky/me/dbgateway/jpa/entity/Vehicle.java
Normal file
47
src/main/java/cryptosky/me/dbgateway/jpa/entity/Vehicle.java
Normal file
@ -0,0 +1,47 @@
|
||||
package cryptosky.me.dbgateway.jpa.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@ToString
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
@Entity
|
||||
public class Vehicle {
|
||||
|
||||
@Id
|
||||
@Column(name = "ID", nullable = false)
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Column(name = "timestamp", nullable = false)
|
||||
private LocalDate timestamp;
|
||||
|
||||
@Column(name = "av_price", nullable = false)
|
||||
private Long average_price;
|
||||
|
||||
@Column(name = "h_price", nullable = false)
|
||||
private Long high_price;
|
||||
|
||||
@Column(name = "l_price", nullable = false)
|
||||
private Long low_price;
|
||||
|
||||
@Column(name = "c_price", nullable = false)
|
||||
private Long close_price;
|
||||
|
||||
@Column(name = "volume", nullable = false)
|
||||
private Long volume;
|
||||
|
||||
private transient String formattedDate;
|
||||
|
||||
public String getFormattedDate() {
|
||||
return getTimestamp().toString();
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package cryptosky.me.dbgateway.jpa.repositories;
|
||||
|
||||
import cryptosky.me.dbgateway.jpa.entities.BtcEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@Repository
|
||||
public class BtcRepository extends JpaRepository<BtcEntity, Integer>{
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cryptosky.me.dbgateway.jpa.repository;
|
||||
|
||||
import cryptosky.me.dbgateway.jpa.entity.Vehicle;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface VehicleRepository extends JpaRepository<Vehicle, Integer> {
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package cryptosky.me.dbgateway.models;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@ToString
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
public class CryptoPriceModel {
|
||||
|
||||
@Id
|
||||
@Column(name = "id", nullable = false)
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "timestamp", nullable = false)
|
||||
private Timestamp timestamp;
|
||||
|
||||
@Column(name = "type", nullable = false)
|
||||
private String type;
|
||||
|
||||
@Column(name = "av_price", nullable = false)
|
||||
private Long average_price;
|
||||
|
||||
@Column(name = "h_price", nullable = false)
|
||||
private Long high_price;
|
||||
|
||||
@Column(name = "l_price", nullable = false)
|
||||
private Long low_price;
|
||||
|
||||
@Column(name = "c_price", nullable = false)
|
||||
private Long close_price;
|
||||
|
||||
@Column(name = "volume", nullable = false)
|
||||
private Long volume;
|
||||
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package cryptosky.me.dbgateway.mutation;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import cryptosky.me.dbgateway.jpa.entity.Vehicle;
|
||||
import cryptosky.me.dbgateway.service.VehicleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Component
|
||||
public class VehicleMutation implements GraphQLMutationResolver {
|
||||
|
||||
@Autowired
|
||||
private VehicleService vehicleService;
|
||||
|
||||
public Vehicle createVehicle(final LocalDate timestamp, final Long av_price, final Long h_price, final Long l_price, final Long c_price, final Long volume ) {
|
||||
return this.vehicleService.createVehicle( timestamp, av_price, h_price, l_price, c_price, volume );
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
package cryptosky.me.dbgateway.mutations;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import cryptosky.me.dbgateway.jpa.entities.BtcEntity;
|
||||
import cryptosky.me.dbgateway.service.BtcService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BtcMutation implements GraphQLMutationResolver {
|
||||
|
||||
@Autowired
|
||||
private BtcService btcService;
|
||||
|
||||
public BtcEntity createBtc(final String type, final String modelCode, final String brandName, final String launchDate) {
|
||||
return this.btcService.createBtc(type, modelCode, brandName, launchDate);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package cryptosky.me.dbgateway.query;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
|
||||
import cryptosky.me.dbgateway.jpa.entities.BtcEntity;
|
||||
import cryptosky.me.dbgateway.service.BtcService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class BtcQuery implements GraphQLQueryResolver {
|
||||
|
||||
@Autowired
|
||||
private BtcService btcService;
|
||||
|
||||
public List<BtcEntity> getBtc(final int count) {
|
||||
return this.btcService.getAllBtc(count);
|
||||
}
|
||||
|
||||
public Optional<BtcEntity> getVehicle(final int id) {
|
||||
return this.btcService.getBtc(id);
|
||||
}
|
||||
}
|
||||
25
src/main/java/cryptosky/me/dbgateway/query/VehicleQuery.java
Normal file
25
src/main/java/cryptosky/me/dbgateway/query/VehicleQuery.java
Normal file
@ -0,0 +1,25 @@
|
||||
package cryptosky.me.dbgateway.query;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
|
||||
import cryptosky.me.dbgateway.jpa.entity.Vehicle;
|
||||
import cryptosky.me.dbgateway.service.VehicleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class VehicleQuery implements GraphQLQueryResolver {
|
||||
|
||||
@Autowired
|
||||
private VehicleService vehicleService;
|
||||
|
||||
public List<Vehicle> getVehicles(final int count) {
|
||||
return this.vehicleService.getAllVehicles(count);
|
||||
}
|
||||
|
||||
public Optional<Vehicle> getVehicle(final int id) {
|
||||
return this.vehicleService.getVehicle(id);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package cryptosky.me.dbgateway.repository;
|
||||
|
||||
import cryptosky.me.dbgateway.models.CryptoPriceModel;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CryptoPriceRepository extends CrudRepository<CryptoPriceModel, Long> {
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package cryptosky.me.dbgateway.resolvers.dataResolvers;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLResolver;
|
||||
import cryptosky.me.dbgateway.models.CryptoPriceModel;
|
||||
import cryptosky.me.dbgateway.repository.CryptoPriceRepository;
|
||||
|
||||
public class CryptoPriceResolver implements GraphQLResolver<CryptoPriceModel> {
|
||||
private CryptoPriceRepository cryptoPriceRepository;
|
||||
|
||||
public CryptoPriceResolver( CryptoPriceRepository cryptoPriceRepository ) {
|
||||
this.cryptoPriceRepository = cryptoPriceRepository;
|
||||
}
|
||||
|
||||
// public CryptoPriceModel getPrice( Long id ) {
|
||||
// return cryptoPriceRepository.findById( CryptoPriceModel );
|
||||
// }
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package cryptosky.me.dbgateway.resolvers.mutations;
|
||||
|
||||
import com.coxautodev.graphql.tools.GraphQLMutationResolver;
|
||||
import cryptosky.me.dbgateway.models.CryptoPriceModel;
|
||||
import cryptosky.me.dbgateway.repository.CryptoPriceRepository;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class CryptoPriceMutation implements GraphQLMutationResolver {
|
||||
private CryptoPriceRepository cryptoPriceRepository;
|
||||
|
||||
// public Mutation(CryptoPriceRepository cryptoPriceRepository) {
|
||||
// this.cryptoPriceRepository = cryptoPriceRepository;
|
||||
// }
|
||||
|
||||
public CryptoPriceModel newPriceEntry(Timestamp timestamp, String type, Long average_price, Long high_price, Long low_price, Long close_price, Long volume) {
|
||||
CryptoPriceModel cryptoPriceModel = CryptoPriceModel.builder()
|
||||
.timestamp(timestamp)
|
||||
.type(type)
|
||||
.average_price(average_price)
|
||||
.high_price(high_price)
|
||||
.low_price(low_price)
|
||||
.close_price(close_price)
|
||||
.volume(volume)
|
||||
.build();
|
||||
|
||||
cryptoPriceRepository.save(cryptoPriceModel);
|
||||
|
||||
return cryptoPriceModel;
|
||||
}
|
||||
|
||||
// public boolean deleteCrypto(Long id) {
|
||||
// cryptoPriceRepository.delete(id);
|
||||
// return true;
|
||||
// }
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package cryptosky.me.dbgateway.service;
|
||||
|
||||
import cryptosky.me.dbgateway.jpa.entities.BtcEntity;
|
||||
import cryptosky.me.dbgateway.jpa.repositories.BtcRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class BtcService {
|
||||
|
||||
private final BtcRepository btcRepository;
|
||||
|
||||
public BtcService(final BtcRepository btcRepository) {
|
||||
this.btcRepository = btcRepository ;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BtcEntity createBtc(final String type, final String modelCode, final String brandName, final String launchDate) {
|
||||
final BtcEntity btc = new BtcEntity();
|
||||
btc.setType(type);
|
||||
btc.setModelCode(modelCode);
|
||||
btc.setBrandName(brandName);
|
||||
btc.setLaunchDate(LocalDate.parse(launchDate));
|
||||
return this.btcRepository.save(btc);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BtcEntity> getAllBtc(final int count) {
|
||||
return this.btcRepository.findAll().stream().limit(count).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BtcEntity> getBtc(final int id) {
|
||||
return this.btcRepository.findById(id);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package cryptosky.me.dbgateway.service;
|
||||
|
||||
import cryptosky.me.dbgateway.jpa.entity.Vehicle;
|
||||
import cryptosky.me.dbgateway.jpa.repository.VehicleRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class VehicleService {
|
||||
|
||||
private final VehicleRepository vehicleRepository ;
|
||||
|
||||
public VehicleService(final VehicleRepository vehicleRepository) {
|
||||
this.vehicleRepository = vehicleRepository ;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Vehicle createVehicle( final LocalDate timestamp, final Long av_price, final Long h_price, final Long l_price, final Long c_price, final Long volume ) {
|
||||
Vehicle vehicle = Vehicle.builder()
|
||||
.timestamp(timestamp)
|
||||
.average_price(av_price)
|
||||
.high_price(h_price)
|
||||
.low_price(l_price)
|
||||
.close_price(c_price)
|
||||
.volume(volume)
|
||||
.build();
|
||||
|
||||
return this.vehicleRepository.save(vehicle);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Vehicle> getAllVehicles(final int count) {
|
||||
return this.vehicleRepository.findAll().stream().limit(count).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<Vehicle> getVehicle(final int id) {
|
||||
return this.vehicleRepository.findById(id);
|
||||
}
|
||||
}
|
||||
16
src/main/resources/graphql/vehicleql.graphqls
Normal file
16
src/main/resources/graphql/vehicleql.graphqls
Normal file
@ -0,0 +1,16 @@
|
||||
type Vehicle {
|
||||
id: ID!,
|
||||
type: String,
|
||||
modelCode: String,
|
||||
brandName: String,
|
||||
launchDate: String
|
||||
}
|
||||
|
||||
type Query {
|
||||
vehicles(count: Int):[Vehicle]
|
||||
vehicle(id: ID):Vehicle
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createVehicle(type: String!, modelCode: String!, brandName: String, launchDate: String):Vehicle
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user