예제 #1
0
        public ActionResult CheckOut()
        {
            List <CartItem> cart = (List <CartItem>)Session["cart"];

            List <Product>        products   = new List <Product>();
            List <int>            quantities = new List <int>();
            List <ActivationCode> codeLists  = new List <ActivationCode>();

            for (int i = 0; i < cart.Count(); i++)
            {
                //adding products in the cart to a new list of product
                products.Add(cart[i].Product);
                quantities.Add(cart[i].Quantity);
                int productId = cart[i].Product.ProductId;
                //for each productId, a new list of codes will be generated, with number of elements equal to
                //the quantity of that product
                List <string> codes = new List <string>();
                for (int j = 0; j < cart[i].Quantity; j++)
                {
                    //activation codes for that products will be generated and saved to the list
                    codes.Add(Guid.NewGuid().ToString());
                }
                ActivationCode actCode = new ActivationCode(productId, codes);
                codeLists.Add(actCode);
            }
            OrderDetail orderDetail = new OrderDetail(products, quantities, codeLists);
            Customer    customer    = (Customer)Session["customer"];
            int         customerId  = Convert.ToInt32(customer.CustomerId);
            Order       order       = new Order()
            {
                CustomerId  = customerId,
                OrderDate   = DateTime.Now,
                OrderDetail = orderDetail
            };

            //order details are saved to the database by OrderData.SaveOrder(order Order) method
            OrderData.SaveOrder(order);
            int orderId = OrderData.GetOrderId(order);

            Session["cart"] = null;
            return(RedirectToAction("Index", "Order", new { orderId = orderId }));
        }