Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
3
Indexable
@PostMapping("/transfer/make")
    String processMakeTransferForm(@Valid Transfer transfer, BindingResult result) {

        Account fromAccount = transfer.getFromAccount();
        Account toAccount = transfer.getToAccount();

        LocalDateTime transferDate = LocalDateTime.now(ZoneId.of("Europe/Warsaw"));

        if (result.hasErrors()) {

            return "transfer/make";
        }

        transfer.setTransferDate(transferDate);

        String fromCurrency = fromAccount.getCurrency();
        String toCurrency = toAccount.getCurrency();

        BigDecimal originalAmount = transfer.getOriginalAmount();
        BigDecimal finalAmount = transferService.calculateFinalAmount(originalAmount, fromCurrency, toCurrency);

        transfer.setFinalAmount(finalAmount);
        transferService.save(transfer);

        fromAccount.setBalance(fromAccount.getBalance().subtract(originalAmount));
        toAccount.setBalance(toAccount.getBalance().add(finalAmount));
        accountService.update(fromAccount);
        accountService.update(toAccount);

        return "redirect:/account/list";
    }