コード例 #1
0
        public bool RemoveItemDB(string id, string username)
        {
            int ProductID = Convert.ToInt32(id);

            emptyCart = false;

            try
            {
                //get user's cart with status not checkout
                var userOrderID = db.Orders
                                  .Where(o => o.CustomerID.Equals(username) && o.OrderStatus == (int)Enumeration.OrderStatus.InCart)
                                  .Select(o => o.OrderID)
                                  .FirstOrDefault();

                //=====================================================================
                if (userOrderID == 0) //currently dont have any cart => can not remove
                {
                    return(false);
                }
                //=====================================================================
                else //have cart => check if item have in cart
                {
                    var quantity = db.OrderDetails
                                   .Where(d => d.OrderID == userOrderID && d.WatchID == ProductID)
                                   .Select(d => d.Quantity).FirstOrDefault();

                    //========================
                    if (quantity == 0) //item not existed in order || item auto become 0 =>
                    {
                        OrderDetail detail = db.OrderDetails.Find(ProductID, userOrderID);
                        db.OrderDetails.Attach(detail);
                        db.OrderDetails.Remove(detail);

                        int resultDetail = db.SaveChanges();
                        if (resultDetail > 0)
                        {
                            //check if cart is empty => to refresh page
                            var itemInCart = db.OrderDetails
                                             .Where(d => d.OrderID == userOrderID)
                                             .Select(d => d.WatchID)
                                             .ToList();

                            if (itemInCart.Count != 0)
                            {
                                return(true); //still have item in cart in OrderDetail DB
                            }

                            var curEmptyOrder = db.Orders.Find(userOrderID);
                            db.Orders.Attach(curEmptyOrder);
                            db.Orders.Remove(curEmptyOrder);
                            db.SaveChanges();

                            emptyCart = true;
                            return(true); //success + empty cart!
                        }
                        else
                        {
                            return(false); //SaveChange fail
                        }
                    }
                    else //======================== have item in cart => remove item
                    {
                        OrderDetail detail = db.OrderDetails.Find(ProductID, userOrderID);
                        db.OrderDetails.Attach(detail);
                        db.OrderDetails.Remove(detail);

                        int resultDetail = db.SaveChanges();
                        if (resultDetail > 0)
                        {
                            //check if cart is empty => to refresh page
                            var itemInCart = db.OrderDetails
                                             .Where(d => d.OrderID == userOrderID)
                                             .Select(d => d.WatchID)
                                             .ToList();

                            if (itemInCart.Count != 0)
                            {
                                return(true); //still have item in cart in OrderDetail DB
                            }

                            var curEmptyOrder = db.Orders.Find(userOrderID);
                            db.Orders.Attach(curEmptyOrder);
                            db.Orders.Remove(curEmptyOrder);
                            db.SaveChanges();

                            emptyCart = true;
                            return(true); //success + empty cart!
                        }
                        else
                        {
                            return(false); //SaveChange fail
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #2
0
        public bool AddToCartDB(string id, string username)
        {
            int ProductID = Convert.ToInt32(id);

            var product = db.Watches
                          .Where(w => w.WatchID == ProductID)
                          .Select(p => new ShoppingProduct
            {
                ProductID       = ProductID,
                ProductCode     = p.WatchCode,
                ProductPrice    = p.Price,
                ProductPhoto    = p.Thumbnail,
                ProductDiscount = p.Discount
            }).FirstOrDefault();

            try
            {
                //get user's cart with status not checkout
                var userOrderID = db.Orders
                                  .Where(o => o.CustomerID.Equals(username) && o.OrderStatus == (int)Enumeration.OrderStatus.InCart)
                                  .Select(o => o.OrderID)
                                  .FirstOrDefault();

                //=====================================================================
                if (userOrderID == 0) //currently dont have any cart
                {
                    //create new cart in DB
                    Order currentOrder = new Order();
                    currentOrder.CustomerID  = username;
                    currentOrder.OrderStatus = (int)Enumeration.OrderStatus.InCart;

                    db.Orders.Add(currentOrder);
                    int result = db.SaveChanges();
                    if (result == 0)
                    {
                        failByQuantity = false;
                        return(false); //insert fail
                    }

                    //get orderID
                    var orderID = (int)db.Orders
                                  .Where(o => o.CustomerID.Equals(username) && o.OrderStatus == (int)Enumeration.OrderStatus.InCart)
                                  .Select(o => o.OrderID)
                                  .First();

                    //check quantity of product in DB
                    bool checkQuantity = this.CheckAvailableAndQuantityInDB(ProductID, 1); //add new 1 item
                    if (!checkQuantity)
                    {
                        failByQuantity = true;
                        return(false);
                    }

                    //add new item to cart
                    OrderDetail detail = new OrderDetail();
                    detail.OrderID  = orderID;
                    detail.WatchID  = ProductID;
                    detail.Quantity = 1;

                    db.OrderDetails.Add(detail);
                    int resultDetail = db.SaveChanges();
                    if (resultDetail > 0)
                    {
                        return(true); //success
                    }
                    else
                    {
                        failByQuantity = false;
                        return(false);
                    }
                }
                //=====================================================================
                else //have cart => check if item have in cart
                {
                    var quantity = db.OrderDetails
                                   .Where(d => d.OrderID == userOrderID && d.WatchID == ProductID)
                                   .Select(d => d.Quantity).FirstOrDefault();

                    //========================
                    if (quantity == 0) //item not existed in order => add new item to cart, quantity = 1
                    {
                        //check quantity of product in DB
                        bool checkQuantity = this.CheckAvailableAndQuantityInDB(ProductID, 1);
                        if (!checkQuantity)
                        {
                            failByQuantity = true;
                            return(false); //add fail
                        }

                        //valid quantity => add new item to order detail
                        OrderDetail detail = new OrderDetail();
                        detail.OrderID  = userOrderID;
                        detail.WatchID  = ProductID;
                        detail.Quantity = 1;

                        db.OrderDetails.Add(detail);
                        int resultDetail = db.SaveChanges();
                        if (resultDetail > 0)
                        {
                            return(true); //success
                        }
                        else
                        {
                            failByQuantity = false;
                            return(false); //fail
                        }
                    }
                    else //======================== have item in cart => update quantity
                    {
                        //check quantity of product in DB
                        bool checkQuantity = this.CheckAvailableAndQuantityInDB(ProductID, quantity + 1);
                        if (!checkQuantity)
                        {
                            failByQuantity = true;
                            return(false); //add fail
                        }

                        OrderDetail detail = db.OrderDetails.Find(ProductID, userOrderID);
                        db.OrderDetails.Attach(detail);
                        detail.Quantity = quantity + 1;

                        int resultDetail = db.SaveChanges();
                        if (resultDetail > 0)
                        {
                            return(true); //success
                        }
                        else
                        {
                            failByQuantity = false;
                            return(false); //fail
                        }
                    }
                }
            }
            catch (Exception)
            {
                failByQuantity = false;
                return(false);
            }
        }