コード例 #1
0
        public ShoppingCart CreateCart()
        {
            var cart = new ShoppingCart();

            _shoppingContext.Add(cart);
            _shoppingContext.Save();

            _httpContext.HttpContext.Response.Cookies.Append("ShoppingCartId", cart.Id.ToString());

            return(cart);
        }
コード例 #2
0
        public JsonResult Add(Guid id, AddItemModel model)
        {
            var targetCart = _shoppingContext.Carts.FirstOrDefault(cart => cart.Id == id);

            if (targetCart == null)
            {
                return(JsonError(HttpStatusCode.NotFound, "Cart not found"));
            }

            var itemToAdd = _shoppingContext.Catalog.FirstOrDefault(item => item.Id == model.ItemId);

            if (itemToAdd == null)
            {
                return(JsonError(HttpStatusCode.BadRequest, "Item not found"));
            }

            try
            {
                var rnd = new Random();
                if (rnd.Next(1, 100) > 70)
                {
                    // Oj vad jobbigt det var idag
                    Thread.Sleep(3000);
                }

                if (!string.IsNullOrEmpty(model.Customization))
                {
                    var customItem = new CustomizedJewelry <Jewelry>(model.Customization, itemToAdd);
                    targetCart.Add(model.Amount, model.Size, customItem);
                }
                else
                {
                    targetCart.Add(model.Amount, model.Size, itemToAdd);
                }
            }
            catch (Exception ex)
            {
                return(JsonError(HttpStatusCode.BadRequest, ex.Message));
            }

            _shoppingContext.Save();
            return(Json(targetCart));
        }