Esempio n. 1
0
 public ActionResult <decimal> Exchange([FromQuery, Required] string from,
                                        [FromQuery, Required] string to,
                                        [FromQuery, Required] decimal amount,
                                        [FromQuery] string date)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         decimal exchangeRate;
         if (date != null)
         {
             DateTime parsedDate = ParseISODate(date);
             if (parsedDate > DateTime.Now)
             {
                 return(DateAfterTodayError("date"));
             }
             exchangeRate = Fixer.GetExchangeRate(from, to, parsedDate);
         }
         else
         {
             exchangeRate = Fixer.GetExchangeRate(from, to);
         }
         return(Ok(amount * exchangeRate));
     }
     catch (FormatException)
     {
         return(DateFormatError("date"));
     }
     catch (FixerException ex)
     {
         if (ex.ErrorCode == FixerErrorCode.InvalidCurrency)
         {
             return(BadRequest(ex.Message));
         }
         logger.LogError(ex, "{Controller}.Exchange({From}, {To}, {Amount}, {Date})", nameof(ExchangeRatesController), from, to, amount, date);
         return(StatusCode(502, "Something went wrong."));
     }
 }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            bool retry = true;

            while (retry)
            {
                string  sourceCurrency = PromptForCode("Source currency code: ");
                string  targetCurrency = PromptForCode("Target currency code: ");
                decimal amount         = PromptForAmount("Currency amount: ");
                try
                {
                    decimal exchangeRate;
                    Console.WriteLine();
                    if (QuestionPrompt("Would you like to check for a particular date? "))
                    {
                        exchangeRate = Fixer.GetExchangeRate(sourceCurrency, targetCurrency, PromptForDate("Date: "));
                    }
                    else
                    {
                        exchangeRate = Fixer.GetExchangeRate(sourceCurrency, targetCurrency);
                    }
                    Console.WriteLine("\n" + amount + " " + sourceCurrency + " = " + (amount * exchangeRate) + " " + targetCurrency);
                    retry = false;
                }
                catch (FixerException ex)
                {
                    Console.WriteLine("ERROR: " + ex.Message + "\n");
                    retry = QuestionPrompt("Would you like to try again? ");
                }
                catch (WebException ex)
                {
                    Console.WriteLine("ERROR: Internet connection problem. (" + ex.Message + ")\n");
                    retry = QuestionPrompt("Would you like to try again? ");
                }
            }
        }