コード例 #1
0
ファイル: CartController.cs プロジェクト: bobbyanne/capstone
        public ActionResult CreateOrder(CartPaymentVM form)
        {
            ActionResult response = null;

            if (ModelState.IsValid)
            {
                if (!form.PaymentPO.PayWithCash && !ValidCreditCard(form.PaymentPO.CreditCard))
                {
                    TempData["PaymentErrorMessage"] = "You must fill out the credit card info.";
                    response = RedirectToAction("Index", "Cart");
                }
                else
                {
                    try
                    {
                        List <PizzaPO> cart = Session["Cart"] as List <PizzaPO>;

                        if (cart.Count == 0)
                        {
                            response = RedirectToAction("Index", "Pizza");
                        }
                        else
                        {
                            bool isUserInfoValid = true;

                            if (form.PaymentPO.ForDelivery)
                            {
                                // TODO: Check for null
                                UserPO        currentUser = Mapping.UserMapper.UserDOtoUserPO(_userDAO.GetUserByID(GetSessionUserID()));
                                List <string> invalidInfo = GetInvalidDeliveryInfo(currentUser);

                                if (invalidInfo.Count > 0)
                                {
                                    isUserInfoValid = false;

                                    string errorMessage = "Some information is required before a delivery order can be submitted: ";
                                    errorMessage += string.Join(", ", invalidInfo);

                                    if (GetSessionRole() == 2)
                                    {
                                        errorMessage += " Your manager must update your account.";
                                    }

                                    TempData["ErrorMessage"] = errorMessage;

                                    response = RedirectToAction("Update", "Account", new { ID = GetSessionUserID() });
                                }
                            }

                            if (isUserInfoValid)
                            {
                                OrderDO newOrder = new OrderDO();

                                newOrder.IsDelivery = form.PaymentPO.ForDelivery;
                                newOrder.UserID     = GetSessionUserID();
                                newOrder.Status     = "Prepping";
                                newOrder.OrderDate  = DateTime.Now;

                                newOrder.Total = _pizzaBLO.GetCostOfPizzas(Mapping.PizzaMapper.PizzaPOListToPizzaBOList(cart));

                                if (form.PaymentPO.PayWithCash)
                                {
                                    newOrder.Paid = false;
                                }
                                else
                                {
                                    newOrder.Paid = true;
                                }

                                long createdOrderID = _orderDAO.CreateOrder(newOrder);

                                foreach (PizzaPO pizzaPO in cart)
                                {
                                    pizzaPO.OrderID = createdOrderID;
                                    PizzaDO pizzaDO = Mapping.PizzaMapper.PizzaPOtoPizzaDO(pizzaPO);

                                    if (!_pizzaDAO.AddNewPizza(pizzaDO))
                                    {
                                        Logger.Log("WARNING", "CartController", "CreateOrder",
                                                   "Unable to add a pizza from the cart to the database.");
                                    }
                                }

                                Session["Cart"]            = new List <PizzaPO>();
                                TempData["SuccessMessage"] = "Successfully created the order.";
                                response = RedirectToAction("MyOrders", "Order");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Logger.LogExceptionNoRepeats(exception);
                    }
                    finally
                    {
                        if (response == null)
                        {
                            response = RedirectToAction("Index", "Home");
                        }
                    }
                }
            }
            else
            {
                if (!ModelState.IsValidField("PaymentPO.CreditCard"))
                {
                    TempData["CreditCardError"] = "Invalid credit card number.";
                }

                TempData["PaymentErrorMessage"] = "Please fix the errors shown below";
                response = RedirectToAction("Index", "Cart");
            }

            return(response);
        }
コード例 #2
0
        public ActionResult CreateOrder(CartPaymentVM form)
        {
            ActionResult response = null;

            if (ModelState.IsValid)
            {
                // Make sure at least one payment method is correct.
                if (!form.PaymentPO.PayWithCash && !ValidCreditCard(form.PaymentPO.CreditCard))
                {
                    TempData["PaymentErrorMessage"] = "You must fill out the credit card info.";
                    response = RedirectToAction("Index", "Cart");
                }
                else // Otherwise, a payment method was supplied.
                {
                    try
                    {
                        // Get the cart from Session.
                        List <PizzaPO> cart = Session["Cart"] as List <PizzaPO>;

                        if (cart.Count == 0) // If the cart is empty..
                        {
                            // The user shouldn't be doing this.
                            response = RedirectToAction("Index", "Pizza");
                        }
                        else
                        {
                            // Sets up a variable to check if the user should update their account or not.
                            bool isUserInfoValid = true;

                            if (form.PaymentPO.ForDelivery) // If the user wants the order to be delivered to them.
                            {
                                // Get the current user based of the Session's UserID
                                UserPO currentUser = Mapping.UserMapper.UserDOtoUserPO(_userDAO.GetUserByID(GetSessionUserID()));

                                // Get any invalid info that is required for a delivery order to be placed.
                                List <string> invalidInfo = GetInvalidDeliveryInfo(currentUser);

                                // ** This is a fallback if the AJAX version doesn't work when the user is creating a delivery order. **
                                if (invalidInfo.Count > 0)   // If there is any invalid info
                                {
                                    isUserInfoValid = false; // The user has not entered the correct information for a delivery order.

                                    string errorMessage = "Some information is required before a delivery order can be submitted: ";
                                    errorMessage += string.Join(", ", invalidInfo);

                                    if (GetSessionRole() == 2) // If the current user is a driver..
                                    {
                                        errorMessage += " Your manager must update your account.";
                                    }

                                    TempData["ErrorMessage"] = errorMessage;

                                    response = RedirectToAction("Update", "Account", new { ID = GetSessionUserID() });
                                }
                            }

                            if (isUserInfoValid) // If the user's information is correct
                            {
                                // Instantiate a new Order.
                                OrderDO newOrder = new OrderDO();

                                // Fill some of the order's properties.
                                newOrder.IsDelivery = form.PaymentPO.ForDelivery;
                                newOrder.UserID     = GetSessionUserID();
                                newOrder.Status     = "Prepping";
                                newOrder.OrderDate  = DateTime.Now;

                                // Get the total for the order.
                                newOrder.Total = _pizzaBLO.GetCostOfPizzas(Mapping.PizzaMapper.PizzaPOListToPizzaBOList(cart));

                                if (form.PaymentPO.PayWithCash)
                                {
                                    newOrder.Paid = false;
                                }
                                else
                                {
                                    newOrder.Paid = true;
                                }

                                // Get the newly created primary key after running the insert command.
                                long createdOrderID = _orderDAO.CreateOrder(newOrder);

                                if (createdOrderID > -1)
                                {
                                    // Add each pizza in the cart to the new order.
                                    foreach (PizzaPO pizzaPO in cart)
                                    {
                                        pizzaPO.OrderID = createdOrderID;
                                        PizzaDO pizzaDO = Mapping.PizzaMapper.PizzaPOtoPizzaDO(pizzaPO);

                                        if (!_pizzaDAO.AddNewPizza(pizzaDO))
                                        {
                                            Logger.Log("WARNING", "CartController", "CreateOrder",
                                                       "Unable to add a pizza from the cart to the database.");
                                        }
                                        else
                                        {
                                        }
                                    }

                                    Session["Cart"]            = new List <PizzaPO>(); // Create a new cart.
                                    TempData["SuccessMessage"] = "Successfully created the order.";
                                    response = RedirectToAction("MyOrders", "Order");
                                }
                                else // An execption didn't occur but the order wasn't created.
                                {
                                    TempData["ErrorMessage"] = "Something happened while creating your order, please try again.";
                                    response = RedirectToAction("Index", "Cart");
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Logger.LogExceptionNoRepeats(exception);
                    }
                    finally
                    {
                        if (response == null)
                        {
                            response = RedirectToAction("Index", "Home");
                        }
                    }
                }
            }
            else
            {
                // If the credit card field was not in a correct format
                if (!ModelState.IsValidField("PaymentPO.CreditCard"))
                {
                    TempData["CreditCardError"] = "Invalid credit card number.";
                }
                else
                {
                }

                TempData["PaymentErrorMessage"] = "Please fix the errors shown below";
                response = RedirectToAction("Index", "Cart");
            }

            return(response);
        }