コード例 #1
0
        public ActionResult Checkout(OrderServiceModelForShoppingCart model)
        {
            var cart = ShoppingCartService.GetCart(this.HttpContext);

            if (cart.GetCartItems().Count() == 0)
            {
                TempData.AddErrorMessage(GlobalResources.ControllerOrderCheckOutErrorText);

                return(this.RedirectToAction(
                           nameof(ShoppingCartController.Index),
                           "ShoppingCart",
                           new { area = string.Empty }));
            }

            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            cart.CreateOrder(model);

            TempData.AddSuccessMessage(GlobalResources.ControllerOrderCheckOutSuccsessText);

            return(this.RedirectToAction(
                       nameof(HomeController.Index),
                       "Home",
                       new { area = string.Empty }));
        }
コード例 #2
0
        public void CreateOrder(OrderServiceModelForShoppingCart order)
        {
            decimal orderTotal = 0;

            var cartItems = this.GetCartItems();
            var orderData = new Order
            {
                Id          = order.Id,
                City        = order.City,
                AddressLine = order.AddressLine,
                Email       = order.Email,
                FirstName   = order.FirstName,
                LastName    = order.LastName,
                PhoneNumber = order.PhoneNumber,
                ZipCode     = order.ZipCode,
                OrderDate   = DateTime.UtcNow
            };

            // Iterate over the items in the cart, adding the order details for each
            foreach (var item in cartItems)
            {
                var orderDetail = new OrderDetail
                {
                    GameId    = item.GameId,
                    OrderId   = orderData.Id,
                    Price     = item.Game.Price,
                    Amount    = item.Amount,
                    GameTitle = item.GameTitle
                };

                // Set the order total of the shopping cart
                orderTotal += (item.Amount * item.Game.Price);
                orderData.OrderDetails.Add(orderDetail);
                this.data.OrderDetails.Add(orderDetail);
            }

            // Set the order's total to the orderTotal count
            orderData.OrderTotal = orderTotal;

            // Save the order
            this.data.Orders.Add(orderData);

            // Empty the shopping cart
            this.EmptyCart();
        }