diff --git a/cfkserver/src/main/java/com/codefathers/cfkserver/controller/BankController.java b/cfkserver/src/main/java/com/codefathers/cfkserver/controller/BankController.java index 5fa87d9..3a51319 100644 --- a/cfkserver/src/main/java/com/codefathers/cfkserver/controller/BankController.java +++ b/cfkserver/src/main/java/com/codefathers/cfkserver/controller/BankController.java @@ -118,15 +118,19 @@ private void pay(HttpServletRequest request, HttpServletResponse response, } } - @PostMapping("/bank/get_balance") - private ResponseEntity getBalance(HttpServletRequest request, HttpServletResponse response, - @RequestBody BalanceDTO dto) { + @GetMapping("/bank/get_balance/{username}") + private Long getBalance(HttpServletRequest request, HttpServletResponse response, + @PathVariable String username) { try { if (checkToken(response, request)) { try { - return ResponseEntity.ok(bankService.getBalance(dto)); + String password = userService.getPassByUsername(username); + String token = bankService.getToken(new TokenRequestDTO(username, password)); + return bankService.getBalance(new BalanceDTO(username, password, token)); } catch (IOException | InvalidUsernameException e) { sendError(response, HttpStatus.BAD_REQUEST, e.getMessage()); + } catch (UserNotFoundException e) { + e.printStackTrace(); } } } catch (ExpiredTokenException | InvalidTokenException e) { diff --git a/cfkserver/src/main/java/com/codefathers/cfkserver/service/UserService.java b/cfkserver/src/main/java/com/codefathers/cfkserver/service/UserService.java index cde6373..d81788d 100644 --- a/cfkserver/src/main/java/com/codefathers/cfkserver/service/UserService.java +++ b/cfkserver/src/main/java/com/codefathers/cfkserver/service/UserService.java @@ -25,6 +25,7 @@ import java.util.Optional; import java.util.Scanner; +import static com.codefathers.cfkserver.model.dtos.bank.ReceiptType.DEPOSIT; import static com.codefathers.cfkserver.model.dtos.bank.ReceiptType.MOVE; import static com.codefathers.cfkserver.model.entities.user.Role.CUSTOMER; @@ -147,7 +148,7 @@ private void chargeBankAccount(String username, String password, long balance, S } public void createManager(ManagerDTO managerDTO) throws UserAlreadyExistsException, InvalidUsernameException, - IOException, PasswordsDoNotMatchException { + IOException, PasswordsDoNotMatchException, UserNotFoundException, PaidReceiptException, InvalidDestAccountException, InvalidTokenException, InvalidSourceAccountException, InvalidAccountIdException, InvalidMoneyException, NotEnoughMoneyAtSourceException, InvalidDescriptionExcxeption, InvalidParameterPassedException, InvalidRecieptTypeException, InvalidReceiptIdException, ExpiredTokenException, EqualSourceDestException { checkUsername(managerDTO.getUsername()); String accountId; @@ -160,6 +161,7 @@ public void createManager(ManagerDTO managerDTO) throws UserAlreadyExistsExcepti managerDTO.getPassword()) ); saveToFile(accountId); + chargeShop(managerDTO, accountId); } else { accountId = bankService.getInfo("AccountId"); } @@ -177,6 +179,22 @@ public void createManager(ManagerDTO managerDTO) throws UserAlreadyExistsExcepti managerRepository.save(manager); } + private void chargeShop(ManagerDTO managerDTO, String accountId) throws IOException, InvalidRecieptTypeException, InvalidMoneyException, InvalidParameterPassedException, InvalidTokenException, ExpiredTokenException, InvalidSourceAccountException, InvalidDestAccountException, EqualSourceDestException, InvalidAccountIdException, InvalidDescriptionExcxeption, InvalidUsernameException, UserNotFoundException, InvalidReceiptIdException, PaidReceiptException, NotEnoughMoneyAtSourceException { + String token = bankService.getToken(new TokenRequestDTO(managerDTO.getUsername(), managerDTO.getPassword())); + int receiptId = bankService.createReceipt( + new CreateReceiptDTO( + managerDTO.getUsername(), + managerDTO.getPassword(), + token, + DEPOSIT, + 10000, + "-1", + accountId, + "Charge" + )); + bankService.pay(receiptId); + } + private void saveToFile(String accountId) { File file = new File("cfkserver/src/main/resources/application_info.txt"); Scanner scanner; @@ -187,7 +205,7 @@ private void saveToFile(String accountId) { String fileLine = scanner.nextLine(); if (fileLine.startsWith("AccountId")) { String changed = fileLine.replaceFirst(fileLine.substring(fileLine.indexOf('=') + 2), accountId); - info.append(changed); + info.append(changed + "\n"); } else { info.append(fileLine).append("\n"); } diff --git a/cfkserver/src/main/java/com/codefathers/cfkserver/utils/BankUtil.java b/cfkserver/src/main/java/com/codefathers/cfkserver/utils/BankUtil.java index 628d35f..3970ccb 100644 --- a/cfkserver/src/main/java/com/codefathers/cfkserver/utils/BankUtil.java +++ b/cfkserver/src/main/java/com/codefathers/cfkserver/utils/BankUtil.java @@ -55,8 +55,11 @@ private void startListeningOnInput() { public void sendMessage(String msg) throws IOException { try { outputStream.writeUTF(msg); + Thread.sleep(100); } catch (IOException e) { throw new IOException("Exception while sending message:"); + } catch (InterruptedException e) { + e.printStackTrace(); } } diff --git a/client/src/main/java/com/codefathers/cfkclient/controllers/CustomerAccount.java b/client/src/main/java/com/codefathers/cfkclient/controllers/CustomerAccount.java index 544ae61..9d4b4d6 100644 --- a/client/src/main/java/com/codefathers/cfkclient/controllers/CustomerAccount.java +++ b/client/src/main/java/com/codefathers/cfkclient/controllers/CustomerAccount.java @@ -106,8 +106,7 @@ private void setLabels() { phone.setText(userFullDTO.getPhoneNumber()); wallet.setText(String.valueOf(userFullDTO.getBalance())); try { - balance.setText(String.valueOf(connector.getBalance( - new BalanceDTO(userFullDTO.getUsername(), userFullDTO.getPassword())))); + balance.setText(String.valueOf(connector.getBalance(cacheData.getUsername()))); } catch (Exception e) { Notification.show("Error", e.getMessage(), back.getScene().getWindow(), true); e.printStackTrace(); diff --git a/client/src/main/java/com/codefathers/cfkclient/controllers/ManagerAccount.java b/client/src/main/java/com/codefathers/cfkclient/controllers/ManagerAccount.java index a108fdc..e1e50be 100644 --- a/client/src/main/java/com/codefathers/cfkclient/controllers/ManagerAccount.java +++ b/client/src/main/java/com/codefathers/cfkclient/controllers/ManagerAccount.java @@ -401,7 +401,7 @@ private void initBudgetLabels(InfoDTO infoDTO) throws Exception { private String calculateTotalBalance() throws Exception { long totalBalance = 0; - totalBalance = connector.getBalance(new BalanceDTO(cacheData.getUsername(), userFullPM.getPassword())); + totalBalance = connector.getBalance(cacheData.getUsername()); return Long.toString(totalBalance); } diff --git a/client/src/main/java/com/codefathers/cfkclient/controllers/SellerAccount.java b/client/src/main/java/com/codefathers/cfkclient/controllers/SellerAccount.java index ce0da7e..a73208d 100644 --- a/client/src/main/java/com/codefathers/cfkclient/controllers/SellerAccount.java +++ b/client/src/main/java/com/codefathers/cfkclient/controllers/SellerAccount.java @@ -142,8 +142,7 @@ private void setLabels() { phone.setText(userFullDTO.getPhoneNumber()); wallet.setText(String.valueOf(userFullDTO.getBalance())); try { - balance.setText(String.valueOf(connector.getBalance( - new BalanceDTO(userFullDTO.getUsername(), userFullDTO.getPassword())))); + balance.setText(String.valueOf(connector.getBalance(cacheData.getUsername()))); } catch (Exception e) { Notification.show("Error", e.getMessage(), back.getScene().getWindow(), true); e.printStackTrace(); diff --git a/client/src/main/java/com/codefathers/cfkclient/utils/Connector.java b/client/src/main/java/com/codefathers/cfkclient/utils/Connector.java index 8f5610d..5567495 100644 --- a/client/src/main/java/com/codefathers/cfkclient/utils/Connector.java +++ b/client/src/main/java/com/codefathers/cfkclient/utils/Connector.java @@ -486,11 +486,9 @@ public void pay(String receiptId) throws Exception { post("http://127.0.0.1:8050/bank/pay", receiptId, String.class); } - public long getBalance(BalanceDTO dto) throws Exception { - dto.setToken(bankToken); - ResponseEntity response = post(address + "/bank/get_balance", - dto, Long.class); - return response.getBody(); + public long getBalance(String username) throws Exception { + return get(address + "/bank/get_balance/" + username, + null, Long.class); } public void createSupport(UserDTO userDTO) throws Exception {