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); }
public void InsertIntoCart(Product product, int quantity) { RemoveFromCart(product); ShoppingCartItems.Add(new ShoppingCartItemDTO() { Product = product, Quantity = quantity }); }
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; } }
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; } }
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); } } }