Ejemplo n.º 1
0
        public ActionResult Create([Bind(Include = "ProductId,ProductName,Description,Price,Image,Quantity,CategoryId,Featured")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Category1", product.CategoryId);
            return(View(product));
        }
Ejemplo n.º 2
0
        public ActionResult Index(ProductModel model)
        {
            // TODO: Add product to cart in database.
            using (WebStoreDatabaseEntities e = new WebStoreDatabaseEntities())
            {
                //create line item
                OrderDetail detail = new OrderDetail();
                detail.OrderOty  = model.Quantity;
                detail.ProductId = model.ID;

                OrderHeader header = new OrderHeader();
                header.OrderDate = DateTime.UtcNow;

                //(ternary expression) Are there order headers? if yes, then do the thing, else = 1.
                header.OrderId = e.OrderHeaders.Any() ? e.OrderHeaders.Max(x => x.OrderId) + 1 : 1;
                detail.OrderId = header.OrderId;

                //Adds detail to order details
                header.OrderDetails.Add(detail);

                //TODO: Make sure people are registered and use THEIR customer ID.
                header.CustomerId = e.Customers.First().CustomerId;

                header.SubTotal  = model.Price * model.Quantity;
                detail.LineTotal = header.SubTotal;
                e.OrderHeaders.Add(header);
                e.SaveChanges();
            }
            return(RedirectToAction("Index", "Cart"));
        }
Ejemplo n.º 3
0
        public ActionResult Index(ProductModel model)
        {
            //TODO: new entity (using), create line item, create the header, add new line item to header,
            using (WebStoreDatabaseEntities e = new WebStoreDatabaseEntities())
            {
                //save cart as cookie so we can use this order on the checkout page
                Response.Cookies.Add(new HttpCookie("orderId", 1.ToString())
                {
                    Expires = DateTime.Now.AddMinutes(15)
                });

                // TODO: finish creating line item, etc...
                //model.ID = e.OrderDetails.

                e.SaveChanges();
            }
            return(RedirectToAction("Index", "Checkout"));
        }
Ejemplo n.º 4
0
        public ActionResult Index(CheckoutModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: if check out...
                using (WebStoreDatabaseEntities entities = new WebStoreDatabaseEntities())
                {
                    int orderNumber = int.Parse(Request.Cookies["OrderId"].Value);
                    var orderHeader = entities.OrderHeaders.Single(x => x.OrderId == orderNumber);
                    //looks at shipping table, gets method that equals model.method, then finds the ID for that method.
                    var shipMethod = entities.Shippings.Single(x => x.Method == model.ShippingMethod).ShippingMethodId;

                    var address = entities.Addresses.FirstOrDefault(
                        x => x.Line1 == model.ShippingAddress1 &&
                        x.Line2 == model.ShippingAddress2 &&
                        x.City == model.ShippingCity &&
                        x.State == model.ShippingState &&
                        x.Zipcode == model.ShippingZipcode);

                    if (address == null)
                    {
                        address = new Address
                        {
                            AddressId = entities.Addresses.Max(x => x.AddressId) + 1,
                            Line1     = model.ShippingAddress1,
                            Line2     = model.ShippingAddress2,
                            City      = model.ShippingCity,
                            State     = model.ShippingState,
                            Zipcode   = model.ShippingZipcode,
                        };
                        entities.Addresses.Add(address);
                    }
                    orderHeader.ShipToAddress = address.AddressId;
                    entities.SaveChanges();
                }
                return(RedirectToAction("Index", "Receipt"));
            }
            else
            {
                return(View(model));
            }
        }