Пример #1
0
		// AddItemToCart(string, MenuItemView) => async void
		// Associates a given item with an order id and sets it in the database
		// PRODUCTION: This should be called from an AJAX POST/GET script. 
		public async Task AddItemToCart(string orderId,  MenuItemView item)//string optionId, Sauce sauce, string itemId, int quantity)
		{
			if (orderId != null && orderId != "" && item != null)
			{
				// see if there is already a menuitem with that comb
				OrderItems existingItem = await this.database.OrderItems.Where(x => x.OrderId == orderId && x.MenuItemId == item.Id && x.OptionId == item.OptionSelected).FirstOrDefaultAsync();
				
				// Entry point: Item does exist already in the cart
				if (existingItem != null)
				{
					// just in case - this shouldn't be possible since there is javascript verification and currently this is AJAX only.
					// However, this is here in case we have users who don't use JS or somehow validation doesn't catch it properly
					if (item.Quantity < 0)
					{
						existingItem.Quantity = 0;
						existingItem.Removed = true;
					}
					else
					{
						// we just update the changes
						existingItem.Quantity = item.Quantity;
						existingItem.Removed = false;
						existingItem.OptionId = item.OptionSelected;
						existingItem.IsHalfOrder = item.IsHalfOrder;
						existingItem.Sauce = item.Sauce;
						existingItem.SpiceLevel = item.SpiceLevel;
						existingItem.Created = DateTime.Now;
					}
					if (this.database.Entry(existingItem).State == EntityState.Detached)
					{
						this.database.OrderItems.Attach(existingItem);
					}
					this.database.Entry(existingItem).State = EntityState.Modified;
				} // end item exists and entry item doesn't exist
				else
				{
					// we create the new item
					existingItem = new OrderItems() { MenuItemId = item.Id, OrderId = orderId, Quantity = item.Quantity, IsHalfOrder = item.IsHalfOrder, OptionId = item.OptionSelected, SpiceLevel = item.SpiceLevel, Created = DateTime.Now };
					this.database.OrderItems.Add(existingItem);
				}
			}
			await this.database.SaveChangesAsync();
		}
Пример #2
0
		private string GenerateItemHtml(OrderItems o)
		{
			string s = "<table class='item " + o.MenuItemId + " tiny'>\n";
			if (o != null)
			{
				MenuItems item = this.SpicyGardenDbContext.Menu.Where(x => x.Id == o.MenuItemId).First();
            s += "<tr><td colspan='2'>" + o.Quantity + " " + item.Name + (o.IsHalfOrder ? " (Half-Order)</td></tr>\n" : "</td></tr>\n");
				if (o.OptionId != null)
				{
					MenuOptions opt = this.SpicyGardenDbContext.Options.Where(x => x.Id == o.OptionId).First();
					s += "<tr><td class='cart-det'>" + opt.OptionName + (o.Sauce != null ? " with " + o.Sauce.Value + " sauce</td>" : "</td>");
					s += "<td class='cart-s price'>" + (o.IsHalfOrder ? opt.HalfOrderPrice * o.Quantity : opt.OptionPrice * o.Quantity) + "</td></tr>\n";
				}
				else
				{
					s += "<tr><td class='cart-det'>" + (o.Sauce != null ? o.Sauce.Value + " sauce</td>" : "</td>");
					s += "<td class='cart-s price'>" + (o.IsHalfOrder ? item.HalfOrderPrice * o.Quantity : item.Price * o.Quantity) + "</td></tr>\n";
				}
			}
			s += "</table>";
			return s;
		}