public IActionResult OrderHistory(OrderViewModel model) { List<OrderModel> orders; try { _ = userLoggedIn; // exception not caught if you just use uLI orders = _repo.GetOrdersForUser(userLoggedIn); } catch (NullReferenceException) { model.ReasonForError = "You are not logged in. Please return to the main page to login and try again."; return View("Error", model); } List<OrderViewClass> orderHistory = new List<OrderViewClass>(); foreach (OrderModel order in orders) { StringBuilder toppings = new StringBuilder(); foreach (string topping in order.Toppings.Split(',')) { int toppingID; if (!int.TryParse(topping, out toppingID)) { Console.WriteLine($"Database error: Expected integer for pizza ID, received {topping}"); toppings.Append("Error, "); continue; } ToppingModel top = _repo.GetTopping(toppingID); toppings.Append($"{top.Name}, "); } toppings.Remove(toppings.Length - 2, 2); OrderViewClass orderView = new OrderViewClass{ OrderID = order.OrderID, Created = order.Created, Size = order.Size, Crust = _repo.GetCrust(order.CrustID).Name, Toppings = toppings.ToString(), Quantity = order.Quantity, Cost = order.TotalCost, StoreName = _repo.GetStore(order.StoreID).Name }; if (order.PizzaID == 0) { orderView.Pizza = "Custom"; } else { try { orderView.Pizza = _repo.GetPizza(order.PizzaID).Name; } catch (NullReferenceException) { Console.WriteLine($"Database error: Could not find a pizza with ID {order.PizzaID} in the Pizza table"); orderView.Pizza = "Error"; } } orderHistory.Add(orderView); } model.OrderHistory = orderHistory; return View(model); }
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)); }