// RemoveSingleItem - Remove one quantity of toRemove from this order. public void RemoveSingleItem(MenuItem toRemove) { // If there are no items, then do nothing. if (OrderContents == null) { return; } // Create a list of all current item IDs. string[] itemIDs = OrderContents.Split(','); // Convert this item's ID into a string // and see whether it is in the ID list. string removeID = toRemove.ItemID.ToString(); if (itemIDs.Contains(removeID)) { // If this is the only item, set the list to null. if (itemIDs.Length == 1) { OrderContents = null; } else { // If this is the first ID, remove it // and its trailing comma. if (itemIDs[0] == removeID) { OrderContents = OrderContents.Substring(removeID.Length + 1); } else { // Otherwise, remove it and the leading comma. int startIdx = OrderContents.IndexOf(removeID); OrderContents = OrderContents.Substring(0, startIdx - 1) + OrderContents.Substring(startIdx + removeID.Length); } } // Update the price if possible. if (toRemove.Price != null) { RawCost -= (int)toRemove.Price; } } }
// ContentsToItemList - Return a list of the items referenced by // the orderContents string. public List <MenuItem> ContentsToItemList() { if (OrderContents == null) { return(null); } // Establish a database connection. var db = new PickUpOrderDBEntities2(); // Create the list to be returned. var itemObjects = new List <MenuItem>(); // Split the ID string. For each ID value, // get the corresponding item and add it to itemObjects. string[] itemIDs = OrderContents.Split(','); foreach (string item in itemIDs) { int id = int.Parse(item); itemObjects.Add(db.MenuItems.Find(id)); } return(itemObjects); }
//Used on Confirm Order Page to complete submission public IActionResult PlaceOrder() //make async if necessary { string userId = _userManager.GetUserId(HttpContext.User); //Store all order contents in seperate table List <Cart> cartItems = context.Carts.Where(x => x.UserId.Equals(userId)).ToList(); if (cartItems.Count == 0) { return(RedirectToAction("Index", "Home")); } //Create an order based off currently available information Order newOrder = new Order(); newOrder.UserId = userId; newOrder.DatePlaced = DateTimeOffset.Now; newOrder.OrderStatus = "Processing"; //put into database before generating order contents context.Order.Add(newOrder); context.SaveChanges(); //haveto get the generated id out Order LastOrder = context.Order.Where(x => x.UserId.Equals(userId)).ToList().LastOrDefault(); float cartTotal = 0; foreach (Cart c in cartItems) { Product currProduct = context.Products.Find(c.ProductId); ProductInfo currProductInfo = context.ProductInfos.Find(c.ProductId); //Create order Content OrderContents currOrderContent = new OrderContents(); currOrderContent.OrderId = LastOrder.OrderId; currOrderContent.ProductId = c.ProductId; currOrderContent.Quantity = c.Quantity; currOrderContent.PricePaid = currProductInfo.ProductPrice; context.OrderContents.Add(currOrderContent); context.SaveChanges(); //End Create order Content cartTotal += currProductInfo.ProductPrice * c.Quantity; //remove from cart table context.Carts.Remove(c); //if it wont work then use some kinda context.SaveChanges(); } //set this after getting calculations LastOrder.Total = cartTotal; context.Entry(LastOrder).State = EntityState.Modified; context.SaveChanges(); //return View("ViewOrders"); return(RedirectToAction("ViewOrders")); }