public void ScenarioB() { PromotionController promotionController = new PromotionController(); PromotionRequestModel request = new PromotionRequestModel(); PromotionResponseModel response = new PromotionResponseModel(); request.PromotionId = 1; request.CartItems.Add( new CartItemModel { ItemName = "A", ItemQty = 5 }); request.CartItems.Add(new CartItemModel { ItemName = "B", ItemQty = 5 }); request.CartItems.Add(new CartItemModel { ItemName = "C", ItemQty = 1 }); response = promotionController.ApplyPromotion(request); Assert.AreEqual(370, response.TotalCost); }
public PromotionResponseModel ApplyPromotion(PromotionRequestModel request) { PromotionResponseModel response = new PromotionResponseModel(); PromotionBusiness promotionBusiness = new PromotionBusiness(); response.TotalCost = 0; //Retrieve SKU Promotion data from database response.PromotionApplied = promotionBusiness.GetPromotion(request.PromotionId); using (PromotionEngineEntities query = new PromotionEngineEntities()) { //Retrieve SKU price from databse var dbItems = (from cartItems in request.CartItems join items in query.Items on cartItems.ItemName equals items.ItemName select new { cartItems.ItemQty, items.ItemName, items.ItemPrice }).ToList(); foreach (FormulaResponseModel formula in response.PromotionApplied.PromotionFormulas) { string[] formulaItemNames = formula.ItemNames.Split('&'); //Logic to calculate the purchase of 'n' items of a SKU for a fixed price if (formulaItemNames.Length > 0 && formulaItemNames.Length == 1) { foreach (var dbitem in dbItems) { if (dbitem.ItemName == formulaItemNames.Single()) { response.TotalCost += (((dbitem.ItemQty - (dbitem.ItemQty % formula.ItemQty)) / (formula.ItemQty)) * formula.PromotionValue) + ((dbitem.ItemQty % formula.ItemQty) * dbitem.ItemPrice); } } } //Logic to calculate the purchase of SKU 1 & SKU 2 ,.SKU 'n' for a fixed price else if (formulaItemNames.Length > 1) { var filteredDBItems = (from filtereddbItems in dbItems join filter in formulaItemNames on filtereddbItems.ItemName equals filter select new { filtereddbItems.ItemQty, filtereddbItems.ItemName, filtereddbItems.ItemPrice }).ToList(); if (formulaItemNames.Length == filteredDBItems.Count()) { int itemsFixedPrice = filteredDBItems.Min(q => q.ItemQty); response.TotalCost += itemsFixedPrice * formula.PromotionValue; foreach (var item in filteredDBItems) { response.TotalCost += ((item.ItemQty - itemsFixedPrice) * item.ItemPrice); } } else { foreach (var item in filteredDBItems) { response.TotalCost += (item.ItemQty * item.ItemPrice); } } } } } return(response); }