Beispiel #1
0
        public async Task <CurrencyRate> LoadRate(int currencyId, string currencyCode, DateTime date)
        {
            using var client = new HttpClient
                  {
                      BaseAddress = new Uri($"{NBU_URL_API}")
                  };
            var response = await client.GetAsync($"statdirectory/exchange?valcode={currencyCode}&date={date.ToString("yyyyMMdd")}&json");

            var result = await response.Content.ReadAsStringAsync();

            var rateResponse = JsonConvert.DeserializeObject <List <RateResponse> >(result);
            var rate         = rateResponse.FirstOrDefault();

            if (rate == null)
            {
                throw new Exception("rate response is empty");
            }
            return(CurrencyRate.CreateRate(currencyId, date, 1, Decimal.Parse(rate.rate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture)));
        }
        public void AddRate(CurrencyRateViewModel rate)
        {
            var currency = currRepository.FindById(rate.CurrencyId);

            if (currency == null)
            {
                throw new ValidationException($"Currency {rate.CurrencyId} not found.");
            }

            var currRate = CurrencyRate.CreateRate(rate.CurrencyId, rate.Date, rate.Koef, rate.Rate);

            try
            {
                unitOfWork.BeginTransaction();
                currRateRepository.Add(currRate);
                unitOfWork.Commit();
            }
            catch (Exception)
            {
                unitOfWork.Rollback();
                throw;
            }
        }