public void ClearShoppingCart() { Stopwatch watch = Stopwatch.StartNew(); //Add new Item to the cart. Profile profile = null; using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(NAME); context.Profile.Detach(profile); } CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); //Clear the cart. using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(NAME); context.Profile.Detach(profile); } CartHelper.ClearCart(profile.ShoppingCart); using (var context = new PetshopDataContext()) { Assert.IsTrue(context.Profile.GetProfile(NAME).ShoppingCart.Count == 0); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
/// <summary> /// Handler for Delete/Move buttons /// </summary> protected void CartItem_Command(object sender, CommandEventArgs e) { var profile = new Profile(); using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(Page.User.Identity.Name); context.Profile.Detach(profile); } 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(); }
public void UpdateItemQuantityShoppingCart() { Stopwatch watch = Stopwatch.StartNew(); //Add new Item to the cart. Profile profile = null; using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(NAME); context.Profile.Detach(profile); } CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(NAME); context.Profile.Detach(profile); } Assert.IsTrue(profile.ShoppingCart.Count == 1 && profile.ShoppingCart[0].Quantity == 2); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
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) { context.Profile.Detach(profile); } } 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); } } }
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); }
/// <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; context.Cart.DetachAll(items); 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."; } } } }
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); }
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 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); }
/// <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> "; }
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); }
/// <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(); }
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 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 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); }
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); }
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); }
/// <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); }
public static void ClearCart(List <Cart> items) { using (var context = new PetshopDataContext()) { context.Cart.AttachAll(items); context.Cart.DeleteAllOnSubmit(items); context.SubmitChanges(); } }
// Bind categories private void BindCategories() { using (var context = new PetshopDataContext()) { rePCategories.DataSource = context.Category.OrderBy(c => c.Name).FromCache().ToList(); } rePCategories.DataBind(); }
protected Inventory GetInventory(string itemId) { Inventory inventory; using (var context = new PetshopDataContext()) { inventory = context.Inventory.GetByKey(itemId); context.Inventory.Detach(inventory); } return(inventory); }
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); } } }
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(); } }
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 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); }
/// <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"); } } } }
public void FetchCategory() { Stopwatch watch = Stopwatch.StartNew(); Category category = null; using (var context = new PetshopDataContext()) { category = context.Category.GetByKey(ID); context.Category.Detach(category); } Assert.IsTrue(category.CategoryId == ID); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
/// <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.ByCategory(categoryId).ToList(); } productsList.DataBind(); }
public void FetchProfile() { Stopwatch watch = Stopwatch.StartNew(); Profile profile = null; using (var context = new PetshopDataContext()) { profile = context.Profile.GetProfile(NAME); context.Profile.Detach(profile); } Assert.IsTrue(profile.Username == NAME); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public void FetchItem() { Stopwatch watch = Stopwatch.StartNew(); Item item = null; using (var context = new PetshopDataContext()) { item = context.Item.GetByKey(ID); context.Item.Detach(item); } Assert.IsTrue(item.ItemId == ID); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }