コード例 #1
0
        // Get
        public async Task <ActionResult> Index()
        {
            ShoppingService   service        = new ShoppingService();
            ProductionService productService = new ProductionService();
            ShoppingCart      shoppingCart   = new ShoppingCart();

            if (FormsAuth.UserManager.User != null)
            {
                /*{
                 *  Amount,
                 *  CartId,
                 *  Type,
                 *  CartDetailsId,
                 *  ExtendedPrice,
                 *  ProductDetailsId,
                 *  Price,
                 *  Picture,
                 *  ProductName,
                 *  ProductID,
                 *  Color,
                 *  UserId,
                 *  TotalAmountProduction
                 * }*/
                // get all shopping cart items of current user
                shoppingCart.listItemCart = await service.getCartItemsByUserId(UserManager.User.Id);
            }
            else  // get from Cookie
            {
                HttpCookie reqCookies;
                HttpCookie reqIDListCookies = Request.Cookies["ProductDetailIDlist"];
                if (reqIDListCookies != null)
                {
                    string dataAsString = reqIDListCookies.Value;
                    if (!dataAsString.Equals(string.Empty))
                    {
                        List <int>      listdata     = new List <int>();
                        List <CartItem> listCartItem = new List <CartItem>();

                        listdata = dataAsString.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                        for (int i = 0; i < listdata.Count(); i++)
                        {
                            CartItem item = new CartItem();
                            item.ProductDetailsId = listdata[i];

                            reqCookies = Request.Cookies["CartItems[" + item.ProductDetailsId.ToString() + "]"];
                            if (reqCookies != null)
                            {
                                CartItem cookiesItem = new JavaScriptSerializer().Deserialize <CartItem>(reqCookies.Value);

                                item.Amount = cookiesItem.Amount;
                                item.Price  = cookiesItem.Price;

                                listCartItem.Add(item);
                            }
                        }

                        //
                        if (listCartItem.Count() > 0)
                        {
                            List <CartDetailItem> cartDetailItemList = new List <CartDetailItem>();

                            // for each cookies item
                            foreach (CartItem cookiesItem in listCartItem)
                            {
                                CartDetailItem cartDetailItem = new CartDetailItem();

                                // get more information from product & productDetails tables in Database
                                ProductionDetail productDetail = await productService.getProductDetailByProductDetailsIds(cookiesItem.ProductDetailsId);

                                Production product = await productService.getProductionByProductDetailsId(cookiesItem.ProductDetailsId);

                                cartDetailItem.ProductDetailsId      = cookiesItem.ProductDetailsId;
                                cartDetailItem.Price                 = productDetail.Price;
                                cartDetailItem.Picture               = productDetail.Picture;
                                cartDetailItem.Color                 = productDetail.Color;
                                cartDetailItem.TotalAmountProduction = productDetail.Amount;
                                cartDetailItem.Amount                = cookiesItem.Amount;
                                cartDetailItem.ExtendedPrice         = cookiesItem.Price;
                                cartDetailItem.CartId                = 0;
                                cartDetailItem.Type          = 0;
                                cartDetailItem.CartDetailsId = 0;
                                cartDetailItem.ProductID     = product.ProductId;
                                cartDetailItem.ProductName   = product.ProductionName;
                                cartDetailItem.UserId        = 0;

                                cartDetailItemList.Add(cartDetailItem);
                            }

                            shoppingCart.listItemCart = cartDetailItemList;
                        }
                    }
                }
            }

            return(View(shoppingCart));
        }