public void AddProductToCart(string username, int productId, int quantity) { using (var context = new EBContext()) { ShoppingCart exists = (from data in context.ShoppingCarts where data.OnlineCustomer.UserName.Equals(username) select data).FirstOrDefault(); ShoppingCartItem newItem = null; if (exists == null) { exists = new ShoppingCart(); exists.OnlineCustomerID = (from data in context.OnlineCustomers where data.UserName == username select data.OnlineCustomerID).SingleOrDefault(); exists.CreatedOn = DateTime.Now; exists = context.ShoppingCarts.Add(exists); } newItem = exists.ShoppingCartItems.SingleOrDefault(x => x.PartID == productId); if (newItem != null) { newItem.Quantity += quantity; } else { newItem = new ShoppingCartItem(); newItem.PartID = productId; newItem.Quantity = quantity; exists.ShoppingCartItems.Add(newItem); } context.SaveChanges(); } }
//DELETE ITEM FROM CART public void DeleteItem(string username, int shoppingcartitemid) { using (var context = new EBContext()) { ShoppingCart currentCart = (from data in context.ShoppingCartItems where data.ShoppingCart.OnlineCustomer.UserName.Equals(username) select data.ShoppingCart).FirstOrDefault(); if (currentCart == null) { throw new Exception("Shopping cart was deleted."); } else { ShoppingCartItem deleteItem = currentCart.ShoppingCartItems.Where(i => i.ShoppingCartItemID == shoppingcartitemid).FirstOrDefault(); if (deleteItem == null) { throw new Exception("Part does not exist."); } else { currentCart.UpdatedOn = DateTime.Now; context.ShoppingCartItems.Remove(deleteItem); context.SaveChanges(); } } } }
// BUSINESS LOGIC TO ADD NEW ONLINECUSTOMER public void AddOnlineCustomer(string username) { using (var context = new EBContext()) { var customer = (from data in context.OnlineCustomers where data.UserName.Equals(username) select data).FirstOrDefault(); if (customer == null) { Guid trackingCookie; trackingCookie = Guid.NewGuid(); customer = new OnlineCustomer(); customer.UserName = username; customer.CreatedOn = DateTime.Now; customer.TrackingCookie = trackingCookie; context.OnlineCustomers.Add(customer); context.SaveChanges(); } } }
// UPDATE ITEM public void UpdateItem(string username, int shoppingcartitemid, string direction) { using (var context = new EBContext()) { ShoppingCart currentCart = (from data in context.ShoppingCartItems where data.ShoppingCart.OnlineCustomer.UserName.Equals(username) select data.ShoppingCart).FirstOrDefault(); if (currentCart == null) { throw new Exception("Shopping cart was deleted."); } else { ShoppingCartItem currentItem = currentCart.ShoppingCartItems.Where(i => i.ShoppingCartItemID == shoppingcartitemid).FirstOrDefault(); if (currentItem == null) { throw new Exception("Part does not exist."); } else { if (direction.Equals("Add")) { currentItem.Quantity += 1; } else if (direction.Equals("Subtract")) { currentItem.Quantity -= 1; if (currentItem.Quantity <= 0) { context.ShoppingCartItems.Remove(currentItem); } } context.SaveChanges(); } } } }
// CREATE ORDER public void CreateOrder(string username, decimal total, string paymentType) { using (var context = new EBContext()) { var exists = (from x in context.ShoppingCarts where x.OnlineCustomer.UserName.Equals(username) select x).FirstOrDefault(); if (exists == null) { throw new Exception("Cart does not exist."); } else { var itemsExist = (from x in exists.ShoppingCartItems select x).ToList(); if (itemsExist == null) { throw new Exception("Shopping cart is empty."); } else { Sale newSale = new Sale(); newSale.SaleDate = DateTime.Now; newSale.UserName = username; newSale.EmployeeID = 10; newSale.TaxAmount = 0; newSale.SubTotal = total; newSale.PaymentType = paymentType; newSale.PaymentToken = null; newSale = context.Sales.Add(newSale); foreach (ShoppingCartItem item in itemsExist) { SaleDetail newSaleItem = new SaleDetail(); newSaleItem.PartID = item.PartID; newSaleItem.Quantity = item.Quantity; int partQty = (from x in context.Parts where x.PartID == item.PartID select x.QuantityOnHand).FirstOrDefault(); if (item.Quantity > partQty) { newSaleItem.Backordered = true; newSaleItem.ShippedDate = null; } else { newSaleItem.Backordered = false; newSaleItem.ShippedDate = DateTime.Now; Part currentPart = context.Parts.Find(item.PartID); currentPart.QuantityOnHand -= item.Quantity; context.Parts.Attach(currentPart); context.Entry(currentPart).State = EntityState.Modified; } newSale.SaleDetails.Add(newSaleItem); } context.SaveChanges(); } } } }