protected void completeOrder(int userId, int transactionId)
        {
            using (StoreContent context = new StoreContent())
            {
                var order = (from c in context.Orders
                             where c.Id == transactionId && c.CustomerID == userId
                             select c).FirstOrDefault();

                var user = (from c in context.Customer
                            where c.Id == userId
                            select c).FirstOrDefault();

                var shipAdd = (from c in context.Address
                               where c.UserName == user.UserName && c.typeAdd == "Shipping"
                               select c).FirstOrDefault();

                order.OrderStatus = "Complete";
                order.OrderDate = DateTime.Now;

                Orders newOrder = context.Orders.Create();

                newOrder.CustomerID = userId;
                newOrder.SubTotal = 0;
                newOrder.OrderDate = DateTime.Now;
                newOrder.ShippingAddress = shipAdd.Address;
                newOrder.OrderStatus = "Active";

                context.Orders.Add(newOrder);

                context.SaveChanges();

                Session["cartID"] = newOrder.Id.ToString();

            }
        }
        public void ClearData(StoreContent content)
        {
            List<string> tablenames = content.Database.SqlQuery<string>("SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME NOT LIKE '%Migration%'").ToList();
            for (int i = 0; tablenames.Count > 0; i++)
            {
                try
                {
                    content.Database.ExecuteSqlCommand(string.Format("DELETE FROM {0}", tablenames.ElementAt(i % tablenames.Count)));
                    tablenames.RemoveAt(i % tablenames.Count);
                    i = 0;
                }
                catch
                {

                }
                content.SaveChanges();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (StoreContent content = new StoreContent())
            {
                ClearData(content);

                Category c = new Category();
                c.Id = 00;
                c.Description = "Climbing";
                content.Category.Add(c);

                c = new Category();
                c.Id = 01;
                c.Description = "Hiking";
                content.Category.Add(c);

                c = new Category();
                c.Id = 11;
                c.Description = "Apparel";
                content.Category.Add(c);

                Products p = new Products();
                p.ProductName = "Climbing Shoes";
                p.ProductDescription = "Shoes that won't weigh you down.";
                p.UnitPrice = 119.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.image = "/images/climbing-shoes.jpg";
                p.Stock = 49;
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Harness";
                p.ProductDescription = "Hang on";
                p.UnitPrice = 79.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 28;
                p.image = "/images/climbing-harness.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Carabiner";
                p.ProductDescription = "Attach yourself to what's important";
                p.UnitPrice = 19.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 150;
                p.image = "/images/climbing-carabiner.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Belay Device";
                p.ProductDescription = "Belay with ease.";
                p.UnitPrice = 99.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 100;
                p.image = "/images/climbing-belaydevice.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Quickdraw";
                p.ProductDescription = "Precisioned attachment.";
                p.UnitPrice = 14.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 25;
                p.image = "/images/climbing-quickdraw";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Rope";
                p.ProductDescription = "Strong durable rope for scaling cliffs.";
                p.UnitPrice = 199.99;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 200;
                p.image = "/images/climbing-rope.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Climbing Cams";
                p.ProductDescription = "For when the only thing to hold on to is between a rock and another rock.";
                p.UnitPrice = 300;
                p.CategoryID = 00;
                p.Featured = false;
                p.Stock = 300;
                p.image = "/images/climbing-cams.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Hiking boots";
                p.ProductDescription = "Never lose traction";
                p.UnitPrice = 110.00;
                p.CategoryID = 01;
                p.Featured = false;
                p.Stock = 115;
                p.image = "/images/hiking-boots.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Hiking Pack";
                p.ProductDescription = "Mystery bags of all bags";
                p.UnitPrice = 200.00;
                p.CategoryID = 01;
                p.Featured = false;
                p.Stock = 35;
                p.image = "/images/hiking-pack.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Hiking Sleeping Bag";
                p.ProductDescription = "Made of that material that will hug you back when you sleep.";
                p.UnitPrice = 140.00;
                p.CategoryID = 01;
                p.Featured = false;
                p.Stock = 60;
                p.image = "/images/hiking-sleepingbag.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Apparel-Jacket";
                p.ProductDescription = "Look ready to take on the climb.";
                p.UnitPrice = 170.00;
                p.CategoryID = 11;
                p.Featured = false;
                p.Stock = 180;
                p.image = "/images/apparel-jacket.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Apparel-Pants";
                p.ProductDescription = "The swiss army knife of pants.";
                p.UnitPrice = 80.00;
                p.CategoryID = 11;
                p.Featured = false;
                p.Stock = 360;
                p.image = "/images/apparel-pants.jpg";
                content.Products.Add(p);

                p = new Products();
                p.ProductName = "Apparel-Shorts";
                p.ProductDescription = "Like the pants but half the length.";
                p.UnitPrice = 60.00;
                p.CategoryID = 11;
                p.Featured = false;
                p.Stock = 360;
                p.image = "/images/apparel-shorts.jpg";
                //this image can be changed, i just couldn't find a picture of actual shorts
                content.Products.Add(p);

                content.SaveChanges();
            }
        }
        private void AddToCart(object sender, CommandEventArgs e)
        {
            if (Session["LoggedInId"] == null)
            {
                lblPageName.Text = "Please sign in or register to add items!";
                lblPageName.ForeColor = System.Drawing.Color.Red;
                return;
            }

            int addId = Convert.ToInt32(e.CommandArgument);
            int custId, cartId;
            bool hasCart = false;
            if (Session["LoggedInId"] == null)
                custId = 1;
            else
                custId = Int32.Parse(Session["LoggedInId"].ToString());

                if (Session["cartID"] == null)
            {
                using (StoreContent context = new StoreContent())
                {
                    var check = (from c in context.Orders
                                 where c.CustomerID == custId && c.OrderStatus == "Active"
                                 orderby c.Id descending
                                 select c).FirstOrDefault();
                    if(check!= null)
                    {
                        Session["cartID"] = check.Id;
                        hasCart = true;
                    }
                }
                if (!hasCart) {
                    var cart = new Orders();
                    cart.CustomerID = custId;
                    cart.OrderStatus = "Active";
                    cart.OrderDate = DateTime.Now;
                    cart.SubTotal = 0;
                    using (StoreContent context = new StoreContent())
                    {
                        context.Orders.Add(cart);
                        context.SaveChanges();
                        Session["cartID"] = cart.Id;
                    }
                }
            }
            cartId = Int32.Parse(Session["cartID"].ToString());
            using (StoreContent context = new StoreContent())
            {
                var cart = (from c in context.Orders
                            where c.Id == cartId
                            select c).FirstOrDefault();
                var item = (from p in context.Products
                            where p.Id == addId
                            select p).FirstOrDefault();
                var checkItem = (from c in context.OrderItem
                                 where c.ProductID == item.Id && c.OrderID == cart.Id
                                 select c).FirstOrDefault();
                if (checkItem != null)
                {
                    checkItem.Quantity++;
                }
                else
                {
                    var orditem = new OrderItem();
                    orditem.CustomerID = custId;
                    orditem.OrderID = cartId;
                    orditem.ProductID = item.Id;
                    orditem.Quantity = 1; //hard coded, can add function to add multiple items
                    context.OrderItem.Add(orditem);
                }
                if (cart != null)
                    cart.SubTotal += Decimal.Parse(item.UnitPrice.ToString());
                item.Stock--;   //remove from stock
                context.SaveChanges();
            }
        }
        protected void instantiateCart(int userId)
        {
            using (StoreContent context = new StoreContent())
            {
                var order = (from c in context.Orders
                             where c.CustomerID == userId && c.OrderStatus == "Active"
                             select c).FirstOrDefault();

                if (order != null)
                {
                    Session["cartID"] = order.Id.ToString();
                }
                else
                {

                    var user = (from c in context.Customer
                                where c.Id == userId
                                select c).First();

                    var shipAdd = (from c in context.Address
                                   where c.UserName == txtUsername.Text && c.typeAdd == "Shipping"
                                   select c).FirstOrDefault();

                    Orders newOrder = context.Orders.Create();

                    newOrder.CustomerID = userId;
                    newOrder.SubTotal = 0;
                    newOrder.OrderDate = DateTime.Now;
                    newOrder.ShippingAddress = shipAdd.Address;
                    newOrder.OrderStatus = "Active";

                    context.Orders.Add(newOrder);
                    context.SaveChanges();

                }
            }
            Response.Redirect(Request.RawUrl);
        }
        private void AddToCart(object sender, CommandEventArgs e)
        {
            if (Session["LoggedInId"] == null)
            {
                lblProductName.Text = "Please sign in or register to add items!";
                lblProductName.ForeColor = System.Drawing.Color.Red;
                return;
            }

            int addId = Convert.ToInt32(e.CommandArgument);
            int custId, cartId;
            bool hasCart = false;
            if (Session["LoggedInId"] == null)
                custId = 1;
            else
                custId = Int32.Parse(Session["LoggedInId"].ToString());

            if (Session["CartID"] == null)
            {
                using (StoreContent context = new StoreContent())
                {
                    var check = (from c in context.Orders
                                 where c.CustomerID == custId && c.OrderStatus == "Active"
                                 orderby c.Id descending
                                 select c).FirstOrDefault();
                    if (check != null)
                    {
                        Session["cartID"] = check.Id;
                        hasCart = true;
                    }
                }
                if (!hasCart)
                {
                    var cart = new Orders();
                    cart.CustomerID = custId;
                    cart.OrderStatus = "Active";
                    cart.OrderDate = DateTime.Now;
                    cart.SubTotal = 0;
                    using (StoreContent context = new StoreContent())
                    {
                        context.Orders.Add(cart);
                        context.SaveChanges();
                        Session["cartID"] = cart.Id;
                    }
                }
            }
            cartId = Int32.Parse(Session["cartID"].ToString());
            using (StoreContent context = new StoreContent())
            {
                int quantityToAdd;

                var cart = (from c in context.Orders
                            where c.Id == cartId
                            select c).FirstOrDefault();
                var item = (from p in context.Products
                            where p.Id == addId
                            select p).FirstOrDefault();
                var checkItem = (from c in context.OrderItem
                                 where c.ProductID == item.Id && c.OrderID == cart.Id
                                 select c).FirstOrDefault();

                if (checkItem != null)
                {
                    checkItem.Quantity = checkItem.Quantity + Int32.Parse(ddlQuantity.SelectedValue);
                    quantityToAdd = Int32.Parse(ddlQuantity.SelectedValue);
                }
                else
                {

                    var orditem = new OrderItem();
                    orditem.CustomerID = custId;
                    orditem.OrderID = cartId;
                    orditem.ProductID = item.Id;

                    if (Int32.Parse(ddlQuantity.SelectedValue) > 1)
                    {
                        quantityToAdd = Int32.Parse(ddlQuantity.SelectedValue);
                        orditem.Quantity = quantityToAdd;
                    }
                    else
                    {
                        quantityToAdd = 1;
                        orditem.Quantity = Int32.Parse(ddlQuantity.SelectedValue);
                    }

                    context.OrderItem.Add(orditem);
                }

                if (cart != null)
                {
                    cart.SubTotal += Decimal.Parse((item.UnitPrice * Int32.Parse(ddlQuantity.SelectedValue)).ToString()); //Subtotal now reflects unit price * quantity of units  --RID
                }
                item.Stock = item.Stock - quantityToAdd;   //remove from stock
                context.SaveChanges();

            }

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                using (StoreContent content = new StoreContent())
                {

                    var checkUsername = (from c in content.Customer
                                     where c.UserName == txtUsername.Text
                                     select c).FirstOrDefault();

                    if (checkUsername == null)
                    {

                        Customer newCustomer = content.Customer.Create();
                        newCustomer.FirstName = txtFirstName.Text;
                        newCustomer.LastName = txtLastName.Text;

                        //adding different type of addresses

                        //billing first
                        TypeofAddress add = new TypeofAddress();
                        add.UserName = txtUsername.Text;
                        add.typeAdd = "Billing";
                        add.Address = txtBillingAddress.Text;
                        add.City = txtBillingCity.Text;
                        add.State = ddlBillingState.SelectedItem.ToString();
                        add.Zip = Int32.Parse(txtBillingZip.Text);
                        content.Address.Add(add);

                        //shipping address now
                        add = new TypeofAddress();
                        add.UserName = txtUsername.Text;
                        add.typeAdd = "Shipping";
                        add.Address = txtShippingAddress.Text;
                        add.City = txtShippingCity.Text;
                        add.State = ddlShippingState.SelectedItem.ToString();
                        add.Zip = Int32.Parse(txtShippingZip.Text);
                        content.Address.Add(add);

                        newCustomer.email = txtEmail.Text;
                        newCustomer.UserName = txtUsername.Text;

                        //password will be hashed here
                        newCustomer.Password = FinalProject.SecuredPasswordHash.GenerateHash(txtPass1.Text);

                        if (chkNewsletter.Checked)
                        {
                            newCustomer.NewsLetter = true;
                        }
                        else
                        {
                            newCustomer.NewsLetter = false;
                        }

                        content.Customer.Add(newCustomer);
                        content.SaveChanges();

                        Session["LoggedInId"] = newCustomer.Id;
                        Response.Redirect("account.aspx?Id=" + newCustomer.Id.ToString());

                    } else
                    {
                        lblPageName.Text = "Username is already taken! Please choose another.";
                        lblPageName.ForeColor = System.Drawing.Color.Red;
                    }

                }
            }
        }
Esempio n. 8
0
        private void RemoveFromCart(object sender, CommandEventArgs e)
        {
            int remId = Convert.ToInt32(e.CommandArgument);
            int cartId = Int32.Parse(Session["cartID"].ToString());

            using (StoreContent context = new StoreContent())
            {
                var items = (from o in context.OrderItem
                            where o.OrderID == cartId && o.ProductID == remId
                            select o).ToList();
                var cart = (from c in context.Orders
                            where c.Id == cartId
                            select c).FirstOrDefault();
                var product = (from p in context.Products
                               where p.Id == remId
                               select p).First();
                foreach (OrderItem item in items)
                {
                    cart.SubTotal -= (decimal)(item.Quantity * product.UnitPrice);
                    product.Stock += item.Quantity;
                    context.OrderItem.Remove(item);
                    context.SaveChanges();
                }
            }
            Response.Redirect("cart.aspx");
        }