Esempio n. 1
0
        public async Task <ActionResult <Product> > GetProductAndUserBasedPrice(int id, int userId)
        {
            var product = dbContext.Products.Find(id);
            var user    = dbContext.Users.Find(userId);

            if (product == null)
            {
                return(NotFound());
            }

            CurrencyRateDto jsonResult = GetExchangeRates();

            if (user.CurrencyCode != "BGN")
            {
                foreach (var rate in jsonResult.Rates)
                {
                    if (rate.Key == user.CurrencyCode)
                    {
                        product.Price = Math.Round(product.Price * rate.Value, 2);
                    }
                }
            }

            return(product);
        }
        private async Task <CurrencyRateDto> GetSingleCurrencyRateAsync(string currencyCode, DateTime date)
        {
            var url = PrepareUrl(currencyCode, date);

            var request = new HttpRequestMessage(HttpMethod.Get, url);

            request.Headers.Add("Accept", "application/vnd.github.v3+json");
            request.Headers.Add("User-Agent", "HttpClientFactory-Sample");

            var client = _clientFactory.CreateClient();

            var response = await client.SendAsync(request);

            CurrencyRateDto currencyRate = null;

            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();

                currencyRate = JsonConvert.DeserializeObject <CurrencyRateDto>(responseString);
            }
            else
            {
                throw new BadRequestException("Not Found - invalid data");
            }

            return(currencyRate);
        }
 public static CurrencyRate MapSingle(CurrencyRateDto dto)
 {
     return(new CurrencyRate(
                dto.Code.ToEnum <CurrencyType>(),
                dto.Rates.First().Bid,
                dto.Rates.First().Ask,
                DateTime.Parse(dto.Rates.First().EffectiveDate)
                ));
 }
Esempio n. 4
0
 public static SapCurrencyRate MapCurrencyRate(CurrencyRateDto currencyRate)
 {
     return(new SapCurrencyRate
     {
         RateDate = currencyRate.Date.AddDays(1).ToString("yyyyMMdd"),
         Rate = Math.Round(currencyRate.SaleValue, 2).ToString(CultureInfo.InvariantCulture),
         //TODO: We can add logic in Sap for this condition
         Currency = currencyRate.CurrencyCode == "ARS" ? "USD" : currencyRate.CurrencyCode.ToUpper()
     });
 }
        public static List <CurrencyRate> MapyMany(CurrencyRateDto dto)
        {
            var rates = new List <CurrencyRate>();

            foreach (var rate in dto.Rates)
            {
                rates.Add(
                    new CurrencyRate(
                        dto.Code.ToEnum <CurrencyType>(),
                        rate.Bid,
                        rate.Ask,
                        DateTime.Parse(rate.EffectiveDate)
                        )
                    );
            }
            return(rates);
        }
        public void ShouldMapFromDtoToEntity()
        {
            var dto = new CurrencyRateDto()
            {
                Code     = "usd",
                Currency = "dolar",
                Rates    = new List <Rate>()
                {
                    new Rate()
                    {
                        Ask           = 3.7800,
                        Bid           = 3.8853,
                        EffectiveDate = "2020-02-01"
                    }
                }
            };

            var entity = CurrencyMapper.MapSingle(dto);

            Assert.NotNull(entity);
        }