public async Task <ConvertedCurrencyDto> Handle(CurrencyExchangeRequest request, CancellationToken cancellationToken) { // Note: Applied robustness principle in this case. // Another option is to only accepts requests with exact API contract try { var exchangeRate = await _exchangeRateService.GetRate(request.SourceCurrency, request.TargetCurrency); var result = new ConvertedCurrencyDto { ConvertedAmount = request.Amount * exchangeRate, TargetCurrency = request.TargetCurrency }; return(result); } catch (Exception e) { _logger.LogError(_logFormatter.FormatMessage(LogType.Error, ""), e); throw; } }
public IOrder ConvertToAnotherCurrency(IOrder order, string currencyCode) { var newOrder = _factory.Create(); foreach (var item in order.GetItems()) { var newItem = new Item(item.Article, item.Count * (int)_exchangeRateService.GetRate("CZK")); newOrder.AddItem(newItem); } return(newOrder); }
private async Task HandleGetExchangeRateRequest(string userId, string currencyFrom, string currencyTo) { decimal rate = 0; try { rate = await _service.GetRate(currencyFrom, currencyTo); } catch (Exception e) { _logger.Error(e, "Error while try to get exchange rate"); var serviceUnawailableMessage = _helpCommandTextBuilder.BuildErrorMessage(e); await _provider.SendMessage(userId, serviceUnawailableMessage); throw; } string response = $"1 {currencyFrom} = {rate} {currencyTo} {UserCommands.GetHelp}"; await _provider.SendMessage(userId, response); }