Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
            }
        }