Ejemplo n.º 1
0
        public static string AddProductInCart(int Product_ID, int Qty, string Size)
        {
            PAWDStoreEntities context = new PAWDStoreEntities();
            int User_ID       = (HttpContext.Current.Session["User"] as AdminLoginMaster).User_ID;
            var IsThereInCart = context.Cart_Master.FirstOrDefault(x => x.User_ID == User_ID && x.Product_ID == Product_ID);

            if (IsThereInCart == null)
            {
                var obj = new Cart_Master
                {
                    Product_ID  = Product_ID,
                    User_ID     = User_ID,
                    Quantity    = Qty,
                    Size        = !string.IsNullOrEmpty(Size) ? Size : string.Empty,
                    CreatedDate = DateTime.Now
                };
                context.Cart_Master.Add(obj);
                context.SaveChanges();
            }
            else
            {
                IsThereInCart.Quantity = (IsThereInCart.Quantity + Qty) > 10 ? 10 : (IsThereInCart.Quantity + Qty);
                context.SaveChanges();
            }
            int CartItemCount = context.Cart_Master.Count(x => x.User_ID == User_ID);

            return(JsonConvert.SerializeObject(CartItemCount));
        }
        public IHttpActionResult AddToCart(Cart_Master C)
        {
            var DB = new AngularDemoEntities();

            try
            {
                if (C != null)
                {
                    var  user_id = User.Identity.GetUserId();
                    bool IsThere = DB.Cart_Master.Any(x => x.ProductID == C.ProductID && x.UserID == user_id);
                    if (!IsThere)
                    {
                        C.UserID = User.Identity.GetUserId();
                        DB.Cart_Master.Add(C);
                        var Products = DB.Product_Master.Where(x => x.ProductID == C.ProductID).SingleOrDefault();
                        if (Products != null)
                        {
                            Products.ProductQuantity--;
                        }
                        DB.SaveChanges();
                        return(Ok("success"));
                    }
                    else
                    {
                        return(Ok("already"));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }