Beispiel #1
0
        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;
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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);
        }