public void SaveAddress(Address addrs)
        {
            int adressId = 0;
            if (addrs.AddressID == 0)
            {
                context.Addresses.Add(addrs);
                adressId = addrs.AddressID;
            }
            else
            {
                Address dbEntry = context.Addresses.Find(addrs.AddressID);
                if (dbEntry != null)
                {
                    dbEntry.Line1 = addrs.Line1;
                    dbEntry.Line2 = addrs.Line2;
                    dbEntry.Line3 = addrs.Line3;
                    dbEntry.City = addrs.City;
                    dbEntry.State = addrs.State;
                    dbEntry.Zip = addrs.Zip;
                    dbEntry.Country = addrs.Country;
                    dbEntry.UserId = addrs.UserId;

                }

                adressId = addrs.AddressID;
            }
            context.SaveChanges();
        }
Ejemplo n.º 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);
        }