コード例 #1
0
 public ActionResult SearchLocation(LocationW locationW)
 {
     try
     {
         var location = Repo.GetLocation(locationW.LocationName);
         if (location == null)
         {
             ModelState.AddModelError("", "Location doesn't exist");
             return(View());
         }
         else
         {
             return(RedirectToAction(nameof(LocationHistory), new { id = location.Id }));
         }
     }
     catch
     {
         return(View());
     }
 }
コード例 #2
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));
            }
        }