Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
2
Indexable
Never
// klasa EchangeRateService

@Service
public class ExchangeRateServiceImpl implements ExchangeRateService {

    private static final String NBP_API_TABLE= "http://api.nbp.pl/api/exchangerates/tables/a?format=json";

    @Override
    public List<String> getCurrencies() {

        List<String> currencies;

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ExchangeRateDtoTable[]> forEntity = restTemplate.getForEntity(NBP_API_TABLE, ExchangeRateDtoTable[].class);

        ExchangeRateDtoTable[] body = forEntity.getBody();

        currencies = Arrays.stream(body)
                .map(ExchangeRateDtoTable::getExchangeRateDtoList)
                .flatMap(e -> e.stream())
                .map(e -> e.getCode())
                .collect(Collectors.toList());

        Collections.sort(currencies);

        return currencies;
    }

    @Override
    public BigDecimal getCurrentExchangeRate(String currency) {

        BigDecimal currentExchangeRate;

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ExchangeRateDtoTable[]> forEntity = restTemplate.getForEntity(NBP_API_TABLE, ExchangeRateDtoTable[].class);

        ExchangeRateDtoTable[] body = forEntity.getBody();

        currentExchangeRate = Arrays.stream(body)
                .map(ExchangeRateDtoTable::getExchangeRateDtoList)
                .flatMap(e -> e.stream())
                .filter(e -> e.getCode().equals(currency))
                .map(ExchangeRateDto::getCurrentExchangeRate)
                .findFirst()
                .get();

        currentExchangeRate = currentExchangeRate.setScale(2, RoundingMode.HALF_UP);

        return currentExchangeRate;
    }
}

// czesc kontrolera wywolujacego metode 1. serwisu

@Controller
@AllArgsConstructor
public class AccountController {

    private final AccountService accountService;
    private final UserServiceImpl userService;
    private final ExchangeRateServiceImpl exchangeRateService;

    @ModelAttribute("currencyList")
    List<String> currencyList() {

        List<String> currencyList = exchangeRateService.getCurrencies();

        return currencyList;
    }

// czesc kontrolera wywolujacego 2. metode serwisu

 @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";
    }