/// <inheritdoc/> public async Task <ProductReport <ProductLocalPrice> > GetCurrentProductsWithLocalCurrencyReport(ICountryCurrencyService countryCurrencyService, ICurrencyExchangeService currencyExchangeService) { var products = await this.GetAllProducts(); var suppliers = await this.GetAllSupplies(); var productsWithLocalPrices = products.Select(p => new ProductLocalPrice { Name = p.ProductName, Price = p.UnitPrice ?? 0, Country = suppliers.Where(s => s.SupplierID == p.SupplierID).Select(s => s.Country).First(), }).ToArray(); for (int i = 0; i < productsWithLocalPrices.Length; i++) { var countryInfo = await countryCurrencyService.GetLocalCurrencyByCountry(productsWithLocalPrices[i].Country); var currencyExchangeRate = await currencyExchangeService.GetCurrencyExchangeRate("USD", countryInfo.CurrencyCode); productsWithLocalPrices[i].Country = countryInfo.CountryName; productsWithLocalPrices[i].CurrencySymbol = countryInfo.CurrencySymbol; productsWithLocalPrices[i].LocalPrice = productsWithLocalPrices[i].Price * currencyExchangeRate; } return(new ProductReport <ProductLocalPrice>(productsWithLocalPrices)); }
public async Task<ProductReport<ProductLocalPrice>> GetCurrentProductsWithLocalCurrencyReport(ICountryCurrencyService countryCurrencyService, ICurrencyExchangeService currencyExchangeService) { var query = (DataServiceQuery<ProductLocalPrice>)( from p in this.entities.Products where !p.Discontinued select new ProductLocalPrice { Name = p.ProductName, Price = p.UnitPrice ?? 0, Country = p.Supplier.Country, LocalPrice = currencyExchangeService.GetCurrencyExchangeRate("USD", countryCurrencyService.GetLocalCurrencyByCountry(p.Supplier.Country).Result.CurrencyCode).Result * (p.UnitPrice ?? 0), CurrencySymbol = countryCurrencyService.GetLocalCurrencyByCountry(p.Supplier.Country).Result.CurrencySymbol, }) ; return await this.GetAllProductReport(query).ConfigureAwait(false); }
/// <summary> /// Gets a product report with local country information. /// </summary> /// <param name="countryCurrencyService">Service to get country info.</param> /// <param name="currencyExchangeService">Service to get currency info.</param> /// <returns>Returns <see cref="ProductReport{ProductLocalPrice}"/>.</returns> public async Task <ProductReport <ProductLocalPrice> > GetCurrentProductsWithLocalCurrencyReport(ICountryCurrencyService countryCurrencyService, ICurrencyExchangeService currencyExchangeService) { if (countryCurrencyService is null) { throw new ArgumentNullException(nameof(countryCurrencyService)); } if (currencyExchangeService is null) { throw new ArgumentNullException(nameof(currencyExchangeService)); } var query = (DataServiceQuery <ProductLocalPrice>) this.entities.Products. Where(p => p.UnitPrice != null). OrderBy(p => p.ProductName). Select(p => new ProductLocalPrice { Name = p.ProductName, Price = p.UnitPrice ?? 0, Country = p.Supplier.Country, }); var localPrices = await Task <ProductLocalPrice[]> .Factory.FromAsync(query.BeginExecute(null, null), (ar) => { return(query.EndExecute(ar).ToArray()); }); for (int i = 0; i < localPrices.Length; i++) { var countryInfo = await countryCurrencyService.GetLocalCurrencyByCountry(localPrices[i].Country); var currencyExchange = await currencyExchangeService.GetCurrencyExchangeRate("USD", countryInfo.CurrencyCode); localPrices[i].Country = countryInfo.CountryName; localPrices[i].LocalPrice = localPrices[i].Price * currencyExchange; localPrices[i].CurrencySymbol = countryInfo.CurrencySymbol; } return(new ProductReport <ProductLocalPrice>(localPrices)); }