public SimpleMembershipInitializer()
            {
                using (var context = new UsersContext())
                    context.UserProfiles.Find(1);

                if (!WebSecurity.Initialized)
                    WebSecurity.InitializeDatabaseConnection("EFDbContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
        public ViewResult Checkout(Cart cart)
        {
            var context = new UsersContext();
            var username = User.Identity.Name;
            var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);

            return View(new CartCheckoutViewModel
            {
                User = user,
                Cart = cart,
                DeliveryTypes = deliveryRepository.Deliveries.ToList(),
                Voucher = null
            });
            
            //return View(user);
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                  //  WebSecurity.InitializeDatabaseConnection("EFDbContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public ViewResult ConfirmCheckout(Cart cart, int deliveryId)
        {
            Delivery delivery = deliveryRepository.Deliveries.FirstOrDefault(d => d.DeliveryId == deliveryId);
            Order order = new Order();
            order.PriceBeforeVouchers = cart.GetTotalValue();
            order.UserId = WebSecurity.CurrentUserId;
            order.TimeSubmitted = DateTime.Now;
            
            order.DeliveryId = deliveryId;
            order.Subtotal = order.PriceBeforeVouchers + delivery.Cost;
            order.Total = order.Subtotal;
            order.Status = "Processed";
            orderRepository.SaveOrder(order);
            int orderId = orderRepository.Orders.FirstOrDefault(o => o.TimeSubmitted == order.TimeSubmitted).OrderId;
            List<Pizza> pizzas = new List<Pizza>();

            foreach(var cartLine in cart.Lines){
                for(int i=0;i<cartLine.Quantity;i++)
                {
                    Orderline orderline = new Orderline();
                    orderline.PizzaId = cartLine.Pizza.PizzaId;
                    orderline.OrderlinePrice = cartLine.Pizza.Price * cartLine.Quantity;
                    orderline.UserId = WebSecurity.CurrentUserId;
                    orderline.OrderId = order.OrderId;
                    orderlineRepository.SaveOrderline(orderline);
                    pizzas.Add(repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId));
                    Pizza pizza = repository.Pizzas.FirstOrDefault(p => p.PizzaId == orderline.PizzaId);
                    if (pizza.Name == "Create Your Own")
                    {
                        foreach (var topping in Session["ToppingList"] as List<Topping>)
                        {
                            PizzaToppingOrder pizzaToppingOrder = new PizzaToppingOrder();
                            pizzaToppingOrder.OrderlineId = orderline.OrderlineId;
                            pizzaToppingOrder.ToppingId = topping.ToppingId;
                            pizzaToppingOrderRepository.SavePizzaToppingOrder(pizzaToppingOrder);
                        }
                    }
                }
            }

            cart.Clear();
            ViewBag.PriceBeforeVouchers = order.PriceBeforeVouchers;
            
            ViewBag.DeliveryCost = delivery.Cost;
            ViewBag.PriceIncDelivery = order.PriceBeforeVouchers + delivery.Cost;
            ViewBag.DeliveryType = delivery.DeliveryType;
            
            var context = new UsersContext();
            var username = User.Identity.Name;
            var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);

            List<Topping> toppings = Session["ToppingList"] as List<Topping>;

            return View(new ConfirmCheckoutViewModel
            {
                User = user,
                Order = order,
                Pizzas =  pizzas, //orderlineRepository.Orderlines.Where(ol => ol.OrderId == order.OrderId).ToList()
                Toppings = toppings
            });
            //return View(user);
        }