public RateHistoryDomainModel GetRateHistory(string currencyCode, string from, string to)
        {
            RateHistoryDomainModel result = default;

            try
            {
                if (string.IsNullOrEmpty(currencyCode) || string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to))
                {
                    throw new Exception("Invalid request data");
                }
                //TODO RegEx change to be done
                var dateRegEx = new Regex(@"^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$");

                DateTime outFrom = DateTime.Now;
                DateTime outTo   = DateTime.Now;
                if (_fixerService.CheckCurrencyCodeExists(currencyCode) && DateTime.TryParse(from, out outFrom) && DateTime.TryParse(to, out outTo) && outTo >= outFrom)
                {
                    var rateList = _unitOfWork.ExchangeRateSyncBaseRepository.Get(x => x.IsDeleted == false && x.SyncDate >= outFrom && x.SyncDate <= outTo)
                                   .Join(_unitOfWork.ExchangeRatesRepository.Get(x => x.IsDeleted == false && x.Currency == currencyCode), sync => sync.Id, rate => rate.SyncId, (sync, rate) => new { sync, rate })
                                   .Select(x => new RateDomainModel
                    {
                        RecordDate   = x.sync.SyncDate,
                        ExchangeRate = x.rate.Rate
                    }).OrderBy(x => x.RecordDate).ToList();
                    if (rateList.Any())
                    {
                        result = result ?? new RateHistoryDomainModel();
                        result.CurrencyCode = currencyCode.ToUpper();
                        result.FromDate     = outFrom;
                        result.ToDate       = outTo;
                        result.Records      = rateList;
                    }
                }
                else
                {
                    throw new Exception("Invalid request data");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                var           container     = GetIocContainer();
                IFixerService _fixerService = container.Resolve <IFixerService>();

                Console.Title = "FOREX - CURRENCY CONVERSION";

QUESTION:
                Console.Clear();
                Console.Write("Want to get currency conversion with current(Y) or past-dated(N) rate?(Y/N): ");
                var      answar1 = Console.ReadLine().Trim().ToUpper();
                DateTime?date    = null;
                if (answar1 == "Y")
                {
                    goto START;
                }
                else if (answar1 == "N")
                {
                    Console.Write("Please enter date in YYYY-MM-DD format: ");
                    var      input = Console.ReadLine().Trim();
                    DateTime outDate;
                    if (DateTime.TryParse(input, out outDate))
                    {
                        date = outDate;
                    }
                    else
                    {
                        Console.Write("Invalid date entered!");
                        Console.ReadKey();
                        goto QUESTION;
                    }
                }
                else
                {
                    goto QUESTION;
                }

START:
                Console.Write("Please enter first currency code: ");
                var firstCurrencyCode = Console.ReadLine().ToUpper().Trim();
                if (!_fixerService.CheckCurrencyCodeExists(firstCurrencyCode))
                {
                    Console.Write("Invalid currency code entered!");
                    Console.ReadKey();
                    goto QUESTION;
                }

                Console.Write("Please enter curency amount to exchange: ");
                var   i      = Console.ReadLine();
                float amount = 0F;
                if (!float.TryParse(i, out amount) && amount < 0)
                {
                    Console.Write("Invalid amount entered!");
                    Console.ReadKey();
                    goto QUESTION;
                }

                Console.Write("Please enter second currency code: ");
                var secondCurrencyCode = Console.ReadLine().ToUpper().Trim();
                if (!_fixerService.CheckCurrencyCodeExists(secondCurrencyCode))
                {
                    Console.Write("Invalid currency code entered!");
                    Console.ReadKey();
                    goto QUESTION;
                }

                var ExchangedAmount = _fixerService.CurrencyConversion(firstCurrencyCode, secondCurrencyCode, amount, date);
                Console.WriteLine($"{amount} {firstCurrencyCode} = {ExchangedAmount} {secondCurrencyCode}");

CLOSE:
                Console.Write("Want to close application?(Y/N): ");
                var answar = Console.ReadLine().ToUpper();
                if (answar == "Y")
                {
                    Environment.Exit(0);
                }
                else if (answar == "N")
                {
                    goto QUESTION;
                }
                else
                {
                    goto CLOSE;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal error: " + ex.Message);
                Console.ReadKey();
            }
        }