public async Task <ActionResult> PizzaTopping(int orderID, Size size, int?pizzaID) { Pizza pizza; var order = await db.Orders.FindAsync(orderID); if (order.Submitted) { return(await Task.Run(() => RedirectToAction("Order"))); } if (pizzaID.HasValue) { pizza = order.Pizzas.Single(p => p.ID == pizzaID); } else { pizza = new Pizza { Inches = size }; order.Pizzas.Add(pizza); await db.SaveChangesAsync(); } var toppingVM = new ToppingViewModel { Pizza = pizza, Toppings = db.Toppings.ToList() }; return(await Task.Run(() => View(toppingVM))); }
/// <summary> /// add du lieu. vao bang? topping /// </summary> /// <param name="_topping"></param> /// <param name="countRows"></param> public void AddDataDgvTopping(ToppingViewModel _topping, int countRows) { if (dgvTopping.Rows.Count == 0) { dgvTopping.Rows.Add(); dgvTopping.Rows[0].Cells["MaTopping"].Value = _topping.MaTopping; dgvTopping.Rows[0].Cells["TenTopping"].Value = _topping.TenTopping; } else { dgvTopping.Rows.Add(); dgvTopping.Rows[countRows].Cells["MaTopping"].Value = _topping.MaTopping; dgvTopping.Rows[countRows].Cells["TenTopping"].Value = _topping.TenTopping; } }
public IActionResult SizeAndToppingPicker() { var toppings = _context.ToppingInfo.ToList(); var model = new ToppingViewModel { Toppings = new string[toppings.Count] }; int i = 0; foreach (var topping in toppings) { model.Toppings[i] = topping.Name; i++; } return(View(model)); }
public async Task <ActionResult> FreePizza(int?pizzaID) { if (User.Identity.IsAuthenticated) { var member = await db.Members.FindAsync(User.Identity.GetUserId()); if (member.GetPoints() > 0) { var order = new Order(); db.Orders.Add(order); Pizza pizza; if (pizzaID.HasValue) { pizza = await db.Pizzas.FindAsync(pizzaID); } else { pizza = new Pizza { Inches = Size.Medium, Free = true }; } order.Pizzas.Add(pizza); await db.SaveChangesAsync(); var toppingVM = new ToppingViewModel { Pizza = pizza, Toppings = db.Toppings.ToList() }; return(await Task.Run(() => View(toppingVM))); } else { throw new InvalidOperationException("You do not have enough points to claim a free pizza."); } } else { throw new UnauthorizedAccessException("Guests cannot claim free pizzas."); } }
public IHttpActionResult GetSumLuaChonByMaHoaDon(int maHoaDon) { using (var db = new QuanLyTraSuaEntities()) { var maLuaChon = db.HoaDonChiTiets.SingleOrDefault(v => v.MaHoaDon == maHoaDon).MaLuaChon; var maTopping = db.LuaChons.Where(v => v.MaLuaChon == maLuaChon).ToList(); var result = new List <ToppingViewModel>(); maTopping.ForEach(v => { var temp = db.Toppings.Single(n => n.MaTopping == v.MaTopping); var resultItem = new ToppingViewModel() { MaTopping = temp.MaTopping, TenTopping = temp.TenTopping, DonGia = temp.DonGia, HinhAnh = temp.HinhAnh }; result.Add(resultItem); }); return(Ok(result.ToList())); } }
public void ToppingOptionAvailable() { ToppingViewModel.GetToppings(); }
public IActionResult Visit(int ID) { StoreModel store; try { store = _repo.GetStore(ID); } catch (SqlException e) { if (e.Message.Contains("server was not found")) { Console.WriteLine("Could not connect to the SQL database"); StoreViewModel thisModel = new StoreViewModel(); thisModel.ReasonForError = "An internal error has occured. Please return to the main page and try again."; return(View("Error", thisModel)); } throw e; } if (store == null) { StoreViewModel thisModel = new StoreViewModel(); thisModel.ReasonForError = $"A store with an ID of {ID} does not exist. Please enter a different ID from the URL, or select a store from the selection page after logging in."; return(View("Error", thisModel)); } List <MenuModel> items = _repo.GetMenu(ID); List <PizzaModel> pizzas = new List <PizzaModel>(); List <CheckModel> pizzasToSelectFrom = new List <CheckModel>(); List <ToppingModel> toppings = _repo.GetToppings(); foreach (MenuModel item in items) { PizzaModel pizza = _repo.GetPizza(item.PizzaID); if (pizza == null) { Console.WriteLine($"Unknown pizza found with ID {item.PizzaID} from store {item.StoreID} at menu ID {item.ID}"); continue; } string[] temp = pizza.DefaultToppings.Split(','); int[] defaultToppingIDs = new int[temp.Length]; for (int i = 0; i < temp.Length; i++) { if (!int.TryParse(temp[i], out defaultToppingIDs[i])) { Console.WriteLine($"Database error: Expected integer for default topping ID in pizza {pizza.ID}, got {temp[i]}"); continue; } } pizzas.Add(pizza); CrustModel crust = _repo.GetCrust(pizza.DefaultCrustID); ToppingViewModel[] toppingsSelected = new ToppingViewModel[toppings.Count()]; for (int i = 0; i < toppingsSelected.Length; i++) { ToppingModel topping = toppings[i]; toppingsSelected[i] = new ToppingViewModel { ID = topping.ID, Name = topping.Name, IsSelected = defaultToppingIDs.Contains(topping.ID) }; } pizzasToSelectFrom.Add(new CheckModel { ID = pizza.ID, Name = pizza.Name, Checked = false, Cost = pizza.Cost, DefaultCrust = crust.ID, SelectedCrust = crust.ID.ToString(), SelectedToppings = toppingsSelected }); } List <SelectListItem> crustDropDownOptions = new List <SelectListItem>(); foreach (CrustModel crust in _repo.GetCrusts()) { crustDropDownOptions.Add(new SelectListItem { Text = crust.Name, Value = crust.ID.ToString() }); } StoreViewModel model = new StoreViewModel(); model.StoreName = store.Name; model.Menu = pizzasToSelectFrom; try { _ = userLoggedIn; // keeps the session data alive } catch (NullReferenceException) { // people can view menus if they're not logged in, but not order } model.Crusts = crustDropDownOptions; model.Toppings = toppings; TempData["StoreID"] = store.ID; TempData.Keep("StoreID"); return(View(model)); }
public IActionResult SubmitOrder(StoreViewModel model) { TempData.Keep("StoreID"); int storeID = (int)TempData["StoreID"]; try { _ = userLoggedIn; } catch (NullReferenceException) { model.ReasonForError = "You are not logged into the system. You will only be able to view menus until you return to the main page and log in."; return(View("Visit", model)); } StoreModel store = _repo.GetStore(storeID); model.StoreName = store.Name; // reference needs to be re-established if an error occurs submitting the order List <SelectListItem> c = new List <SelectListItem>(); foreach (CrustModel crust in _repo.GetCrusts()) { c.Add(new SelectListItem { Text = crust.Name, Value = crust.ID.ToString() }); } model.Crusts = c; bool submitOrderClicked = Request.Form["SubmitOrder"].ToString() != ""; bool addCustomPizzaClicked = Request.Form["AddCustom"].ToString() != ""; bool backButtonClicked = Request.Form["Back"].ToString() != ""; int buttonsClicked = (submitOrderClicked ? 1 : 0) + (addCustomPizzaClicked ? 1 : 0) + (backButtonClicked ? 1 : 0); if (buttonsClicked > 1) { Console.WriteLine("Multiple buttons registered as clicked on the menu page"); model.ReasonForError = "There was a problem processing your request. Please try again."; return(View("Visit", model)); } else if (submitOrderClicked) { Tuple <int, string> userCanOrder = _repo.UserCanOrder(storeID, userLoggedIn); if (userCanOrder.Item1 == 1) { model.ReasonForError = $"You cannot place another order at any store for {userCanOrder.Item2}"; return(View("Visit", model)); } else if (userCanOrder.Item1 == 2) { model.ReasonForError = $"You cannot place another order at this store for {userCanOrder.Item2}"; return(View("Visit", model)); } decimal overallCost = 0.00M; int overallQuantity = 0; int max = _repo.GetNextOrderNumber(); bool noIssues = true; foreach (CheckModel selectedPizza in model.Menu) { if (selectedPizza.Checked) { string size = selectedPizza.SelectedSize.ToString().ToLower(); if (Enum.IsDefined(typeof(Size), size)) { model.ReasonForError = $"Invalid size on pizza {selectedPizza.Name}"; return(View("Visit", model)); } if (selectedPizza.Quantity == 0) { model.ReasonForError = $"{selectedPizza.Name} pizza must have a quantity greater than 0 if selected to be ordered"; return(View("Visit", model)); } else if (selectedPizza.Quantity < 0) { model.ReasonForError = $"{selectedPizza.Name} pizza must have a positive quantity greater"; return(View("Visit", model)); } int crustID; CrustModel crust = null; if (int.TryParse(selectedPizza.SelectedCrust, out crustID)) { crust = _repo.GetCrust(crustID); } if (crust == null) { model.ReasonForError = $"No crust was selected on the {selectedPizza.Name} pizza. Please try selecting a different crust."; return(View("Visit", model)); } PizzaModel pizza; if (selectedPizza.ID != 0) { pizza = _repo.GetPizza(selectedPizza.ID); } else { pizza = new PizzaModel { Cost = 20.00M }; } if (pizza == null) { Console.WriteLine($"Unknown pizza with ID {selectedPizza.ID} submitted; skipping"); continue; } decimal costOfThesePizzas = pizza.Cost * (decimal)selectedPizza.Quantity; string toppingIDs = ""; int toppingCount = 0; foreach (ToppingViewModel topping in selectedPizza.SelectedToppings) { if (topping.IsSelected) { toppingIDs += $"{topping.ID},"; toppingCount++; } } if (toppingCount > 5) { model.ReasonForError = $"{selectedPizza.Name} has more than 5 toppings selected. Please uncheck some toppings on this pizza."; return(View("Visit", model)); } else if (toppingCount < 2) { model.ReasonForError = $"{selectedPizza.Name} needs at least 2 toppings selected. Please add some more toppings on this pizza."; return(View("Visit", model)); } toppingIDs = toppingIDs.Substring(0, toppingIDs.Length - 1); noIssues &= _repo.AddOrder(new OrderModel { OrderID = max + 1, StoreID = storeID, PizzaID = pizza.ID, UserID = userLoggedIn, Created = DateTime.Now, Quantity = selectedPizza.Quantity, TotalCost = costOfThesePizzas, Size = selectedPizza.SelectedSize.ToString(), CrustID = crust.ID, Toppings = toppingIDs }); overallCost += costOfThesePizzas; overallQuantity += selectedPizza.Quantity; } } if (overallCost > 250.00M) { model.ReasonForError = "This order exceeds $250. Please remove some pizzas, then try again."; return(View("Visit", model)); } else if (overallQuantity > 50) { model.ReasonForError = "This order exceeds 50 pizzas. Please remove some pizzas, then try again."; return(View("Visit", model)); } else if (overallQuantity == 0) { model.ReasonForError = "There are no pizzas in this order. Please add some pizzas, then try again."; return(View("Visit", model)); } else if (!noIssues) { model.ReasonForError = "There was a problem adding some pizzas to your order"; } return(View("Submitted")); } else if (addCustomPizzaClicked) { List <ToppingModel> toppings = _repo.GetToppings(); ToppingViewModel[] toppingsSelected = new ToppingViewModel[toppings.Count()]; for (int i = 0; i < toppingsSelected.Length; i++) { ToppingModel topping = toppings[i]; toppingsSelected[i] = new ToppingViewModel { ID = topping.ID, Name = topping.Name, IsSelected = false }; } model.Menu.Add(new CheckModel { ID = 0, Name = "Custom", Checked = true, Cost = 20.00M, DefaultCrust = 0, SelectedToppings = toppingsSelected }); return(View("Visit", model)); } else if (backButtonClicked) { return(Redirect("/User/StoreSelection")); } else // no buttons check is placed down here to remove the 'not all code paths return a value' error { Console.WriteLine("Request was sent but no buttons registered as clicked"); model.ReasonForError = "There was a problem processing your request. Please try again."; return(View("Visit", model)); } }