Example #1
0
        public async Task convertCurrencyAsync_ShouldConvertBetweenCurrencies_WhenBothAndAmountValid()
        {
            Currency brl = new Currency()
            {
                name = "BRL", rate = 0.5M
            };
            Currency eur = new Currency()
            {
                name = "EUR", rate = 2M
            };

            _cache.Setup(x => x.GetAsync(brl.name)).ReturnsAsync(brl.rate.ToString());
            _cache.Setup(x => x.GetAsync(eur.name)).ReturnsAsync(eur.rate.ToString());

            decimal amount = 1;
            var     result = await _sut.convertCurrencyAsync(brl.name, eur.name, amount);

            var fromAmount = amount * brl.rate;
            var toAmount   = fromAmount / eur.rate;

            Assert.Equal(toAmount, result);
        }
Example #2
0
        public async Task <IActionResult> Converter([FromQuery] string from, [FromQuery] string to, [FromQuery] string amount)
        {
            try
            {
                //Logger removed to maximize performance
                if (from == null)
                {
                    return(new BadRequestObjectResult(new { Error = "variable 'from' not found or empty" }));
                }

                if (to == null)
                {
                    return(new BadRequestObjectResult(new { Error = "variable 'to' not found or empty" }));
                }

                if (amount == null)
                {
                    return(new BadRequestObjectResult(new { Error = "variable 'amount' not found" }));
                }

                decimal amountDecimal = 0;
                if (!decimal.TryParse(amount, NumberStyles.AllowDecimalPoint, new NumberFormatInfo()
                {
                    NumberDecimalSeparator = ","
                }, out amountDecimal))
                {
                    return(new BadRequestObjectResult(new { Error = "variable 'amount' doesn't have valid value (must be positive and use ',' as decimal separator" }));
                }

                decimal result = await _converterSrvc.convertCurrencyAsync(from.ToUpper(), to.ToUpper(), amountDecimal);

                return(new OkObjectResult(result));
            }
            catch (Exception)
            {
                return(new BadRequestObjectResult(new { Error = "Currency not found" }));
            }
        }