java-api/src/main/java/restService/TransactionResource.java
2019-04-12 19:30:30 +02:00

138 lines
7.5 KiB
Java

package restService;
import database.transaction.SendingTransaction;
import database.transaction.Transaction;
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;
import java.util.List;
@RestController
@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){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try{
HashMap mapResponse = transactionImplementation.sendTransaction(SendingTransaction.getSource_user_hash(), SendingTransaction.getSource_user_pwd(),
SendingTransaction.getTransaction_from(),SendingTransaction.getTransaction_to(),
Double.parseDouble(SendingTransaction.getTransaction_amount()),SendingTransaction.getTransaction_unit());
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);
}
}
//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 = "wallet_hash") String wallet_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
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);
}
}
//SENT
//GET ALL SENT TRANSACTION
@RequestMapping(value = "/get/allSent", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getAllSentTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
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());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
//RECEIVED
//GET ALL RECEIVED TRANSACTION
@RequestMapping(value = "/get/allReceived", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getAllReceivedTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
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());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
}
}
@RequestMapping(value = "/get/latest", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getLatestTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
List<Transaction> listTransaction = transactionImplementation.getLatestTransactions(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);
}
}
@RequestMapping(value = "/getAll", method = RequestMethod.GET, params = {"user_hash"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity getAllTransaction(@RequestParam(value = "user_hash") String user_hash){
TransactionImplementation transactionImplementation = new TransactionImplementation();
try {
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);
}
}
}