Exemple #1
0
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateCartAsync(CartInput input)
        {
            var cartdto = _cartAppService.GetAllList(x => x.UserID == AbpSession.UserId && x.ProductId == input.ProductId).FirstOrDefault();

            if (cartdto != null)
            {
                cartdto.Qty += 1;
                await _cartAppService.UpdateAsync(cartdto);
            }
            else
            {
                var model = input.MapTo <CartModel>();
                model.UserID   = Convert.ToInt32(AbpSession.UserId);
                model.TenantID = AbpSession.TenantId.HasValue ? AbpSession.TenantId.Value : 1;
                model.Qty      = 1;

                await _cartAppService.InsertAsync(model);
            }
        }
Exemple #2
0
        // Add items to shopping cart from Home Page and Product Details Page
        public IActionResult UpdateCart([FromBody] CartInput cartInput)
        {
            // Get the User ID for the current session
            string sessionId = HttpContext.Session.GetString("SessionId");
            string message   = "Items have been added to cart";

            // Get the product Id from cartInput
            int productId = Convert.ToInt32(cartInput.ProductId);
            int quantity  = Convert.ToInt32(cartInput.Quantity);

            if (sessionId == null)
            {
                int    i           = 0;
                string newcartitem = Convert.ToString(productId) + "," + Convert.ToString(quantity);

                // Get all the temp cart items
                // items is a dictionary of key = "productId,quanty", value = "Product{j}"
                Dictionary <string, string> items = new Dictionary <string, string>();
                int    j    = 0;
                string item = "initiate";
                do
                {
                    item = HttpContext.Session.GetString("Product" + Convert.ToString(j));
                    if (item == null)
                    {
                        break;
                    }
                    if (item != "removed product")
                    {
                        items.Add(item, "Product" + Convert.ToString(j));
                    }
                    j++;
                } while (item != null);


                // Find if the productId is in the session already
                bool     exist         = false;
                string[] tempProductId = new string[2];
                string   tempCartIndex = "";
                foreach (KeyValuePair <string, string> entry in items)
                {
                    tempProductId = entry.Key.Split(",");
                    tempCartIndex = entry.Value;
                    if (Convert.ToInt32(tempProductId[0]) == productId)
                    {
                        exist = true;
                        break;
                    }
                }

                // If productId is found in the session
                if (exist)
                {
                    string   toUpdate         = HttpContext.Session.GetString(tempCartIndex);
                    string[] tempproductIdQty = toUpdate.Split(",");
                    int      tempproductId    = Convert.ToInt32(tempproductIdQty[0]);
                    int      tempQty          = Convert.ToInt32(tempproductIdQty[1]);
                    // Check is quantity is more that what we have in stock
                    if (tempQty + quantity <= db.ActivationCode.Where(x => x.ProductId == tempproductId && x.IsSold == false).Count())
                    {
                        tempQty += quantity;
                    }
                    else
                    {
                        tempQty = db.ActivationCode.Where(x => x.ProductId == tempproductId && x.IsSold == false).Count();
                        message = "Reached maximum stock";
                    }
                    // Remove the old session storage and add the new one
                    HttpContext.Session.Remove(tempCartIndex);
                    string toAdd = Convert.ToString(tempproductId) + "," + Convert.ToString(tempQty);
                    HttpContext.Session.SetString(tempCartIndex, toAdd);
                }
                // If productId is not found in the session
                else
                {
                    while (string.IsNullOrEmpty(HttpContext.Session.GetString("Product" + Convert.ToString(i))) == false)
                    {
                        i++;
                    }

                    HttpContext.Session.SetString("Product" + Convert.ToString(i), newcartitem);
                }
            }


            else
            {
                int userId = db.Sessions.FirstOrDefault(x => x.SessionId == sessionId).UserId;



                // Check if the user currently as an order
                // Create a new order if user currently don't have any order yet
                if (db.Orders.FirstOrDefault(x => x.UserId == userId && x.IsPaid == false) == null)
                {
                    db.Orders.Add(new Order
                    {
                        UserId    = userId,
                        OrderDate = DateTime.Now.ToString()
                    });

                    db.SaveChanges();
                }

                // Get the order id
                Order order = db.Orders.FirstOrDefault(x => x.UserId == userId && x.IsPaid == false);

                // Check if the current item is already in the cart
                Cart cart = db.Cart.FirstOrDefault(x => x.ProductId == productId && x.OrderId == order.Id);

                if (cart == null)
                {
                    cart = new Cart()
                    {
                        OrderId   = order.Id,
                        ProductId = productId,
                        Quantity  = quantity
                    };
                    db.Cart.Add(cart);
                }
                // Don't allow user to add more that what we have in stock
                else if (cart.Quantity + quantity <= db.ActivationCode.Where(x => x.ProductId == cart.ProductId && x.IsSold == false).Count())
                {
                    cart.Quantity += quantity;
                }
                else
                {
                    cart.Quantity = db.ActivationCode.Where(x => x.ProductId == cart.ProductId && x.IsSold == false).Count();
                    message       = "Reached maximum stock";
                }

                db.SaveChanges();
            }

            return(Json(new
            {
                status = "success",
                message
            }));
        }