public IActionResult PlaceOrder(PizzaViewModel pvm)
        {
            CrustModel c = new CrustModel()
            {
                Name = pvm.Crust
            };
            SizeModel s = new SizeModel()
            {
                Name = pvm.Size
            };
            ToppingModel t = new ToppingModel()
            {
                Name = pvm.Toppings[0].Name
            };
            PizzaModel p = new PizzaModel()
            {
                Crust = c, Size = s, Topping = new List <ToppingModel>()
                {
                    t
                }
            };
            OrderModel o = new OrderModel()
            {
                Pizzas = new List <PizzaModel>()
                {
                    p
                }
            };

            _db.Orders.Add(o);
            return(RedirectToPage("/Home Page"));
        }
Example #2
0
        public AModel Update(PizzaModel pizza, CrustModel crust, SizeModel size, List <ToppingModel> toppings)
        {
            pizza.Crust    = crust;
            pizza.Size     = size;
            pizza.Toppings = toppings;

            return(pizza);
        }
Example #3
0
        public IActionResult PlaceOrder(PizzaViewModel pvm)
        {
            CrustModel c = new CrustModel()
            {
                Name = pvm.crust
            };
            SizeModel s = new SizeModel()
            {
                Name = pvm.size
            };
            List <ToppingsBase> t = new List <ToppingsBase>();
            int toppingCounter    = 0;
            int refToppingCounter = 0;

            foreach (var toppingStr in pvm.SelectedToppings)
            {
                refToppingCounter = 0;
                foreach (var referenceTopping in pvm.ToppingsBase)
                {
                    System.Console.WriteLine("Yeet");
                    System.Console.WriteLine(pvm.ToppingsBase.ElementAt(0));
                    if (referenceTopping.Name.Equals(toppingStr))
                    {
                        t.Add(pvm.ToppingsBase.ElementAt(refToppingCounter));
                    }
                    refToppingCounter++;
                }
                toppingCounter++;
            }

            PizzaModel p = new PizzaModel()
            {
                Crust = c, Size = s, Toppings = t
            };
            OrderModel o = new OrderModel()
            {
                Pizzas = new List <PizzaModel>()
                {
                    p
                }
            };

            _db.Orders.Add(o);
            return(View("displayOrders", o));
        }
        public decimal ReadNewPrice(string crust, string size, List <string> toppings, int orderId)
        {
            decimal price = 0.00m;

            OrderModel order = _db.Orders
                               .Where(o => o.Id == orderId)
                               .Include(o => o.Pizzas).ThenInclude(p => p.Size)
                               .Include(o => o.Pizzas).ThenInclude(p => p.Crust)
                               .Include(o => o.Pizzas).ThenInclude(p => p.PizzaToppings).ThenInclude(pt => pt.Topping)
                               .SingleOrDefault();

            CrustModel newCrust = _db.Crusts
                                  .Where(c => c.Name == crust)
                                  .SingleOrDefault();

            SizeModel newSize = _db.Sizes
                                .Where(s => s.Name == size)
                                .SingleOrDefault();

            List <ToppingModel> newToppings = new List <ToppingModel>();

            foreach (var topping in toppings)
            {
                newToppings.Add(_db.Toppings.Where(t => t.Name == topping).SingleOrDefault());
            }

            //Get the price of the current order
            foreach (var p in order.Pizzas.ToList())
            {
                price += (p.Size.Price + p.Crust.Price);
                foreach (var pt in p.PizzaToppings)
                {
                    price += pt.Topping.Price;
                }
            }

            //Add the price of the pizza to add
            price += (newSize.Price + newCrust.Price);
            foreach (var t in newToppings)
            {
                price += t.Price;
            }

            return(price);
        }
        //[ValidateAntiForgeryToken]
        public IActionResult ConfirmOrder(PizzaViewModel pizzaViewModel) //model binding
        {
            //if (ModelState.IsValid) //validating that the requirements in PizzaViewModel are being met
            //{
            CrustModel crust = new CrustModel()
            {
                Name = pizzaViewModel.Crust
            };
            SizeModel size = new SizeModel()
            {
                Name = pizzaViewModel.Size
            };
            ToppingModel topping = new ToppingModel()
            {
                Name = pizzaViewModel.SelectedTopping
            };
            List <ToppingModel> ToppingList = new List <ToppingModel>()
            {
                topping
            };
            PizzaModel pizza = new PizzaModel()
            {
                Crust = crust, Size = size, Toppings = ToppingList
            };

            _db.Pizzas.Add(pizza);
            OrderModel order = new OrderModel();

            order.UserID = pizzaViewModel.UserID;


            //order.Pizzas.Add(new PizzaModel(){})
            _db.Orders.Add(order);
            _db.SaveChanges();
            return(RedirectToAction("Summary", "User", pizzaViewModel));//http 300-series status //should probably redirect to a user/cart/ or something like that
            //}
            //return View("MainOrder");
        }
Example #6
0
        // [ValidateAntiForgeryToken]
        public IActionResult Post(PizzaViewModel pizzaViewModel, int id) //model binding
        {
            var return_view = "";

            switch (id)
            {
            case 1:       // set return view for standard pizza orders

                return_view = "OrderStandard";
                break;

            case 2:       // set return view for custom pizza orders
                pizzaViewModel.Type = "Custom";
                return_view         = "OrderCustom";
                break;

            default:
                break;
            }
            ;

            if (ModelState.IsValid)                //  what is the validation? (add to viewmodel)
            {
                var        p = new PizzaFactory(); // use dependency injection
                PizzaModel domainPizzaModel = new PizzaModel();
                domainPizzaModel = p.Create();     // factory-created Domain PizzaModel

                if (pizzaViewModel.Types.Contains(pizzaViewModel.Type) || pizzaViewModel.Type == "Custom")
                {
                    switch (id)
                    {
                    case 1:                                                                                        //standard pizza chosen
                        pizzaViewModel.SelectedToppings.Clear();
                        pizzaViewModel.SelectedToppings.AddRange(pizzaViewModel.ToppingSets[pizzaViewModel.Type]); // add topping sets based on chosen pizza type
                        break;

                    case 2: //custom pizza chosen
                        if (pizzaViewModel.SelectedToppings.Contains("dummy"))
                        {
                            return(View(return_view, pizzaViewModel));      // no toppings were chosen at all, return to custom pizza order page
                        }
                        break;

                    default:
                        break;
                    }

                    var pricepertopping = 1.0m;
                    switch (id)
                    {
                    case 1:   // set prices for standard pizza orders
                        domainPizzaModel.Price = pizzaViewModel.Prices[pizzaViewModel.Type];
                        break;

                    case 2:                                                        // set prices for custom pizza orders
                        domainPizzaModel.Price  = pizzaViewModel.Prices["Cheese"]; //cheapest
                        domainPizzaModel.Price += pizzaViewModel.SelectedToppings.Count * pricepertopping;
                        break;

                    default:
                        break;
                    }
                    ;
                }
                else
                {
                    domainPizzaModel.Price = pizzaViewModel.Prices["Cheese"]; //cheapest
                    pizzaViewModel.SelectedToppings.AddRange(pizzaViewModel.ToppingSets["Cheese"]);
                }

                // bind PizzaViewModel to Domain PizzaModel
                domainPizzaModel.Name = pizzaViewModel.Type;

                var newC = new CrustModel();
                newC.Name = pizzaViewModel.Crust;
                domainPizzaModel.Crust = newC;

                var newS = new SizeModel();
                newS.Name             = pizzaViewModel.Size;
                domainPizzaModel.Size = newS;

                foreach (var t in pizzaViewModel.SelectedToppings)
                {
                    var newToppings = new List <ToppingModel>();
                    var newTopping  = new ToppingModel();
                    newTopping.Name = t;
                    domainPizzaModel.Toppings.Add(newTopping);
                }

                var repo_pizza = new PizzaRepository();
                repo_pizza.Create(domainPizzaModel, _db);

                return_view = "ThankYou";
                return(View(return_view));
            }


            return(View(return_view, pizzaViewModel));
        }
Example #7
0
 public void Add(CrustModel t)
 {
     _db.Crusts.Add(t);
     _db.SaveChanges();
 }
        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));
            }
        }
        public PizzaModel CreateSpecific(CrustModel c, SizeModel s, List <ToppingModel> t)
        {
            var p = new PizzaModel();

            return(p);
        }