Exemplo n.º 1
0
        /// <summary>
        /// Method to retrieve and cache category name by its ID
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Category name</returns>
        public static string GetCategoryName(string categoryId)
        {
            if (!enableCaching)
            {
                string categoryName = String.Empty;
                using (var context = new PetShopDataContext())
                {
                    categoryName = context.Category.GetByKey(categoryId).Name;
                }
                return(categoryName);
            }

            string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);

            // Check if the data exists in the data cache
            var data = (string)HttpRuntime.Cache[cacheKey];

            if (data == null)
            {
                // Caching duration from Web.config
                int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]);

                // If the data is not in the cache then fetch the data from the business logic tier
                using (var context = new PetShopDataContext())
                {
                    data = context.Category.GetByKey(categoryId).Name;
                }

                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
                HttpRuntime.Cache.Add(cacheKey, data, null, DateTime.Now.AddHours(cacheDuration),
                                      Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }

            return(data);
        }
Exemplo n.º 2
0
        /// <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.UniqueID = profile.UniqueID;

                        UpdateAccount(ref account, AddressForm.Address);
                    }

                    context.SubmitChanges();
                }
            }
            lblMessage.Text = "Your profile information has been successfully updated.<br>&nbsp;";
        }
Exemplo n.º 3
0
        public void UpdateCategory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Category category = null;

            using (var context = new PetShopDataContext())
            {
                category = context.Category.GetByKey(ID);
                category.Detach();
            }

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

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

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 4
0
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string itemId = Request.QueryString["addItem"];
                if (!string.IsNullOrEmpty(itemId))
                {
                    Profile profile = null;
                    using (var context = new PetShopDataContext())
                    {
                        profile = context.Profile.GetProfile(Page.User.Identity.Name);

                        if (profile != null)
                        {
                            profile.Detach();
                        }
                    }

                    if (profile != null && !string.IsNullOrEmpty(profile.Username))
                    {
                        CartHelper.Add(profile.WishList, itemId, profile.UniqueID, false);
                    }

                    // Redirect to prevent duplictations in the wish list if user hits "Refresh"
                    Response.Redirect("~/WishList.aspx", true);
                }
            }
        }
        /// <summary>
        /// Handler for Delete/Move buttons
        /// </summary>
        protected void CartItem_Command(object sender, CommandEventArgs e)
        {
            var profile = new Data.Profile();

            using (var context = new PetShopDataContext())
            {
                profile = context.Profile.GetProfile(Page.User.Identity.Name);
                profile.Detach();
            }
            if (!string.IsNullOrEmpty(profile.Username))
            {
                switch (e.CommandName)
                {
                case "Del":
                    CartHelper.Remove(profile.WishList, e.CommandArgument.ToString());
                    break;

                case "Move":
                    CartHelper.MoveToCart(profile, e.CommandArgument.ToString());
                    break;
                }
            }

            BindCart();
        }
Exemplo n.º 6
0
        public void UpdateInventory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Inventory inventory = null;

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

            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);
        }
Exemplo n.º 7
0
        public void UpdateProduct()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Product product = null;

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

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

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

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Calculate total
 /// </summary>
 protected void BtnTotal_Click(object sender, ImageClickEventArgs e)
 {
     using (var context = new PetShopDataContext())
     {
         var profile = context.Profile.GetProfile(Page.User.Identity.Name);
         if (!string.IsNullOrEmpty(profile.Username))
         {
             TextBox     txtQuantity;
             ImageButton btnDelete;
             int         qty = 0;
             foreach (RepeaterItem row in repShoppingCart.Items)
             {
                 txtQuantity = (TextBox)row.FindControl("txtQuantity");
                 btnDelete   = (ImageButton)row.FindControl("btnDelete");
                 if (int.TryParse(WebUtility.InputText(txtQuantity.Text, 10), out qty))
                 {
                     if (qty > 0)
                     {
                         CartHelper.SetQuantity(profile.ShoppingCart, btnDelete.CommandArgument, qty);
                     }
                     else if (qty == 0)
                     {
                         CartHelper.Remove(profile.ShoppingCart, btnDelete.CommandArgument);
                     }
                 }
             }
         }
     }
     BindCart();
 }
Exemplo n.º 9
0
        public void CreateProduct()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var product = new Product();

            product.ProductId  = ID;
            product.CategoryId = ID;
            product.Image      = "/.png";
            product.Descn      = "";
            product.Name       = "";

            try
            {
                using (var context = new PetShopDataContext())
                {
                    context.Product.InsertOnSubmit(product);
                    context.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            Assert.IsTrue(true);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Bind repeater to Cart object in Profile
 /// </summary>
 private void BindCart()
 {
     using (var context = new PetShopDataContext())
     {
         var profile = context.Profile.GetProfile(Page.User.Identity.Name);
         if (!string.IsNullOrEmpty(profile.Username))
         {
             List <Cart> items = profile.ShoppingCart;
             items.ForEach(i => i.Detach());
             if (items.Count > 0)
             {
                 repShoppingCart.DataSource = items;
                 repShoppingCart.DataBind();
                 PrintTotal();
                 plhTotal.Visible = true;
             }
             else
             {
                 repShoppingCart.Visible = false;
                 plhTotal.Visible        = false;
                 lblMsg.Text             = "Your cart is empty.";
             }
         }
     }
 }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        public void CreateProfile()
        {
            Stopwatch watch = Stopwatch.StartNew();

            var profile = new Profile();

            profile.Username         = NAME;
            profile.ApplicationName  = PetShopConstants.APPLICATION_NAME;
            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);
        }
Exemplo n.º 13
0
        // Bind categories
        private void BindCategories()
        {
            using (var context = new PetShopDataContext())
            {
                rePCategories.DataSource = context.Category.OrderBy(c => c.Name).FromCache().ToList();
            }

            rePCategories.DataBind();
        }
Exemplo n.º 14
0
 public static void ClearCart(List <Cart> items)
 {
     using (var context = new PetShopDataContext())
     {
         context.Cart.AttachAll(items);
         context.Cart.DeleteAllOnSubmit(items);
         context.SubmitChanges();
     }
 }
Exemplo n.º 15
0
        public static void Remove(List <Cart> items, string itemId)
        {
            var item = items.FirstOrDefault(i => i.ItemId == itemId);

            using (var context = new PetShopDataContext())
            {
                context.Cart.Delete(item.CartId);
            }
            items.Remove(item);
        }
Exemplo n.º 16
0
        public void DeleteCategory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            using (var context = new PetShopDataContext())
            {
                context.Category.Delete(ID);
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 17
0
        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();
            }
        }
Exemplo n.º 18
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (billingForm.Address == null)
     {
         using (var context = new PetShopDataContext())
         {
             var profile = context.Profile.GetProfile(User.Identity.Name);
             billingForm.Address = new Address(profile);
         }
     }
 }
Exemplo n.º 19
0
        protected Inventory GetInventory(string itemId)
        {
            var inventory = new Inventory();

            using (var context = new PetShopDataContext())
            {
                inventory = context.Inventory.GetByKey(itemId);
                inventory.Detach();
            }
            return(inventory);
        }
Exemplo n.º 20
0
        public void DeleteSupplier()
        {
            Stopwatch watch = Stopwatch.StartNew();

            using (var context = new PetShopDataContext())
            {
                context.Supplier.Delete(_supplierId);
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 21
0
        public static void MoveToWishList(Profile profile, string itemId)
        {
            var item = profile.ShoppingCart.FirstOrDefault(i => i.ItemId == itemId);

            using (var context = new PetShopDataContext())
            {
                context.Cart.Attach(item);
                item.IsShoppingCart = false;
                context.SubmitChanges();
            }
            profile.WishList.Remove(item);
            profile.ShoppingCart.Add(item);
        }
Exemplo n.º 22
0
 /// <summary>
 /// Recalculate the total
 /// </summary>
 private void PrintTotal()
 {
     using (var context = new PetShopDataContext())
     {
         var profile = context.Profile.GetProfile(Page.User.Identity.Name);
         if (!string.IsNullOrEmpty(profile.Username))
         {
             if (profile.ShoppingCart.Count > 0)
             {
                 ltlTotal.Text = CartHelper.GetTotal(profile.ShoppingCart).ToString("c");
             }
         }
     }
 }
Exemplo n.º 23
0
        /// <summary>
        /// Rebind control
        /// </summary>
        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            productsList.CurrentPageIndex = e.NewPageIndex;

            //get category id
            string categoryId = Request.QueryString["categoryId"];

            //bind data(
            using (var context = new PetShopDataContext())
            {
                productsList.DataSource = context.Product.ByCategoryId(categoryId).ToList();
            }
            productsList.DataBind();
        }
Exemplo n.º 24
0
        public void FetchCategory()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Category category = null;

            using (var context = new PetShopDataContext())
            {
                category = context.Category.GetByKey(ID);
                category.Detach();
            }
            Assert.IsTrue(category.CategoryId == ID);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 25
0
        public void DeleteProfile()
        {
            Stopwatch watch = Stopwatch.StartNew();

            using (var context = new PetShopDataContext())
            {
                var profile = context.Profile.GetProfile(NAME);

                if (profile != null)
                {
                    context.Profile.Delete(profile.UniqueID);
                }
            }

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 26
0
        public void FetchProduct()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Product product = null;

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

            Assert.IsTrue(product.ProductId == ID);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 27
0
        public void FetchProfile()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Profile profile = null;

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

            Assert.IsTrue(profile.Username == NAME);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 28
0
        public void FetchItem()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Item item = null;

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

            Assert.IsTrue(item.ItemId == ID);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 29
0
        public void FetchSupplier()
        {
            Stopwatch watch = Stopwatch.StartNew();

            Supplier supplier = null;

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

            Assert.IsTrue(supplier.SuppId == _supplierId);

            Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds);
        }
Exemplo n.º 30
0
        /// <summary>
        /// Rebind control
        /// </summary>
        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            itemsGrid.CurrentPageIndex = e.NewPageIndex;

            //get category id
            string productId = Request.QueryString["productId"];

            using (var context = new PetShopDataContext())
            {
                var options = new DataLoadOptions();
                options.LoadWith <Item>(i => i.Product);
                context.LoadOptions = options;

                itemsGrid.DataSource = context.Item.ByProductId(productId).ToList();
            }
            itemsGrid.DataBind();
        }