示例#1
0
        public void AddCourse(int quantity, Course course, ApplicationUser user)
        {
            // 1. Avoid duplicate order
            var isOrderDuplicate = ShoppingCartItems.Select(i => i.CourseId).Contains(course.Id);

            if (isOrderDuplicate)
            {
                throw new ShoppingCartValidationException("Course \"" + course.Title + "\" already in your ShoppingCart.");
            }

            // 2. check course state
            if (!course.IsActivate)
            {
                throw new ShoppingCartValidationException("Course \"" + course.Title + "\" is not activate, please remove it from your selection.");
            }

            // 3. check user purchase history, avoid duplicate purchase
            if (user.PurchasedCourses.Any(uc => uc.CourseId == course.Id))
            {
                throw new ShoppingCartValidationException("Course \"" + course.Title + "\"  has already purchased, please check your course list.");
            }

            // 4. create line item (shoppingCartItem)
            var shoppingCartItem = LineItem.Create(quantity, this, course);

            // 4. add shoppingCartItem into shoppingCart
            ShoppingCartItems.Add(shoppingCartItem);
        }
示例#2
0
        public void InsertIntoCart(Product product, int quantity)
        {
            RemoveFromCart(product);

            ShoppingCartItems.Add(new ShoppingCartItemDTO()
            {
                Product  = product,
                Quantity = quantity
            });
        }
示例#3
0
 public void PopulateShoppingCartItemsByLineItems(List <AddItemViewModel> lineItems)
 {
     foreach (var lineItem in lineItems)
     {
         var item          = _context.Items.Find(lineItem.ItemId);
         var lineItemTotal = item.Price * lineItem.Quantity;
         ShoppingCartItems.Add(new ShoppingCartLineItemViewModel
         {
             ItemId   = lineItem.ItemId,
             Item     = item,
             Quantity = lineItem.Quantity,
             Total    = lineItemTotal
         });
         Total += lineItemTotal;
     }
 }
示例#4
0
        public void PopulateShoppingCartItemsByCustomerId(string customerId)
        {
            var lineItems = _context.ShoppingCartLineItems.Include(li => li.Item).Where(li => li.CustomerId == customerId).ToList();

            foreach (var lineItem in lineItems)
            {
                var lineItemTotal = lineItem.Item.Price * lineItem.Quantity;
                ShoppingCartItems.Add(new ShoppingCartLineItemViewModel
                {
                    ItemId   = lineItem.ItemId,
                    Item     = lineItem.Item,
                    Quantity = lineItem.Quantity,
                    Total    = lineItemTotal
                });
                Total += lineItemTotal;
            }
        }
示例#5
0
        public void addItem(Product product, int quantity)
        {
            if (product == null)
            {
                throw new ArgumentNullException("Product is Null");
            }

            if (quantity > 0)
            {
                if (ShoppingCartItems.ContainsKey(product))
                {
                    ShoppingCartItems[product] += quantity;
                }
                else
                {
                    ShoppingCartItems.Add(product, quantity);
                }
            }
        }