Skip to content

Commit

Permalink
bank errors fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mofayezi committed Jul 24, 2020
1 parent 7d41756 commit 4b04d69
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -160,6 +161,7 @@ public void createManager(ManagerDTO managerDTO) throws UserAlreadyExistsExcepti
managerDTO.getPassword())
);
saveToFile(accountId);
chargeShop(managerDTO, accountId);
} else {
accountId = bankService.getInfo("AccountId");
}
Expand All @@ -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;
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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 {
Expand Down

0 comments on commit 4b04d69

Please sign in to comment.