コード例 #1
0
        public ActionResult UserHistory(int id)
        {
            var user   = Repo.GetUserById(id);
            var orders = Repo.GetUserOrders(id);

            var UserOrders = orders.Select(x => new OrderW
            {
                Id           = x.Id,
                LocationName = x.Location.LocationName,
                User         = new UserW
                {
                    Id        = x.User.Id,
                    FirstName = x.User.FirstName,
                    LastName  = x.User.LastName
                },
                Pizzas      = OrderW.Map(Repo.GetPizzasFromOder(x.Id)),
                TimeOfOrder = x.OrderTime
            });

            TempData["Id"]        = "" + id;
            TempData["FirstName"] = user.FirstName;
            TempData["LastName"]  = user.LastName;

            return(View(UserOrders));
        }
コード例 #2
0
        public ActionResult LocationOrderDetails(int id)
        {
            var    order    = Repo.GetOrderById(id);
            OrderW orderWeb = new OrderW
            {
                Id           = order.Id,
                LocationName = order.Location.LocationName,
                User         = new UserW
                {
                    Id        = order.User.Id,
                    FirstName = order.User.FirstName,
                    LastName  = order.User.LastName
                },
                Pizzas      = OrderW.Map(Repo.GetPizzasFromOder(order.Id)),
                TimeOfOrder = order.OrderTime,
                Price       = Location.OrderPrice(Repo.GetPizzasFromOder(order.Id))
            };

            return(View(orderWeb));
        }
コード例 #3
0
        public ActionResult DisplayOrderHistoryByLatest()
        {
            var orders = Repo.GetAllOrdersLatest();

            var ordersByLatest = orders.Select(x => new OrderW
            {
                Id           = x.Id,
                LocationName = x.Location.LocationName,
                User         = new UserW
                {
                    Id        = x.User.Id,
                    FirstName = x.User.FirstName,
                    LastName  = x.User.LastName
                },
                Pizzas      = OrderW.Map(Repo.GetPizzasFromOder(x.Id)),
                TimeOfOrder = x.OrderTime
            });

            return(View(ordersByLatest));
        }
コード例 #4
0
        public ActionResult NewOrder(OrderW webOrder, IFormCollection collection)
        {
            TimeSpan timeSpan;

            // get all of the data from TempData and collection
            var location  = collection["Location"];
            var orderName = TempData.Get <String>("orderName");
            var orderWeb  = TempData.Get <OrderW>(orderName);

            TempData.Put("orderName", orderName);
            TempData.Put(orderName, orderWeb);

            try
            {
                timeSpan = DateTime.Now.Subtract(Repo.GetLastOrderFromLocation(orderWeb.User.Id, location).OrderTime);
            }
            catch (Exception E)
            {
                TimeSpan time1 = TimeSpan.FromHours(1);
                TimeSpan ts    = DateTime.Now.TimeOfDay;
                timeSpan = ts.Add(time1);
            }

            if (timeSpan.Hours >= 2)
            {
                // Place the orderName and Order in the TempData in case the order cannnot be completed
                TempData.Put("orderName", orderName);
                TempData.Put(orderName, orderWeb);

                // Get the location from the db
                var locationOrder = Repo.GetLocation(location);

                // And make a LocationW object to check if there are enough ingridients
                LocationW LocationW = new LocationW
                {
                    Id           = locationOrder.Id,
                    LocationName = locationOrder.LocationName,
                    DoughQ       = locationOrder.DoughQ,
                    SouceQ       = locationOrder.SouceQ,
                    CheeseQ      = locationOrder.CheeseQ,
                    PepperoniQ   = locationOrder.PepperoniQ
                };

                // get all the pizzaz in Data.Pizza for convenience
                List <Data.Pizza> pizzas = OrderW.Map(orderWeb.Pizzas);

                // Assign values to the location and time of the order
                orderWeb.LocationName = location;
                orderWeb.TimeOfOrder  = DateTime.Now;

                // Get the id of the location
                int locationId = Repo.GetLocationId(location);

                // Make an Data.Orders object to insert into the db
                Data.Orders orderD = OrderW.Map(orderWeb, locationId);

                if (LocationW.EnoughIngridients(pizzas))
                {
                    // Substract ingridients from the location
                    LocationW.SubstractIngridients(pizzas);

                    // Update the location in the db
                    Repo.UpdateLocation(LocationW.Map(LocationW));

                    // Add order to the db
                    Repo.AddOrder(orderD);

                    // save changes in the db
                    Repo.Save();

                    // Get the id of the order
                    int orderId = Repo.GetOrderId(orderD);

                    // Get the id of all the pizzas in the order (create a new pizza entry in the db if it doesn't exist)
                    List <int> pizzaIds = Repo.AddPizzas(pizzas);

                    // Add data to the junction table
                    foreach (var item in pizzaIds)
                    {
                        Repo.AddPizzaOrders(orderId, item);
                    }

                    // Save changes to the db
                    Repo.Save();

                    // Get the price of the order
                    decimal price = Library.Location.OrderPrice(pizzas);

                    // Place the id and price in the orderWeb obj
                    orderWeb.Id    = orderId;
                    orderWeb.Price = price;

                    // Insert the data into TempData to get it in the OrderReview
                    TempData.Put("orderName", orderName);
                    TempData.Put(orderName, orderWeb);

                    // Let the user review his Order
                    return(RedirectToAction(nameof(OrderReview), new { id = orderWeb.User.Id }));
                }
                else
                {
                    ModelState.AddModelError("", "Sorry but that location does not have enough ingridients to complete your order");
                    return(NewOrder(orderName));
                    //return RedirectToAction(nameof(NewOrder), new { newOrder = orderName });
                }
            }
            else
            {
                ModelState.AddModelError("", $"Your last order from this locations was {timeSpan.Hours} : {timeSpan.Minutes} : {timeSpan.Seconds} ago." +
                                         $"Need to wait at least 2 hours to order from the same location");
                return(NewOrder(orderName));
            }
        }