示例#1
0
        public IActionResult UpdateCart([FromBody] CartUpdate cu)
        {
            int   cartCount;
            float subTotal;
            float total = 0;

            try
            {
                int productId; int qty;
                productId = cu.ProductId; qty = cu.Qty;
                Debug.WriteLine($"Prod ID:{productId}, Qty: {qty}");
                if (qty <= 0)
                {
                    return(Json(new
                    {
                        success = false
                    }));
                }

                // if user is not logged in, update cart data in Session State as a Jsonified dict
                if (!User.Identity.IsAuthenticated)
                {
                    var cartList = HttpContext.Session.GetJson <CartListViewModel>("cart");

                    // check if "cart" exists in Session data
                    if (cartList != null)
                    {
                        // update cart item qty if cart item exists, otherwise add new cart item
                        cartList.UpdateCart(new Cart {
                            ProductId = productId, Qty = qty
                        });
                    }
                    // create new cratList Dict if there isn't one in session
                    else
                    {
                        cartList = new CartListViewModel();
                        cartList.UpdateCart(new Cart {
                            ProductId = productId, Qty = qty
                        });
                    }

                    // update "cart" Session data
                    HttpContext.Session.SetJson("cart", cartList);

                    // get latest "cartCount" and set to Session data
                    cartCount = cartList.CartCount;
                    HttpContext.Session.SetInt32("cartCount", cartCount);

                    // for debugging, to delete
                    foreach (Cart c in cartList.List)
                    {
                        Debug.WriteLine($"Prod: {c.ProductId} - {c.Qty}");
                    }
                    Debug.WriteLine("Cart count: " + cartCount);

                    var prod = db.Products.FirstOrDefault(p => p.Id == productId);

                    subTotal = (prod.UnitPrice * qty) * (1 - prod.Discount);

                    foreach (Cart c in cartList.List)
                    {
                        var currProd  = db.Products.FirstOrDefault(p => p.Id == c.ProductId);
                        var unitPrice = currProd.UnitPrice;
                        var discount  = currProd.Discount;
                        total += unitPrice * c.Qty * (1 - discount);
                    }
                }
                // else user is logged in, update cart data in SQL db Cart table
                else
                {
                    string userId = User.FindFirst("userId").Value;
                    var    cart   = db.Carts.FirstOrDefault(c => c.ProductId == productId && c.UserId == userId);

                    // update cart item's qty if exists, otherwise add new Cart object
                    if (cart != null)
                    {
                        cart.Qty = qty;
                    }
                    else
                    {
                        cart = new Cart()
                        {
                            UserId = userId, ProductId = productId, Qty = qty
                        };
                        db.Carts.Add(cart);
                    }
                    db.SaveChanges();

                    // get latest "cartCount" and set to Session data
                    cartCount = db.Users.FirstOrDefault(u => u.Id == userId).Carts.Sum(c => c.Qty);
                    HttpContext.Session.SetInt32("cartCount", cartCount);

                    // for debugging, to delete
                    //foreach (Cart c in db.Users.FirstOrDefault(u => u.Id == userId).Carts)
                    //{
                    //    Debug.WriteLine($"Prod: {c.ProductId} - {c.Qty}");
                    //}
                    //Debug.WriteLine("Cart count: " + cartCount);

                    subTotal = cart.Product.UnitPrice * qty * (1 - cart.Product.Discount);

                    foreach (Cart c in db.Users.FirstOrDefault(u => u.Id == userId).Carts)
                    {
                        total += c.Product.UnitPrice * c.Qty * (1 - c.Product.Discount);
                    }
                }

                HttpContext.Session.SetInt32("cartCount", cartCount);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                _logger.LogError(ex, $"Error updating cart for {cu}");
                return(Json(new
                {
                    success = false
                }));
            }

            return(Json(new
            {
                success = true,
                cartCount = cartCount,
                subTotal = subTotal.ToString("S$ 0.00"),
                total = total.ToString("S$ 0.00")
            }));
        }