public async Task <GetExchangeRatesQueryResult> Handle(GetExchangeRatesQuery request, CancellationToken cancellationToken) { var eurRate = await _currencyExchangeAdapter.GetExchangeRate(AccountCurrency.UAH, AccountCurrency.EUR); var usdRate = await _currencyExchangeAdapter.GetExchangeRate(AccountCurrency.UAH, AccountCurrency.USD); return(new GetExchangeRatesQueryResult { Rates = new CurrencyRateDto[] { new CurrencyRateDto { Currency = AccountCurrency.EUR.ToString(), Rate = eurRate.Value }, new CurrencyRateDto { Currency = AccountCurrency.USD.ToString(), Rate = usdRate.Value } } }); }
public async Task <BuyCurrencyResult> Handle(BuyCurrencyCommand request, CancellationToken cancellationToken) { var baseAccount = await _accountRepository.FindAsync(request.AccountId); var currencyAccount = await _accountRepository.FindAsync(request.CurrencyAccountId); var exchangeBaseAccount = SystemAccount.FromCurrency(baseAccount.Currency); var exchangeCurrencyAccount = SystemAccount.FromCurrency(currencyAccount.Currency); var exchangeRate = await _currencyExchangeAdapter.GetExchangeRate(currencyAccount.Currency, baseAccount.Currency); var baseAmount = request.Amount * exchangeRate; var debitBaseAccount = baseAccount.Transfer(exchangeBaseAccount, baseAmount.Value, $"Buy {request.Amount} {currencyAccount.Currency.ToString()} operation"); var creditCurrencyAccount = exchangeCurrencyAccount.Transfer(currencyAccount, request.Amount, $"Buy {request.Amount} {currencyAccount.Currency.ToString()} operation"); await _accountTransactionRepository.SaveAsync(debitBaseAccount); await _accountTransactionRepository.SaveAsync(creditCurrencyAccount); return(new BuyCurrencyResult()); }