public JsonResult AddItemToCart(int id, int count) { var item = _itemBusiness.GetById(id); var currentCart = Session["Cart"] as List <DTOCartItem>; if (currentCart.Any(i => i.Item.Id == item.Id)) { currentCart.FirstOrDefault(i => i.Item.Id == item.Id).Count += count; } else { var newCartItem = new DTOCartItem() { Item = item, Count = count }; currentCart.Add(newCartItem); } Session["Cart"] = currentCart; var result = GetCart(currentCart); return(Json(result, JsonRequestBehavior.AllowGet)); }
public void TestApplyPromotionThreeFor10() { var cartItem = new DTOCartItem() { Item = new Item() { Id = 0, Name = "", Price = 4.0m, Promotion = 2 }, Count = 3 }; decimal result = new PromotionBusiness().ApplyPromotion(cartItem); object.Equals(result, 10.0m); }
public void TestApplyPromotionBuyOneGetOneFree() { var cartItem = new DTOCartItem() { Item = new Item() { Id = 0, Name = "", Price = 2.0m, Promotion = 1 }, Count = 2 }; decimal result = new PromotionBusiness().ApplyPromotion(cartItem); object.Equals(result, 2.0m); }
public void TestApplyNoPromotion() { var cartItem = new DTOCartItem() { Item = new Item() { Id = 0, Name = "", Price = 3.0m, Promotion = 0 }, Count = 2 }; decimal result = new PromotionBusiness().ApplyPromotion(cartItem); object.Equals(result, 3.0m); }
public decimal ApplyPromotion(DTOCartItem cartItem) { switch ((Promotion)cartItem.Item.Promotion) { case Promotion.BuyOneGetOneFree: return(ApplyPromotionBuyOneGetOneFree(cartItem.Item.Price, cartItem.Count)); case Promotion.ThreeFor10: return(ApplyPromotionThreeFor10(cartItem.Item.Price, cartItem.Count)); default: return(cartItem.Item.Price); } }