public async Task <ShopperHistory> GetShopperHistoryAsync() { var url = $"{configuration.WooliesXBaseUrl}/api/resource/shopperHistory?token={configuration.Token}"; var request = new HttpRequestMessage(HttpMethod.Get, url); var client = clientFactory.CreateClient(); try { var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var orders = await response.Content.ReadAsAsync <IEnumerable <Order> >(); var history = new ShopperHistory { Orders = orders }; return(history); } else { throw new ProxyCallException(url, "Invalid response from server"); } } catch (Exception ex) { Console.WriteLine(ex); // TODO: Add logging throw new ProxyCallException(url, "Service call failure", ex); } }
public void Can_Calculate_Product_Popularity() { var orders = CreateOrders(); var popularity = new ShopperHistory(orders).CalculatePopularity(); Assert.Equal(6, popularity["ProductA"].QuantitySold); Assert.Equal(5, popularity["ProductB"].QuantitySold); Assert.Equal(7, popularity["ProductC"].QuantitySold); }
public void Can_Sort_Products_By_Popularity() { var history = new ShopperHistory(CreateOrders()); var products = CreateProducts(); var sortedProducts = history.SortProductsByPopularity(products).ToList(); Assert.Equal("ProductC", sortedProducts[0].Name); Assert.Equal("ProductA", sortedProducts[1].Name); Assert.Equal("ProductB", sortedProducts[2].Name); }
public IEnumerable <ProductCustomerAssociation> CalculateProductAssociations(ShopperHistory history) { var associations = new Dictionary <string, ProductCustomerAssociation>(); foreach (var order in history.Orders) { foreach (var product in order.Products) { if (associations.ContainsKey(product.Name)) { associations[product.Name].AddAssociation(product, order); } else { associations.Add(product.Name, new ProductCustomerAssociation(product, order)); } } } return(associations.Values); }
private void SetupEqualPopularityShoppingHistory() { var shopperHistory1 = new ShopperHistory() { CustomerId = 1, Products = new List <Product>() { new Product() { Name = "testC", Quantity = 1 } } }; var shopperHistory2 = new ShopperHistory() { CustomerId = 2, Products = new List <Product>() { new Product() { Name = "testB", Quantity = 1 } } }; var shopperHistory3 = new ShopperHistory() { CustomerId = 3, Products = new List <Product>() { new Product() { Name = "testA", Quantity = 1 } } }; var equalPopularityShoppingHistory = new List <ShopperHistory>() { shopperHistory1, shopperHistory2, shopperHistory3 }; _resourceApiMock.Setup(s => s.GetShopperHistory()).ReturnsAsync(equalPopularityShoppingHistory); }