Update transactions

This commit is contained in:
GME 2019-04-08 20:56:32 +02:00
parent 996c880055
commit 959db7b991
5 changed files with 346 additions and 40 deletions

View file

@ -3,8 +3,10 @@ package database.transaction;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.Date;
@DatabaseTable(tableName = "T_TRANSACTION")
public class Transaction {
public class Transaction implements Comparable<Transaction>{
@DatabaseField(generatedId = true)
private int transaction_id;
@DatabaseField(canBeNull = false)
@ -101,4 +103,11 @@ public class Transaction {
", transaction_unit='" + transaction_unit + '\'' +
'}';
}
@Override
public int compareTo(Transaction t){
Date date1 = new Date(getTransaction_date());
Date date2 = new Date(t.getTransaction_date());
return date1.compareTo(date2);
}
}

View file

@ -26,46 +26,48 @@ public class TransactionDao {
transactionDao.create(transaction);
}
public List<Transaction> getUserTransactions(String userHash)throws Exception{
public List<Transaction> getWalletTransactions(String wallet_hash)throws Exception{
transactionDao = createTransactionDaoConnection();
QueryBuilder<Transaction, String> queryBuilder = transactionDao.queryBuilder();
queryBuilder.where().eq("transaction_from",userHash).or().eq("transaction_to",userHash);
queryBuilder.where().eq("transaction_from",wallet_hash).or().eq("transaction_to",wallet_hash);
queryBuilder.orderBy("transaction_date",false);
PreparedQuery<Transaction> preparedQuery = queryBuilder.prepare();
return transactionDao.query(preparedQuery);
}
public List<Transaction> getTenLastUserTransactions(String userHash)throws Exception{
/*
public List<Transaction> getTenLastUserTransactions(String wallet_hash)throws Exception{
transactionDao = createTransactionDaoConnection();
QueryBuilder<Transaction, String> queryBuilder = transactionDao.queryBuilder();
queryBuilder.where().eq("transaction_from",userHash).or().eq("transaction_to",userHash);
queryBuilder.where().eq("transaction_from",wallet_hash).or().eq("transaction_to",wallet_hash);
queryBuilder.limit(new Long(10));
queryBuilder.orderBy("transaction_date",false);
PreparedQuery<Transaction> preparedQuery = queryBuilder.prepare();
return transactionDao.query(preparedQuery);
}
*/
public Transaction getTransaction(String userHash, String transactionHash)throws Exception{
public Transaction getTransaction(String wallet_hash, String transaction_hash)throws Exception{
transactionDao = createTransactionDaoConnection();
QueryBuilder<Transaction, String> queryBuilder = transactionDao.queryBuilder();
queryBuilder.where().eq("transaction_to",userHash).or().eq("transaction_from",userHash).and().eq("transaction_hash",transactionHash);
queryBuilder.where().eq("transaction_to",wallet_hash).or().eq("transaction_from",wallet_hash).and().eq("transaction_hash",transaction_hash);
PreparedQuery<Transaction> preparedQuery = queryBuilder.prepare();
return transactionDao.queryForFirst(preparedQuery);
}
public List<Transaction> getUserSentTransaction(String userHash)throws Exception{
public List<Transaction> getUserSentTransaction(String wallet_hash)throws Exception{
transactionDao = createTransactionDaoConnection();
QueryBuilder<Transaction, String> queryBuilder = transactionDao.queryBuilder();
queryBuilder.where().eq("transaction_from",userHash);
queryBuilder.where().eq("transaction_from",wallet_hash);
queryBuilder.orderBy("transaction_date",false);
PreparedQuery<Transaction> preparedQuery = queryBuilder.prepare();
return transactionDao.query(preparedQuery);
}
public List<Transaction> getUserReceivedTransaction(String userHash)throws Exception{
public List<Transaction> getUserReceivedTransaction(String wallet_hash)throws Exception{
transactionDao = createTransactionDaoConnection();
QueryBuilder<Transaction, String> queryBuilder = transactionDao.queryBuilder();
queryBuilder.where().eq("transaction_to",userHash);
queryBuilder.where().eq("transaction_to",wallet_hash);
queryBuilder.orderBy("transaction_date",false);
PreparedQuery<Transaction> preparedQuery = queryBuilder.prepare();
return transactionDao.query(preparedQuery);

View file

@ -2,6 +2,8 @@ package restImplementation;
import blockchain.query.QueryWrapper;
import blockchain.query.TransactionWrapper;
import database.Wallet.Wallet;
import database.Wallet.WalletDao;
import database.transaction.Transaction;
import database.transaction.TransactionDao;
import database.user.User;
@ -14,8 +16,7 @@ import javax.json.JsonObject;
import javax.json.JsonReader;
import java.io.StringReader;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.*;
public class TransactionImplementation {
@ -82,29 +83,100 @@ public class TransactionImplementation {
}
public List<Transaction> getSentTransaction(String userHash) throws Exception {
//GET
public Transaction getTransaction(String wallet_hash, String transaction_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserSentTransaction(userHash);
return transactionDao.getTransaction(wallet_hash,transaction_hash);
}
public List<Transaction> getReceivedTransaction(String userHash) throws Exception {
//SENT
public List<Transaction> getSentTransaction(String wallet_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserReceivedTransaction(userHash);
return transactionDao.getUserSentTransaction(wallet_hash);
}
public List<List<Transaction>> getAllSentTransaction(String user_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
WalletDao walletDao = new WalletDao();
List<List<Transaction>> transactionList = new ArrayList<>();
List<Wallet> userWallets = walletDao.getUserWallet(user_hash);
for(Wallet w : userWallets){
transactionList.add(transactionDao.getUserSentTransaction(w.getWallet_hash()));
}
return transactionList;
}
public Transaction getTransaction(String userHash, String transactionHash) throws Exception {
//RECEIVED
public List<Transaction> getReceivedTransaction(String wallet_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getTransaction(userHash,transactionHash);
return transactionDao.getUserReceivedTransaction(wallet_hash);
}
public List<List<Transaction>> getAllReceivedTransaction(String user_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
WalletDao walletDao = new WalletDao();
List<List<Transaction>> transactionList = new ArrayList<>();
List<Wallet> userWallets = walletDao.getUserWallet(user_hash);
for(Wallet w : userWallets){
transactionList.add(transactionDao.getUserReceivedTransaction(w.getWallet_hash()));
}
return transactionList;
}
public List<Transaction> getLatestTransactions(String userHash) throws Exception {
public List<Transaction> getLatestTransactions(String user_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getTenLastUserTransactions(userHash);
WalletDao walletDao = new WalletDao();
List<Transaction> returnList = new ArrayList<>();
List<Wallet> userWallets = walletDao.getUserWallet(user_hash);
List<Transaction> allTransactions = new ArrayList<>();
for(Wallet w : userWallets){
List<Transaction> list = transactionDao.getWalletTransactions(w.getWallet_hash());
for(Transaction t : list){
allTransactions.add(t);
}
}
Collections.sort(allTransactions);
int iterator = 0;
int reverseListIndex = allTransactions.size()-1;
while (iterator<10){
returnList.add(allTransactions.get(reverseListIndex));
iterator++;
reverseListIndex--;
}
return returnList;
}
public List<Transaction> getUserTransactions(String userHash) throws Exception {
public List<Transaction> getAllTransactions(String user_hash) throws Exception {
TransactionDao transactionDao = new TransactionDao();
return transactionDao.getUserTransactions(userHash);
WalletDao walletDao = new WalletDao();
List<Wallet> userWallets = walletDao.getUserWallet(user_hash);
List<Transaction> allTransactions = new ArrayList<>();
List<Transaction> returnList = new ArrayList<>();
for(Wallet w : userWallets){
List<Transaction> list = transactionDao.getWalletTransactions(w.getWallet_hash());
for(Transaction t : list){
allTransactions.add(t);
}
}
Collections.sort(allTransactions);
for(int i=allTransactions.size()-1;i>0;i--){
returnList.add(allTransactions.get(i));
}
return returnList;
}
//TODO SELECT BETWEEN
}

View file

@ -15,6 +15,7 @@ import java.util.List;
@RequestMapping(value = "/api/rest/transaction")
public class TransactionResource {
//DO TRANSACTION
@RequestMapping(value = "/send", method = RequestMethod.POST, produces = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity doTransaction(@Valid @RequestBody SendingTransaction SendingTransaction){
@ -35,25 +36,45 @@ public class TransactionResource {
}
}
@RequestMapping(value = "/get", method = RequestMethod.GET, params = {"userHash","txID"},produces = "application/json")
//GET A TRANSACTION
@RequestMapping(value = "/get", method = RequestMethod.GET, params = {"wallet_hash","txID"},produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getTransaction(@RequestParam(value = "txID") String txID, @RequestParam(value = "userHash") String userHash){
public ResponseEntity getTransaction(@RequestParam(value = "txID") String txID, @RequestParam(value = "wallet_hash") String wallet_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
Transaction transaction = transactionImplementation.getTransaction(userHash,txID);
return ResponseEntity.status(HttpStatus.OK).body(transaction);
Transaction transaction = transactionImplementation.getTransaction(wallet_hash,txID);
if(transaction!=null){
return ResponseEntity.status(HttpStatus.OK).body(transaction);
} else {
return new ResponseEntity("{\"response\":\"error\"}", HttpStatus.NOT_FOUND);
}
}catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
@RequestMapping(value = "/get/sent", method = RequestMethod.GET, params = {"userHash"}, produces = "application/json")
//SENT
//GET ALL SENT TRANSACTION
@RequestMapping(value = "/get/allSent", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getSentTransaction(@RequestParam(value = "userHash") String userHash){
public ResponseEntity getAllSentTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getSentTransaction(userHash);
List<List<Transaction>> listTransaction = transactionImplementation.getAllSentTransaction(user_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
//GET SENT FROM WALLET
@RequestMapping(value = "/get/sent", method = RequestMethod.GET, params = {"wallet_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getSentTransaction(@RequestParam(value = "wallet_hash") String wallet_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getSentTransaction(wallet_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
@ -61,12 +82,28 @@ public class TransactionResource {
}
}
@RequestMapping(value = "/get/received", method = RequestMethod.GET, params = {"userHash"}, produces = "application/json")
//RECEIVED
//GET ALL RECEIVED TRANSACTION
@RequestMapping(value = "/get/allReceived", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getReceivedTransaction(@RequestParam(value = "userHash") String userHash){
public ResponseEntity getAllReceivedTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getReceivedTransaction(userHash);
List<List<Transaction>> listTransaction = transactionImplementation.getAllReceivedTransaction(user_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
//GET RECEIVED FROM WALLET
@RequestMapping(value = "/get/received", method = RequestMethod.GET, params = {"wallet_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getReceivedTransaction(@RequestParam(value = "wallet_hash") String wallet_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getReceivedTransaction(wallet_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
@ -74,12 +111,14 @@ public class TransactionResource {
}
}
@RequestMapping(value = "/get/latest", method = RequestMethod.GET, params = {"userHash"}, produces = "application/json")
@RequestMapping(value = "/get/latest", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getLatestTransaction(@RequestParam(value = "userHash") String userHash){
public ResponseEntity getLatestTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getLatestTransactions(userHash);
List<Transaction> listTransaction = transactionImplementation.getLatestTransactions(user_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
@ -87,16 +126,18 @@ public class TransactionResource {
}
}
@RequestMapping(value = "/getAll", method = RequestMethod.GET, params = {"userHash"}, produces = "application/json")
@RequestMapping(value = "/getAll", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getAllTransaction(@RequestParam(value = "userHash") String userHash){
public ResponseEntity getAllTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getUserTransactions(userHash);
List<Transaction> listTransaction = transactionImplementation.getAllTransactions(user_hash);
return ResponseEntity.status(HttpStatus.OK).body(listTransaction);
} catch (Exception e){
StringResponse responseS = new StringResponse(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
}

View file

@ -1,21 +1,69 @@
package restImplementation;
import database.transaction.Transaction;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Ignore
public class TransactionImplementationTest {
private static Logger logger = Logger.getLogger(TransactionImplementationTest.class);
/*
@Test
public void doMultipleTransaction(){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
for(int i=0;i<12;i++){
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.","thomasPwd158$*",
"$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu","$2a$10$hdvk6LuOxVOI1EXtmswh0eJco0nOJVTEaJdEKuy/6tGcdxetDbY4i",1.0,"gonette");
if(Boolean.parseBoolean(transactionResponse.get("success").toString())){
logger.info("transaction ID: "+transactionResponse.get("message"));
} else {
logger.warn("Error (else) : "+transactionResponse.get("message"));
}
}
for(int i=0;i<4;i++){
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$GzyMhD3n05Z2lwCnoGi2dusjKrNAmHhtn5fw1xLpes5.cEV5T7lve","gonClaire789$*",
"$2a$10$UKiTo/eqpnvlny3U5xpJ../C2WOgoxebAl3xiexAhinC8X7Jt2wY.","$2a$10$8PJyraUjMoL/h.cQjJyMZedBUlcTjPjNm4j1NuqzQBTnSe2XNuGM2",1.0,"gonette");
if(Boolean.parseBoolean(transactionResponse.get("success").toString())){
logger.info("transaction ID: "+transactionResponse.get("message"));
} else {
logger.warn("Error (else) : "+transactionResponse.get("message"));
}
}
for(int i=0;i<3;i++){
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$GzyMhD3n05Z2lwCnoGi2dusjKrNAmHhtn5fw1xLpes5.cEV5T7lve","gonClaire789$*",
"$2a$10$hdvk6LuOxVOI1EXtmswh0eJco0nOJVTEaJdEKuy/6tGcdxetDbY4i","$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu",1.0,"gonette");
if(Boolean.parseBoolean(transactionResponse.get("success").toString())){
logger.info("transaction ID: "+transactionResponse.get("message"));
} else {
logger.warn("Error (else) : "+transactionResponse.get("message"));
}
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void doTransaction(){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$WN6ARfShm9bgRZ8s9bzZqejvL.VzZrjXRmZLj6N3U6No9G/YLVqVi","$2a$10$7gIjefImB/xYvsOukIbk5.miCn88CE9pBfhz9xE5NE4HwQKZWcorS",1.0,"gonette");
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.","thomasPwd158$*",
"$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu","$2a$10$8PJyraUjMoL/h.cQjJyMZedBUlcTjPjNm4j1NuqzQBTnSe2XNuGM2",1.0,"gonette");
if(Boolean.parseBoolean(transactionResponse.get("success").toString())){
logger.info("transaction ID: "+transactionResponse.get("message"));
} else {
@ -26,4 +74,138 @@ public class TransactionImplementationTest {
}
}
*/
/*
@Test
public void doTransaction(){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
HashMap transactionResponse = transactionImplementation.sendTransaction("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.","thomasPwd158$*",
"$2a$10$8PJyraUjMoL/h.cQjJyMZedBUlcTjPjNm4j1NuqzQBTnSe2XNuGM2","$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu",1.0,"gonette");
if(Boolean.parseBoolean(transactionResponse.get("success").toString())){
logger.info("transaction ID: "+transactionResponse.get("message"));
} else {
logger.warn("Error (else) : "+transactionResponse.get("message"));
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getAllSentTransaction(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<List<Transaction>> result = transactionImplementation.getAllSentTransaction("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.");
//logger.info(result);
for(List<Transaction> re : result){
logger.info(re);
for(Transaction t: re){
logger.info(t);
}
logger.info(" other wallet");
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getSent(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> result = transactionImplementation.getSentTransaction("$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu");
//logger.info(result);
for(Transaction re : result){
logger.info(re);
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getAllReceivedTransaction(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<List<Transaction>> result = transactionImplementation.getAllReceivedTransaction("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.");
//logger.info(result);
for(List<Transaction> re : result){
logger.info(re);
for(Transaction t: re){
logger.info(t);
}
logger.info(" other wallet");
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getReceived(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> result = transactionImplementation.getReceivedTransaction("$2a$10$6Z4uNeOgAKhpWYGVYwPWhe6G91lIKHOCA2yYcpzS1pbJyosNNw3Gu");
//logger.info(result);
for(Transaction re : result){
logger.info(re);
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getLatestTransactions(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try{
List<Transaction> listT = transactionImplementation.getLatestTransactions("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.");
logger.info("size : "+listT.size());
for(Transaction t:listT){
logger.info(t.getTransaction_from()+" - "+t.getTransaction_to()+" - "+new Date(t.getTransaction_date()));
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
/*
@Test
public void getAllTransaction(){
BasicConfigurator.configure();
TransactionImplementation transactionImplementation = new TransactionImplementation();
try{
List<Transaction> listT = transactionImplementation.getAllTransactions("$2a$10$J7B8sXULfiPfnlKRlTjqQuSrbumTyv.acB8NpxDrUQ3WmnfKdjx0.");
logger.info("size : "+listT.size());
for(Transaction t:listT){
logger.info(new Date(t.getTransaction_date())+"-"+t);
}
}catch (Exception e){
logger.warn("Error: "+e.getMessage());
}
}
*/
}