public void CreateProfile()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var profile = new Profile();
            profile.Username = NAME;
            profile.ApplicationName = "PetShop..Businesss";
            profile.IsAnonymous = false;
            profile.LastActivityDate = DateTime.Now;
            profile.LastUpdatedDate = DateTime.Now;

            try
            {
                using (var context = new PetshopDataContext())
                {
                    context.Profile.InsertOnSubmit(profile);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            
            Assert.IsTrue(true);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        /// <summary>
        /// Update profile
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (var context = new PetshopDataContext())
            {
                /*var options = new DataLoadOptions();
                options.LoadWith<Profile>(p => p.AccountList);
                context.LoadOptions = options;*/

                var profile = context.Profile.GetProfile(User.Identity.Name);

                if (!string.IsNullOrEmpty(profile.Username) && AddressForm.IsValid)
                {
                    if (profile.AccountList.Count > 0)
                    {
                        Account account = profile.AccountList[0];
                        UpdateAccount(ref account, AddressForm.Address);
                    }
                    else
                    {
                        var account = new Account();
                        profile.AccountList.Add(account);
                        account.Profile = profile;

                        UpdateAccount(ref account, AddressForm.Address);
                    }

                    context.SubmitChanges();
                }
            }
            lblMessage.Text = "Your profile information has been successfully updated.<br>&nbsp;";
        }
 public static void ClearCart(List<Cart> items)
 {
     using (var context = new PetshopDataContext())
     {
         context.Cart.AttachAll(items);
         context.Cart.DeleteAllOnSubmit(items);
         context.SubmitChanges();
     }
 }
 public static void SetQuantity(List<Cart> items, string itemId, int quantity)
 {
     var item = items.FirstOrDefault(i => i.ItemId == itemId);
     using (var context = new PetshopDataContext())
     {
         context.Cart.Attach(item);
         item.Quantity = quantity;
         context.SubmitChanges();
     }
 }
 protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
 {
     using (var context = new PetshopDataContext())
     {
         string userName = ((System.Web.UI.WebControls.CreateUserWizard)sender).UserName;
         var profile = context.Profile.GetProfile(userName);
         if (null == profile)
         {
             profile = new Profile();
             profile.Username = userName;
             profile.ApplicationName = ".NET Pet Shop 4.0";
             profile.IsAnonymous = false;
             profile.LastActivityDate = DateTime.Now;
             profile.LastUpdatedDate = DateTime.Now;
             context.Profile.InsertOnSubmit(profile);
             context.SubmitChanges();
         }
     }
 }
        public static void Add(List<Cart> items, string itemId, int uniqueId, bool isShoppingCart)
        { 
            int index = 0;
            bool found = false;
            using (var context = new PetshopDataContext())
            {
                /*var options = new DataLoadOptions();
                options.LoadWith<Item>(i => i.Product);*/

                var item = items.FirstOrDefault(i => i.ItemId == itemId);

                if (item != null)
                {
                    context.Cart.Attach(item);
                    item.Quantity++;

                }
                else
                {
                    var cartItem = context.Item
                        .ByItemId(itemId)
                        .Fetch(i => i.Product)
                        .FirstOrDefault();
                    var profile = context.Profile.GetByKey(uniqueId);

                    var cart = new Cart();
                    cart.Profile = profile;
                    cart.ItemId = itemId;
                    cart.Name = cartItem.Name;
                    cart.ProductId = cartItem.Product.ProductId; // HERE
                    cart.IsShoppingCart = isShoppingCart;
                    cart.Price = cartItem.ListPrice ?? cartItem.UnitCost ?? 0;
                    cart.Type = cartItem.Product.Name;
                    cart.CategoryId = cartItem.Product.Category.CategoryId; // HERE
                    cart.Quantity = 1;
                    items.Add(cart);

                    context.Cart.InsertOnSubmit(cart);
                }
                context.SubmitChanges();
            }
        }
        public void UpdateSupplier()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Supplier supplier = null;
            using (var context = new PetshopDataContext())
            {
                supplier = context.Supplier.GetByKey(_supplierId);
                context.Supplier.Detach(supplier);
            }

            using (var context = new PetshopDataContext())
            {
                context.Supplier.Attach(supplier);
                supplier.Phone = "111-111-1111";
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Supplier.GetByKey(_supplierId).Phone == "111-111-1111");
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public void CreateSupplier()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var supplier = new Supplier();
            supplier.Name = NAME;
            supplier.Status = "AB";
            supplier.Addr1 = "One  Way";
            supplier.Addr2 = "Two  Way";
            supplier.City = "Dallas";
            supplier.State = "TX";
            supplier.Zip = "90210";
            supplier.Phone = "555-555-5555";

            try
            {
                using (var context = new PetshopDataContext())
                {
                    context.Supplier.InsertOnSubmit(supplier);
                    context.SubmitChanges();
                    context.Supplier.Detach(supplier);
                    _supplierId = supplier.SuppId;
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            Assert.IsTrue(true);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public void UpdateProduct()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Product product = null;
            using (var context = new PetshopDataContext())
            {
                product = context.Product.GetByKey(ID);
                context.Product.Detach(product);
            }

            using (var context = new PetshopDataContext())
            {
                context.Product.Attach(product);
                product.Description = "This is a ";
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Product.GetByKey(ID).Description == "This is a ");
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public void UpdateInventory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Inventory inventory = null;
            using (var context = new PetshopDataContext())
            {
                inventory = context.Inventory.GetByKey(ID);
                context.Inventory.Detach(inventory);
            }

            using (var context = new PetshopDataContext())
            {
                context.Inventory.Attach(inventory);
                inventory.Qty = 100;
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Inventory.GetByKey(ID).Qty == 100);
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        /// <summary>
        /// Bind controls to profile
        /// </summary>
        private void BindUser()
        {
            using (var context = new PetshopDataContext())
            {
                var profile = context.Profile.GetProfile(User.Identity.Name);

                if (string.IsNullOrEmpty(profile.Username))
                {
                    profile = new Profile();
                    profile.Username = User.Identity.Name;
                    profile.ApplicationName = ".NET Pet Shop 4.0";
                    profile.IsAnonymous = !User.Identity.IsAuthenticated;
                    profile.LastActivityDate = DateTime.Now;
                    profile.LastUpdatedDate = DateTime.Now;
                    context.Profile.InsertOnSubmit(profile);
                    context.SubmitChanges();
                }
                AddressForm.Address = new Address(profile);
            }
        }
        public void CreateCategory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var category = new Category();
            category.CategoryId = ID;
            category.Name = "";
            category.Description = "";

            try
            {
                using (var context = new PetshopDataContext())
                {
                    context.Category.InsertOnSubmit(category);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            Assert.IsTrue(true);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public static void MoveToCart(Profile profile, string itemId)
        {
            var item = profile.WishList.FirstOrDefault(i => i.ItemId == itemId);

            using (var context = new PetshopDataContext())
            {
                context.Cart.Attach(item);
                item.IsShoppingCart = true;
                context.SubmitChanges();
            }
            profile.WishList.Remove(item);
            profile.ShoppingCart.Add(item);

        }
        public static void SaveOrderLineItems(List<Cart> cart, int orderId)
        {
            int lineNum = 0;

            using (var context = new PetshopDataContext())
            {
                var order = context.Order.GetByKey(orderId);
                foreach (var item in cart)
                {
                    var lineItem = new LineItem
                    {
                        Order = order,
                        ItemId = item.ItemId,
                        LineNum = ++lineNum,
                        Quantity = item.Quantity,
                        UnitPrice = item.Price,
                    };
                    context.LineItem.InsertOnSubmit(lineItem);
                }
                context.SubmitChanges();
            }
        }
        public static void Remove(List<Cart> items, string itemId)
        {
            var item = items.FirstOrDefault(i => i.ItemId == itemId);

            using (var context = new PetshopDataContext())
            {
                var cart = context.Cart.GetByKey(item.CartId);
                context.Cart.DeleteOnSubmit(cart);
                context.SubmitChanges();
                //context.Cart.Delete(item.CartId);
            }
            items.Remove(item);
        }
        public void UpdateItem()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Item item = null;
            using (var context = new PetshopDataContext())
            {
                item = context.Item.GetByKey(ID);
                context.Item.Detach(item);
            }

            using (var context = new PetshopDataContext())
            {
                context.Item.Attach(item);
                item.ListPrice = 111;
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Item.GetByKey(ID).ListPrice == 111);
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public void UpdateProfile()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Profile profile = null;
            using (var context = new PetshopDataContext())
            {
                profile = context.Profile.GetProfile(NAME);
                context.Profile.Detach(profile);
            }

            using (var context = new PetshopDataContext())
            {
                context.Profile.Attach(profile);
                profile.IsAnonymous = true;
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Profile.GetProfile(NAME).IsAnonymous.Value);
            }
            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        public void UpdateCategory()
        {
            Stopwatch watch = Stopwatch.StartNew();
            
            Category category = null;
            using (var context = new PetshopDataContext())
            {
                category = context.Category.GetByKey(ID);
                context.Category.Detach(category);
            }

            using (var context = new PetshopDataContext())
            {
                context.Category.Attach(category);
                category.Description = "This is a .";
                context.SubmitChanges();
            }

            using (var context = new PetshopDataContext())
            {
                Assert.IsTrue(context.Category.GetByKey(ID).Description == "This is a .");
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
        /// <summary>
        /// Process the order
        /// </summary>
        protected void wzdCheckOut_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            using (var context = new PetshopDataContext())
            {
                var profile = context.Profile.GetProfile(User.Identity.Name);
                if (profile.ShoppingCart.Count > 0)
                {
                    // display ordered items
                    CartListOrdered.Bind(profile.ShoppingCart);

                    // display total and credit card information
                    ltlTotalComplete.Text = ltlTotal.Text;
                    ltlCreditCardComplete.Text = ltlCreditCard.Text;

                    #region Create Order

                    var order = new Order();

                    order.UserId = profile.UniqueID.ToString();
                    order.OrderDate = DateTime.Now;
                    order.CreditCard = GetCreditCard();
                    order.Courier = order.CreditCard.CardType;
                    order.TotalPrice = CartHelper.GetTotal(profile.ShoppingCart);
                    order.AuthorizationNumber = 0;
                    order.Locale = "en-us";

                    #region Shipping Information

                    order.ShipAddr1 = billingForm.Address.Address1;
                    order.ShipAddr2 = billingForm.Address.Address2;
                    order.ShipCity = billingForm.Address.City;
                    order.ShipState = billingForm.Address.State;
                    order.ShipZip = billingForm.Address.Zip;
                    order.ShipCountry = billingForm.Address.Country;
                    order.ShipToFirstName = billingForm.Address.FirstName;
                    order.ShipToLastName = billingForm.Address.LastName;

                    #endregion

                    #region Billing Information

                    order.BillAddr1 = shippingForm.Address.Address1;
                    order.BillAddr2 = shippingForm.Address.Address2;
                    order.BillCity = shippingForm.Address.City;
                    order.BillState = shippingForm.Address.State;
                    order.BillZip = shippingForm.Address.Zip;
                    order.BillCountry = shippingForm.Address.Country;
                    order.BillToFirstName = shippingForm.Address.FirstName;
                    order.BillToLastName = shippingForm.Address.LastName;

                    #endregion
                    context.Order.InsertOnSubmit(order);
                    context.SubmitChanges();

                    #endregion

                    int itemsOnBackOrder = 0;
                    //Decrement and check the Inventory.
                    foreach (Cart cart in profile.ShoppingCart)
                    {
                        var inventory = context.Inventory.GetByKey(cart.ItemId);

                        if (cart.Quantity > inventory.Qty)
                        {
                            itemsOnBackOrder += cart.Quantity - inventory.Qty;
                        }

                        inventory.Qty -= cart.Quantity;
                        context.SubmitChanges();
                    }

                    if (itemsOnBackOrder > 0)
                    {
                        ItemsOnBackOrder.Text = string.Format("<br /><p style=\"color:red;\"><b>Backorder ALERT:</b> {0} items are on backorder.</p>", itemsOnBackOrder);
                    }

                    CartHelper.SaveOrderLineItems(profile.ShoppingCart, order.OrderId);

                    // destroy cart
                    CartHelper.ClearCart(profile.ShoppingCart);
                }
                else
                {
                    lblMsg.Text =
                        "<p><br>Can not process the order. Your cart is empty.</p><p class=SignUpLabel><a class=linkNewUser href=Default.aspx>Continue shopping</a></p>";
                    wzdCheckOut.Visible = false;
                }
            }
        }
        public void CreateInventory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var inventory = new Inventory();
            inventory.ItemId = ID;
            inventory.Qty = 10;

            try
            {
                using (var context = new PetshopDataContext())
                {
                    context.Inventory.InsertOnSubmit(inventory);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            Assert.IsTrue(true);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }