예제 #1
0
        public ActionResult AddToDatabase(OrderViewModel formResponse)
        {
            // variabel for seats
            int seats = 0;

            // Looking for session in cart
            CartModel cart = (CartModel)Session["Cart"];

            if (cart == null)
            {
                return(RedirectToAction("CartEmpty"));
            }

            // Put data from form into Order class
            Order orderFromForm = formResponse.Order;

            // find existing order
            Order existingOrder = _orderRepo.FindOrder(formResponse.Order.Email);

            // If there is no order, there will be one created with the information
            if (existingOrder == null)
            {
                // Creating Account Order
                _orderRepo.CreateOrder(orderFromForm);
                // Now that an id had been created for order, the id can be passed into orderitem.
                orderFromForm = _orderRepo.FindOrder(formResponse.Order.Email);
                for (int i = 0; i < cart.OrderItems.Count; i++)
                {
                    // Giving OrderItem a Orderid from the form
                    cart.OrderItems[i].OrderID = orderFromForm.ID;

                    // Creating a new Order Item
                    _orderRepo.CreateOrderItem(cart.OrderItems[i]);

                    // Changing the amount of seats
                    seats = cart.OrderItems[i].Event.SeatsAvailable - cart.OrderItems[i].Amount;

                    //Assign value to event
                    cart.OrderItems[i].Event.SeatsAvailable = seats;

                    // Update in the database
                    _orderRepo.UpDateEvent(cart.OrderItems[i].Event);
                }
            }
            // passing the order id in the orderitem and creating database record
            else
            {
                // In this case there already is a AccountOrder
                for (int i = 0; i < cart.OrderItems.Count; i++)
                {
                    // Giving OrderItem a Orderid from the form
                    cart.OrderItems[i].OrderID = existingOrder.ID;

                    // Creating a new Order Item
                    _orderRepo.CreateOrderItem(cart.OrderItems[i]);

                    // cart.OrderItems[i].Event.ID =

                    // Changing the amount of seats
                    seats = cart.OrderItems[i].Event.SeatsAvailable - cart.OrderItems[i].Amount;

                    //Assign value to event
                    cart.OrderItems[i].Event.SeatsAvailable = seats;

                    // Update in the database
                    _orderRepo.UpDateEvent(cart.OrderItems[i].Event);
                }
            }
            return(RedirectToAction("Index", "Home"));
        }