// GET: Order/Create
        public ActionResult Create(int id)
        {
            //int storeId = Int32.Parse((string)ViewData["StoreId"]);

            try
            {
                lib.Order  libOrder  = lib.Order.GetById(id);
                view.Order formOrder = view.Mapper.Map(libOrder);

                return(View(formOrder));
            }
            catch
            {
                return(View(new view.Order()));
            }
        }
Esempio n. 2
0
        /***** Library -> View *****/
        public static view.Order Map(lib.Order libOrder)
        {
            view.Order viewOrder = new view.Order
            {
                Id         = libOrder.Id,
                TimePlaced = libOrder.TimePlaced,
                TotalPrice = libOrder.TotalPrice
            };

            try
            {
                viewOrder.Location = lib.Location.GetById(libOrder.LocationId).Name;
                lib.User u = lib.User.GetById(libOrder.UserId);
                viewOrder.UserName = u.FirstName + " " + u.LastName;
            }
            catch (e.InvalidIdException e)
            {
                throw (e);
            }

            return(viewOrder);
        }
Esempio n. 3
0
        /***** View -> Library *****/
        public static lib.Order Map(view.Order viewOrder)
        {
            lib.Order libOrder = new lib.Order
            {
                TimePlaced = viewOrder.TimePlaced,
            };
            if (viewOrder.Id != 0)
            {
                libOrder.Id = viewOrder.Id;
            }

            try
            {
                libOrder.LocationId = lib.Location.GetByName(viewOrder.Location).Id;
                libOrder.UserId     = lib.User.GetByName(viewOrder.UserFirstName, viewOrder.UserLastName).Id;
            }
            catch (e.InvalidNameException e)
            {
                throw (e);
            }

            return(libOrder);
        }
        public ActionResult CreatePizza(view.Order formOrder, string AddPizza, string PlaceOrder, string Default, string Suggest)
        {
            lib.Order libOrder;
            if (formOrder.Id != 0)
            {
                libOrder = lib.Order.GetById(formOrder.Id);
            }
            else
            {
                libOrder = view.Mapper.Map(formOrder);
            }
            if (Suggest != null)
            {
                lib.Order temp = libOrder;
                libOrder = lib.Order.Orders.Where(o => o.UserId == libOrder.UserId).OrderByDescending(o => o.TimePlaced).FirstOrDefault();
                if (libOrder != null)
                {
                    lib.Order.Orders.Remove(libOrder);
                    lib.Order o = new lib.Order
                    {
                        LocationId = libOrder.LocationId,
                        UserId     = libOrder.UserId
                    };
                    foreach (var pizza in libOrder.Pizzas)
                    {
                        o.AddPizzaToOrder(pizza.Ingredients.Select(i => i.Name).ToList().ToArray());
                    }

                    return(RedirectToAction(nameof(Create), new { o.Id }));
                }
                else
                {
                    libOrder = temp;
                }
            }
            if (AddPizza != null)
            {
                if (libOrder.Pizzas.Count < 12 && libOrder.TotalPrice + formOrder.pizza.Price <= 500)
                {
                    // 2 hour check
                    lib.Order mostRecentOrder = lib.Order.Orders
                                                .Where(o => o.UserId == libOrder.UserId)
                                                .OrderByDescending(o => o.TimePlaced)
                                                .FirstOrDefault();
                    //if(mostRecentOrder != null)
                    //  if((DateTime.Now - mostRecentOrder.TimePlaced).Hours > 2)
                    libOrder.AddPizzaToOrder(formOrder.pizza);
                }
            }
            else if (PlaceOrder != null || Default != null)
            {
                if (Default != null)
                {
                    lib.User libUser = lib.User.GetById(libOrder.UserId);
                    if (libUser.DefaultLocationId != null)
                    {
                        libOrder.LocationId = (int)libUser.DefaultLocationId;
                    }
                }

                // Place the order
                try
                {
                    // Attempt to place the order
                    // Get all the ingredients required to place the order
                    List <string> ingredients = new List <string>();
                    foreach (var pizza in libOrder.Pizzas)
                    {
                        foreach (var ingredient in pizza.Ingredients)
                        {
                            ingredients.Add(ingredient.Name);
                        }
                    }
                    var inventory = ingredients.Select(s => new KeyValuePair <string, int>(s, ingredients.Where(s2 => s2 == s).Count())).ToList();
                    lib.Location.GetById(libOrder.LocationId).UpdateInventory(inventory);
                }
                catch (e.InsufficientIngredientException)
                {
                }
                lib.Order.OrderHasBeenPlace = true;
                libOrder.TimePlaced         = DateTime.Now;
                db.Order dbOrder;
                List <db.OrderJunction> orderJunctionList;
                List <db.PizzaJunction> pizzaJunctionList;
                dbOrder = db.Mapper.Map(libOrder, out orderJunctionList, out pizzaJunctionList);

                foreach (var pizzaJunction in pizzaJunctionList)
                {
                    orderRepo.pizzaJunctionRepo.Create(pizzaJunction);
                }
                foreach (var orderJunction in orderJunctionList)
                {
                    orderRepo.orderJunctionRepo.Create(orderJunction);
                }
                orderRepo.Create(dbOrder);
                orderRepo.SaveChanges();

                bool resync = false;
                return(RedirectToAction(nameof(Index), new { resync }));
            }

            return(RedirectToAction(nameof(Create), new { libOrder.Id }));
        }