public ActionResult Create(lib.Order formOrder)
        {
            try
            {
                lib.Order libOrder = lib.Order.GetById(formOrder.Id);

                db.Order dbOrder;
                List <db.OrderJunction> orderJunctionList;
                List <db.PizzaJunction> pizzaJunctionList;
                dbOrder = db.Mapper.Map(libOrder, out orderJunctionList, out pizzaJunctionList);

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

                bool resync = false;
                return(RedirectToAction(nameof(Index), new { resync }));
            }
            catch
            {
                return(View());
            }
        }
Exemple #2
0
        public static lib.Order Map(db.Order dbOrder)
        {
            lib.Order libOrder = new lib.Order
            {
                Id         = dbOrder.Id,
                LocationId = dbOrder.LocationId,
                UserId     = dbOrder.UserId,
                TimePlaced = dbOrder.TimePlaced
            };
            List <db.OrderJunction> orderJunctions = orderJunctionRepo.GetAllOrderJunctions()
                                                     .Where(o => o.OrderId == dbOrder.Id).ToList();

            foreach (var orderJunction in orderJunctions)
            {
                List <db.PizzaJunction> pizzaJunctions = pizzaRepo.GetAllPizzaJunctions()
                                                         .Where(p => p.PizzaId == orderJunction.PizzaId).ToList();
                lib.Pizza libPizza = new lib.Pizza
                {
                    Id = orderJunction.PizzaId
                };
                foreach (var pizzaJunction in pizzaJunctions)
                {
                    for (int i = 0; i < pizzaJunction.Count; ++i)
                    {
                        libPizza.AddIngredientsToPizza(lib.Ingredient.GetById(pizzaJunction.IngredientId).Name);
                    }
                }
                libOrder.AddPizzaToOrder(libPizza);
            }
            return(libOrder);
        }
        // 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()));
            }
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        public static db.Order Map(lib.Order libOrder,
                                   out List <db.OrderJunction> orderJunctionList,
                                   out List <db.PizzaJunction> pizzaJunctionList)
        {
            db.Order dbOrder = new db.Order
            {
                Id         = libOrder.Id,
                LocationId = libOrder.LocationId,
                UserId     = libOrder.UserId,
                TimePlaced = libOrder.TimePlaced,
                TotalPrice = libOrder.TotalPrice
            };

            orderJunctionList = new List <db.OrderJunction>();
            pizzaJunctionList = new List <db.PizzaJunction>();
            foreach (var pizza in libOrder.Pizzas)
            {
                db.OrderJunction orderJunction = new db.OrderJunction
                {
                    OrderId = libOrder.Id,
                    PizzaId = pizza.Id
                };
                orderJunctionList.Add(orderJunction);
                foreach (var ingredient in pizza.Ingredients.Distinct())
                {
                    db.PizzaJunction pizzaJunction = new db.PizzaJunction
                    {
                        PizzaId      = pizza.Id,
                        IngredientId = ingredient.Id,
                        Count        = pizza.Ingredients.Where(i => i.Id == ingredient.Id).Count()
                    };
                    pizzaJunctionList.Add(pizzaJunction);
                }
            }
            return(dbOrder);
        }
        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 }));
        }