public async Task <decimal> GetLowestPrice(CustomerTrolleyRequest request) { try { var trolleyCalculatorUrl = configuration.GetValue <string>("ApplicationData:Resources:TrolleyCalculatorUrl") + configuration.GetValue <string>("ApplicationData:AppToken"); StringContent stringContent = new StringContent(JsonConvert.SerializeObject(request), UnicodeEncoding.UTF8, "application/json"); var response = await httpClient.PostAsync(trolleyCalculatorUrl, stringContent); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return(Convert.ToDecimal(result)); } else { throw new WebException(JsonConvert.SerializeObject(response)); } } catch (WebException ex) { logger.LogError(ex.StackTrace); throw; } catch (Exception ex) { logger.LogError(ex.StackTrace); throw; } }
//custom implementation of the TrolleyCalculator (all Scenarios working) public async Task <decimal> GetLowestTrolleyTotalAsync3(CustomerTrolleyRequest request) { try { logger.LogInformation($"Initiating the calculation for TrolleyRequest: {JsonConvert.SerializeObject(request, Formatting.Indented)}"); //Prepare dictionaries. var productDictionary = new Dictionary <string, decimal>(); var quantityDictionary = new Dictionary <string, decimal>(); foreach (var product in request.Products) { productDictionary.Add(product.Name, product.Price); } foreach (var quantity in request.Quantities) { quantityDictionary.Add(quantity.Name, quantity.Quantity); } return(findCartValue(productDictionary, quantityDictionary, request.Specials)); } catch (Exception) { throw; } }
public async Task <decimal> GetLowestTrolleyTotalAsync(CustomerTrolleyRequest request) { logger.LogInformation($"Initiating the calculation for TrolleyRequest: {JsonConvert.SerializeObject(request, Formatting.Indented)}"); var lowestCost = await serviceAPIRepository.GetLowestPrice(request); return(lowestCost); }
public async Task GetOrderedProductListAysnc_Scenario2_ShouldReturn_LeastTrolleyCost(int qtyA, int qtyB, decimal expectedOutput) { var request = new CustomerTrolleyRequest(); request.Products = new List <TrolleyProduct> { new TrolleyProduct { Name = "A", Price = 10 }, new TrolleyProduct { Name = "B", Price = 10 } }; request.Specials = new List <Special> { new Special { Quantities = new List <ProductQuantities> { new ProductQuantities { Name = "A", Quantity = qtyA }, new ProductQuantities { Name = "B", Quantity = 0 } }, Total = 10 }, new Special { Quantities = new List <ProductQuantities> { new ProductQuantities { Name = "A", Quantity = 0 }, new ProductQuantities { Name = "B", Quantity = qtyB } }, Total = 15 } }; request.Quantities = new List <ProductQuantities> { new ProductQuantities { Name = "A", Quantity = qtyA }, new ProductQuantities { Name = "B", Quantity = qtyB } }; var result = await ShoppingService.GetLowestTrolleyTotalAsync(request); Assert.That(result == expectedOutput); }
public async Task <IActionResult> CustomTrolleyCalculator([FromBody] CustomerTrolleyRequest request) { var result = await service.GetLowestTrolleyTotalAsync3(request); return(Ok(result)); }
//Custom Implementation of the TrolleyCalculator for split scenario. /// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public async Task <decimal> GetLowestTrolleyTotalAsync2(CustomerTrolleyRequest request) { try { logger.LogInformation("In the GetLowestTrolleyTotalAsync() method for Request:" + request); decimal lowestCost = 0, totalCost = 0; var productList = request.Products; var specials = request.Specials; var productQuantities = request.Quantities; //calculate totalPrice before specials. foreach (var productQuantity in request.Quantities) { //get the Price from Products List. var itemPrice = productList.FirstOrDefault(x => x.Name == productQuantity.Name).Price; totalCost += itemPrice * productQuantity.Quantity; } foreach (var specialProducts in specials) { if (ProductsExistInSpecials(productList, specialProducts.Quantities)) { foreach (var splQuantity in specialProducts.Quantities) { if (productQuantities.Where(x => x.Name == splQuantity.Name).Any()) { var productQty = productQuantities.Where(x => x.Name == splQuantity.Name).FirstOrDefault().Quantity; if (productQty >= splQuantity.Quantity) { specialProducts.IsValid = true; } else { specialProducts.IsValid = false; } } } } } bool specialsQtyEqualsProdQty = false; //check if any of the specials are valid. if (specials.Where(x => x.IsValid == true).Count() > 0) { //Take the least applicable total for the Specials. var applicableSpecial = specials.Where(x => x.IsValid == true).OrderBy(x => x.Total).FirstOrDefault(); foreach (var item in applicableSpecial.Quantities) { if (productQuantities.Where(x => x.Name == item.Name).Any()) { var productQty = productQuantities.Where(x => x.Name == item.Name).FirstOrDefault().Quantity; if (productQty > item.Quantity) { var diffQty = productQty - item.Quantity; //get the price of the product item. var productPrice = productList.Where(x => x.Name == item.Name).FirstOrDefault().Price; var costOfProduct = diffQty * productPrice; lowestCost += costOfProduct; } else { if (productQty == item.Quantity) { specialsQtyEqualsProdQty = true; } } } } //if the product quantity is equal to specials quantity; just the specials price. if (specialsQtyEqualsProdQty) { lowestCost = applicableSpecial.Total; } else { // lowestCost = await serviceAPIRepository.GetLowestPrice(request); return(lowestCost); } } else { //no valid specials. lowestCost = totalCost; } return(lowestCost); } catch (Exception ex) { logger.LogError(ex.StackTrace); throw ex; } }