public void SaveOrder(Order order)
 {
     using (var context = new EFDbContext())
     {
         var _order = new Order
         {
             AddressId = order.AddressId,
             UserId = order.UserId,
             OrderDate = DateTime.Now,
             OrderTotal = order.OrderTotal
         };
         context.Orders.Add(_order);
         foreach (var detail in order.OrderDetails)
         {
             var _orderDetail = new OrderDetail
             {
                 ProductId = detail.ProductId,
                 Quantity = detail.Quantity,
                 SizeId = detail.SizeId,
                 ColorId = detail.ColorId,
                 ManufacturerId = detail.ManufacturerId,
                 SubTotal = detail.SubTotal,
                 Order = _order
             };
             context.OrderDetails.Add(_orderDetail);
         }
        context.SaveChanges();
     }
 }
예제 #2
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
            {
                ModelState.AddModelError("", "Sorry, your cart is empty!");
            }

            if (ModelState.IsValid)
            {
                var addrs = new Address
                {
                    Name = shippingDetails.Name,
                    Line1 = shippingDetails.Line1,
                    Line2 = shippingDetails.Line2,
                    Line3 = shippingDetails.Line3,
                    City = shippingDetails.City,
                    State = shippingDetails.State,
                    Zip = shippingDetails.Zip,
                    Country = shippingDetails.Country,
                    UserId = shippingDetails.UserId
                };

                _repository.SaveAddress(addrs);

                if (addrs.AddressID > 0)
                {

                    var order = new Order
                    {
                        AddressId = addrs.AddressID,
                        UserId = shippingDetails.UserId,
                        OrderDate = DateTime.Now
                    };

                    var orderDetails = new List<OrderDetail>();

                    foreach (var line in cart.Lines)
                    {
                        var orderDetail = new OrderDetail
                        {
                            ProductId = line.Product.ProductID,
                            Quantity = line.Quantity,
                            SizeId = line.SizeId,
                            ColorId = line.ColorId,
                            ManufacturerId = line.ManufactererId,
                            SubTotal = (line.Product.Price * line.Quantity)
                        };

                        order.OrderTotal += orderDetail.SubTotal;

                        orderDetails.Add(orderDetail);
                    }

                    order.OrderDetails = orderDetails;

                    _repository.SaveOrder(order);
                }

                return View("Completed", new ShippingViewModel
                {
                    crt = cart,
                    shipDtls = shippingDetails
                });
            }

            return View(shippingDetails);
        }