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){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //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){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET LATEST @RequestMapping(value = "/get/latest", method = RequestMethod.GET, params = {"wallet_hash"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getLatestTransaction(@RequestParam(value = "wallet_hash") String wallet_hash){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getLatestTransactions(wallet_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET ALL @RequestMapping(value = "/getTransactions", method = RequestMethod.GET, params = {"wallet_hash","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getWalletTransactions(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getWalletTransactions(wallet_hash,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET ALL WITH DATE @RequestMapping(value = "/getTransactions", method = RequestMethod.GET, params = {"wallet_hash","start_date","end_date","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getWalletTransactionsWithDate(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getAllBetweenDate(wallet_hash,start_date,end_date,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //SENT //GET SENT @RequestMapping(value = "/get/sent", method = RequestMethod.GET, params = {"wallet_hash","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getSentTransaction(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getSentTransaction(wallet_hash,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET SENT WITH DATE @RequestMapping(value = "/get/sent", method = RequestMethod.GET, params = {"wallet_hash","start_date","end_date","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getSentTransactionWithDate(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getSentBetweenDate(wallet_hash,start_date,end_date,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //RECEIVED //GET RECEIVED @RequestMapping(value = "/get/received", method = RequestMethod.GET, params = {"wallet_hash","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getReceivedTransaction(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getReceivedTransaction(wallet_hash,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //GET RECEIVED WITH DATE @RequestMapping(value = "/get/received", method = RequestMethod.GET, params = {"wallet_hash","start_date","end_date","size"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getReceivedTransactionWithDate(@RequestParam(value = "wallet_hash") String wallet_hash, @RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "size") String size){ TransactionImplementation transactionImplementation = new TransactionImplementation(); try { List listTransaction = transactionImplementation.getReceivedBetweenDate(wallet_hash,start_date,end_date,size); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } /* //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> listTransaction = transactionImplementation.getAllSentTransaction(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } //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> listTransaction = transactionImplementation.getAllReceivedTransaction(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } @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 listTransaction = transactionImplementation.getAllTransactions(user_hash); return ResponseEntity.status(HttpStatus.OK).body(listTransaction); } catch (Exception e){ String r = "{\"response\":\""+e.getMessage()+"\"}"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r); } } */ }