コード例 #1
0
        public ActionResult <double> Get(string code1, string code2, int num)
        {
            if (code1.Length != 3)
            {
                return(BadRequest($"400 Error (Bad Request): Currency Code \"{code1}\" not valid."));
            }
            if (code2.Length != 3)
            {
                return(BadRequest($"400 Error (Bad Request): Currency Code \"{code2}\" not valid."));
            }
            if (num < 0 || num > 15)
            {
                return(BadRequest("400 Error (Bad Request): Precision must be from 0 to 15."));
            }
            code1 = code1.ToUpper();
            code2 = code2.ToUpper();
            var currencies = CurrencyService.GetCurrencies();

            if (currencies.ContainsKey(code1) && currencies.ContainsKey(code2))
            {
                var    currency1 = currencies[code1];
                var    currency2 = currencies[code2];
                double exchange  = CurrencyService.ExchangeRate(currency1, currency2);
                if (exchange == 0)
                {
                    return(NotFound("404 Error (Not Found): Currency Conversion Rate could not be found."));
                }
                exchange = Math.Round(exchange, num);
                return(exchange);
            }
            else if (currencies.ContainsKey(code1) && !currencies.ContainsKey(code2))
            {
                return(NotFound($"404 Error (Not Found): Currency Code \"{code2}\" could not be found."));
            }
            else if (!currencies.ContainsKey(code1) && currencies.ContainsKey(code2))
            {
                return(NotFound($"404 Error (Not Found): Currency Code \"{code1}\" could not be found."));
            }
            else
            {
                return(NotFound($"404 Error (Not Found): Currency Code \"{code1}\" & \"{code2}\" could not be found."));
            }
        }