Setup transaction api base
This commit is contained in:
parent
16c97a6608
commit
5be8d03dd3
|
@ -22,7 +22,6 @@ public class QueryWrapper {
|
||||||
UserContext user = Util.readUserContext(Config.ORG1, "admin");
|
UserContext user = Util.readUserContext(Config.ORG1, "admin");
|
||||||
String response = null;
|
String response = null;
|
||||||
|
|
||||||
//try {
|
|
||||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(user);
|
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(user);
|
||||||
ChannelClientWrapper channelClientWrapper = ChannelClientWrapper.setupChannel(fabricClientWrapper);
|
ChannelClientWrapper channelClientWrapper = ChannelClientWrapper.setupChannel(fabricClientWrapper);
|
||||||
|
|
||||||
|
@ -49,12 +48,6 @@ public class QueryWrapper {
|
||||||
logger.info("Query payload : " + response + " from peer : " + proposalResponse.getPeer().getName());
|
logger.info("Query payload : " + response + " from peer : " + proposalResponse.getPeer().getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package restImplementation;
|
||||||
|
|
||||||
|
import blockchain.query.QueryWrapper;
|
||||||
|
import blockchain.query.TransactionWrapper;
|
||||||
|
import database.transaction.Transaction;
|
||||||
|
import database.transaction.TransactionDao;
|
||||||
|
import org.hyperledger.fabric.sdk.BlockEvent;
|
||||||
|
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TransactionImplementation {
|
||||||
|
|
||||||
|
public HashMap sendTransaction(String sourceWalletHash, String destinationWalletHash, double amount, String transactionUnit) throws Exception {
|
||||||
|
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||||
|
HashMap returnResponse = new HashMap();
|
||||||
|
|
||||||
|
QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
|
String response = queryWrapper.sendQuery("readWallet", new String[]{sourceWalletHash});
|
||||||
|
|
||||||
|
if (response != null){
|
||||||
|
JsonReader reader = Json.createReader(new StringReader(response));
|
||||||
|
JsonObject walletInfo = reader.readObject();
|
||||||
|
double balance = walletInfo.getJsonNumber("sold").doubleValue();
|
||||||
|
|
||||||
|
if(balance-amount<0){ //CHECK WALLET BALANCE
|
||||||
|
returnResponse.put("success",false);
|
||||||
|
returnResponse.put("message","Not enough money");
|
||||||
|
} else {
|
||||||
|
//CHECK DEST WALLET
|
||||||
|
String responseDestWallet = queryWrapper.sendQuery("readWallet", new String[]{destinationWalletHash});
|
||||||
|
if(responseDestWallet!=null){
|
||||||
|
BlockEvent.TransactionEvent eventResponse = transactionWrapper.sendTransaction("transaction",new String[]{sourceWalletHash,destinationWalletHash,String.valueOf(amount)});
|
||||||
|
String txID = eventResponse.getTransactionID();
|
||||||
|
|
||||||
|
Transaction transaction = new Transaction();
|
||||||
|
long now = Instant.now().toEpochMilli();
|
||||||
|
transaction.setTransactionDate(now);
|
||||||
|
transaction.setTransactionFrom(sourceWalletHash);
|
||||||
|
transaction.setTransactionTo(destinationWalletHash);
|
||||||
|
transaction.setTransactionAmount(amount);
|
||||||
|
transaction.setTransactionHash(txID);
|
||||||
|
transaction.setTransactionUnit(transactionUnit);
|
||||||
|
|
||||||
|
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
transactionDao.addTransaction(transaction);
|
||||||
|
|
||||||
|
returnResponse.put("success",true);
|
||||||
|
returnResponse.put("message",txID);
|
||||||
|
} else {
|
||||||
|
returnResponse.put("success",false);
|
||||||
|
returnResponse.put("message","Destination wallet doesn't exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
returnResponse.put("success",false);
|
||||||
|
returnResponse.put("message","Error in user wallet");
|
||||||
|
}
|
||||||
|
return returnResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Transaction> getSentTransaction(String userHash) throws Exception {
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
return transactionDao.getUserSentTransaction(userHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Transaction> getReceivedTransaction(String userHash) throws Exception {
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
return transactionDao.getUserReceivedTransaction(userHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Transaction getTransaction(String userHash, String transactionHash) throws Exception {
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
return transactionDao.getTransaction(userHash,transactionHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Transaction> getLatestTransactions(String userHash) throws Exception {
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
return transactionDao.getTenLastUserTransactions(userHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Transaction> getUserTransactions(String userHash) throws Exception {
|
||||||
|
TransactionDao transactionDao = new TransactionDao();
|
||||||
|
return transactionDao.getUserTransactions(userHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,10 +1,37 @@
|
||||||
package restService;
|
package restService;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import database.transaction.Transaction;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import restImplementation.TransactionImplementation;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/api/rest/transaction")
|
@RequestMapping(value = "/api/rest/transaction")
|
||||||
public class TransactionResource {
|
public class TransactionResource {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/send", method = RequestMethod.POST, produces = "application/json")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public ResponseEntity saveTransaction(@Valid @RequestBody Transaction transaction){
|
||||||
|
TransactionImplementation transactionImplementation = new TransactionImplementation();
|
||||||
|
try{
|
||||||
|
HashMap mapResponse = transactionImplementation.sendTransaction(transaction.getTransactionFrom(),transaction.getTransactionTo(),transaction.getTransactionAmount(),transaction.getTransactionUnit());
|
||||||
|
|
||||||
|
if(Boolean.parseBoolean(mapResponse.get("success").toString())){
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\""+mapResponse.get("message")+"\"}");
|
||||||
|
}else {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"response\":\""+mapResponse.get("message")+"\"}");
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
StringResponse responseS = new StringResponse(e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,6 +130,6 @@ public class UserResource {
|
||||||
@PostMapping(value = "/update")
|
@PostMapping(value = "/update")
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public ResponseEntity updateUser(@RequestBody User user){
|
public ResponseEntity updateUser(@RequestBody User user){
|
||||||
return new ResponseEntity(null, HttpStatus.OK);
|
return new ResponseEntity(null, HttpStatus.SERVICE_UNAVAILABLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,20 +7,61 @@ import org.apache.log4j.Logger;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public class ReadWalletTest {
|
public class ReadWalletTest {
|
||||||
private static Logger logger = Logger.getLogger(ReadWalletTest.class);
|
private static Logger logger = Logger.getLogger(ReadWalletTest.class);
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void TestReadWalletTest() {
|
public void SuccessTestReadWalletTest() {
|
||||||
BasicConfigurator.configure();
|
BasicConfigurator.configure();
|
||||||
try{
|
try{
|
||||||
QueryWrapper queryWrapper = new QueryWrapper();
|
QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
String functionName = "readWallet";
|
String functionName = "readWallet";
|
||||||
String[] args = new String[]{"qerh654d5f5h46q4fdh6h65fh00"};
|
String[] args = new String[]{"qerh654d5f5h46q4fdh6h65fh00"};
|
||||||
String response = queryWrapper.sendQuery(functionName,args);
|
String response = queryWrapper.sendQuery(functionName,args);
|
||||||
|
|
||||||
|
if(response!=null){
|
||||||
|
JsonReader reader = Json.createReader(new StringReader(response));
|
||||||
|
JsonObject walletInfo = reader.readObject();
|
||||||
|
double balance = walletInfo.getJsonNumber("sold").doubleValue();
|
||||||
|
|
||||||
logger.info("response : "+response);
|
logger.info("response : "+response);
|
||||||
|
logger.info("balance : "+balance);
|
||||||
|
}
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void FailTestReadWalletTest() {
|
||||||
|
BasicConfigurator.configure();
|
||||||
|
try{
|
||||||
|
QueryWrapper queryWrapper = new QueryWrapper();
|
||||||
|
String functionName = "readWallet";
|
||||||
|
String[] args = new String[]{"tttt"};
|
||||||
|
String response = queryWrapper.sendQuery(functionName,args);
|
||||||
|
|
||||||
|
if(response!=null){
|
||||||
|
JsonReader reader = Json.createReader(new StringReader(response));
|
||||||
|
JsonObject walletInfo = reader.readObject();
|
||||||
|
double balance = walletInfo.getJsonNumber("sold").doubleValue();
|
||||||
|
|
||||||
|
logger.info("response : "+response);
|
||||||
|
logger.info("balance : "+balance);
|
||||||
|
} else {
|
||||||
|
logger.warn("ERROR");
|
||||||
|
}
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,12 @@ import java.util.List;
|
||||||
public class GetUserAssociationTest {
|
public class GetUserAssociationTest {
|
||||||
private static Logger logger = Logger.getLogger(GetUserAssociationTest.class);
|
private static Logger logger = Logger.getLogger(GetUserAssociationTest.class);
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void GetUserAssociationTest() {
|
public void GetUserAssociationTest() {
|
||||||
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
||||||
String association = queryImplementation.getUserAssociation("bitman");
|
String association = queryImplementation.getUserAssociation("bitman");
|
||||||
logger.info("association : "+association);
|
logger.info("association : "+association);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.util.List;
|
||||||
public class GetUserWalletsTest {
|
public class GetUserWalletsTest {
|
||||||
private static Logger logger = Logger.getLogger(GetWalletSoldTest.class);
|
private static Logger logger = Logger.getLogger(GetWalletSoldTest.class);
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void GetUserWalletsTest() {
|
public void GetUserWalletsTest() {
|
||||||
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
||||||
|
@ -19,4 +20,5 @@ public class GetUserWalletsTest {
|
||||||
logger.info("walletHash : "+w.get("walletHash")+". walletType : "+w.get("walletType")+". sold : "+w.get("sold"));
|
logger.info("walletHash : "+w.get("walletHash")+". walletType : "+w.get("walletType")+". sold : "+w.get("sold"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,13 @@ import org.junit.Test;
|
||||||
public class GetWalletSoldTest {
|
public class GetWalletSoldTest {
|
||||||
private static Logger logger = Logger.getLogger(GetWalletSoldTest.class);
|
private static Logger logger = Logger.getLogger(GetWalletSoldTest.class);
|
||||||
|
|
||||||
|
/*
|
||||||
@Test
|
@Test
|
||||||
public void GetWalletSold() {
|
public void GetWalletSold() {
|
||||||
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
BlockchainQueryImplementation queryImplementation = new BlockchainQueryImplementation();
|
||||||
Double sold = queryImplementation.getWalletSold("qerh654d5f5h46q4fdh6h65fh00");
|
Double sold = queryImplementation.getWalletSold("qerh654d5f5h46q4fdh6h65fh00");
|
||||||
logger.info("result sold : "+sold);
|
logger.info("result sold : "+sold);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,11 +22,11 @@ public class WalletImplementationTest {
|
||||||
Map<String,String> walletResponse = walletImplementation.createWallet(wallet);
|
Map<String,String> walletResponse = walletImplementation.createWallet(wallet);
|
||||||
Wallet returnWallet = new Wallet();
|
Wallet returnWallet = new Wallet();
|
||||||
returnWallet.setWallet_hash(walletResponse.get("walletHash"));
|
returnWallet.setWallet_hash(walletResponse.get("walletHash"));
|
||||||
returnWallet.setSold(Double.parseDouble(walletResponse.get("walletSold")));
|
returnWallet.setBalance(Double.parseDouble(walletResponse.get("walletSold")));
|
||||||
returnWallet.setType(walletResponse.get("walletType"));
|
returnWallet.setType(walletResponse.get("walletType"));
|
||||||
|
|
||||||
logger.info("wallet hash: "+returnWallet.getWallet_hash());
|
logger.info("wallet hash: "+returnWallet.getWallet_hash());
|
||||||
logger.info("wallet sold: "+returnWallet.getSold());
|
logger.info("wallet sold: "+returnWallet.getBalance());
|
||||||
logger.info("wallet type: "+returnWallet.getType());
|
logger.info("wallet type: "+returnWallet.getType());
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
logger.warn("Error: "+e.getMessage());
|
logger.warn("Error: "+e.getMessage());
|
||||||
|
|
Loading…
Reference in a new issue