Exemplo n.º 1
0
        public int AddToCart(int productId, string name, decimal price)
        {
            var userId = GetUserId();

            if (userId != 0)
            {
                using (var entities = new AngularBootcampEntities())
                {
                    var existingCartItem = entities.UserCartItems.FirstOrDefault(x => x.UserId == userId && x.ProductId == productId);

                    if (existingCartItem == null)
                    {
                        UserCartItem cartItem = new UserCartItem
                        {
                            ProductId = productId,
                            UserId = userId,
                            ProductName = name,
                            Price = price,
                            Count = 1
                        };
                        entities.UserCartItems.Add(cartItem);
                    }
                    else
                    {
                        existingCartItem.Count++;
                    }

                    entities.SaveChanges();
                }
            }
            return userId;
        }
Exemplo n.º 2
0
        public void EmptyCart()
        {
            var userId = GetUserId();

            if (userId != 0)
            {
                using (var entities = new AngularBootcampEntities())
                {
                    var remove = entities.UserCartItems.Where(x => x.UserId == userId);
                    entities.UserCartItems.RemoveRange(remove);

                    entities.SaveChanges();
                }
            }
        }
Exemplo n.º 3
0
        public void UpdateCartItem(int productId, int count)
        {
            var userId = GetUserId();

            if (userId != 0)
            {
                using (var entities = new AngularBootcampEntities())
                {
                    var existingCartItem = entities.UserCartItems.FirstOrDefault(x => x.ProductId == productId);

                    if (existingCartItem != null)
                    {
                        if (count < 1)
                        {
                            entities.UserCartItems.Remove(existingCartItem);
                        }
                        else
                        {
                            existingCartItem.Count = count;
                        }
                    }

                    entities.SaveChanges();
                }
            }
        }
Exemplo n.º 4
0
        public int RemoveFromCart(int productId)
        {
            var userId = GetUserId();

            if (userId != 0)
            {
                using (var entities = new AngularBootcampEntities())
                {
                    var existingCartItem = entities.UserCartItems.FirstOrDefault(x => x.UserId == userId && x.ProductId == productId);

                    entities.UserCartItems.Remove(existingCartItem);

                    entities.SaveChanges();
                }
            }
            return userId;
        }
Exemplo n.º 5
0
        //List<UserPurchaseHistoryItem> items)
        public JsonResult PlaceOrder(OrderDetail customerOrder, List<Product> items)
        {
            var success = true;
            List<UserPurchaseHistoryItem> purchaseItems = new List<UserPurchaseHistoryItem>();

            try
            {
                var userId = GetUserId();

                if (userId != 0)
                {
                    using (var entities = new AngularBootcampEntities())
                    {
                        customerOrder.UserId = userId;
                        var user = entities.Users.FirstOrDefault(x => x.Id == userId);

                        entities.OrderDetails.Add(customerOrder);

                        UserPurchaseHistoryItem item;
                        items.ForEach(x =>
                        {
                            item = new UserPurchaseHistoryItem
                            {
                                Date = DateTime.Now,
                                UserId = userId,
                                OrderId = customerOrder.OrderId,
                                ProductId = x.ProductId,
                                Name = x.Name,
                                Price = x.Price,
                                OrderDetail = customerOrder,
                                User = user,
                            };
                            for(int i=0; i<x.Count; i++)
                            {
                                purchaseItems.Add(item);
                            }
                        });

                        entities.UserPurchaseHistoryItems.AddRange(purchaseItems);

                        entities.SaveChanges();
                    }
                }
            }
            catch
            {
                success = false;
            }

            return new JsonResult { Data = success };
        }