// // GET: /ShoppingCart/AddToCart/5 public async Task <IActionResult> AddToCart(int id) { // Retrieve the product from the database var addedProduct = _db.Products .Single(product => product.ProductId == id); // Start timer for save process telemetry var startTime = System.DateTime.Now; // Add it to the shopping cart var cart = ShoppingCart.GetCart(_db, Context); cart.AddToCart(addedProduct); await _db.SaveChangesAsync(Context.RequestAborted); // Trace add process var measurements = new Dictionary <string, double>() { { "ElapsedMilliseconds", System.DateTime.Now.Subtract(startTime).TotalMilliseconds } }; _telemetry.TrackEvent("Cart/Server/Add", null, measurements); // Go back to the main store page for more shopping return(RedirectToAction("Index")); }
// // GET: /ShoppingCart/AddToCart/5 public async Task <ActionResult> AddToCart(int id) { // Retrieve the product from the database var addedProduct = db.Products .Single(product => product.ProductId == id); // Start timer for save process telemetry var startTime = DateTime.Now; // Add it to the shopping cart var cart = ShoppingCart.GetCart(db, HttpContext); cart.AddToCart(addedProduct); await db.SaveChangesAsync(CancellationToken.None); // Trace add process var measurements = new Dictionary <string, double>() { { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds }, { "Price", (double)addedProduct.Price } }; var properties = new Dictionary <string, string>() { { "ProductCategory", addedProduct.Category.Name }, { "Product", addedProduct.Title } }; telemetry.TrackEvent("Cart/Server/Add", properties, measurements); // Go back to the main store page for more shopping return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(Product product) { if (ModelState.IsValid) { _db.Entry(product).State = EntityState.Modified; await _db.SaveChangesAsync(HttpContext.RequestAborted); //Invalidate the cache entry as it is modified _cache.Remove(string.Format("product_{0}", product.ProductId)); return(RedirectToAction("Index")); } ViewBag.Categories = new SelectList(_db.Categories, "CategoryId", "Name", product.CategoryId); return(View(product)); }
public async Task <ActionResult> AddressAndPayment(Order order) { var formCollection = Request.Form; try { if (string.Equals(formCollection.GetValues("PromoCode").FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return(View(order)); } else { order.Username = User.Identity.GetUserName(); order.OrderDate = DateTime.Now; //Add the Order db.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(db, HttpContext); cart.CreateOrder(order); // Save all changes await db.SaveChangesAsync(CancellationToken.None); return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }
public async Task <ActionResult> Create(Product product) { if (ModelState.IsValid) { db.Products.Add(product); await db.SaveChangesAsync(CancellationToken.None); //var annoucementHub = GlobalHost.ConnectionManager.GetHubContext<AnnouncementHub>(); //annoucementHub.Clients.All.announcement(new ProductData() { Title = product.Title, Url = Url.Action("Details", "Store", new { id = product.ProductId }) }); MemoryCache.Default.Remove("latestProduct"); return(RedirectToAction("Index")); } return(View(product)); }
public async Task <IActionResult> AddressAndPayment(Order order) { var formCollection = await HttpContext.Request.ReadFormAsync(); try { order.Username = HttpContext.User.GetUserName(); order.OrderDate = DateTime.Now; //Add the Order _db.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(_db, HttpContext); cart.CreateOrder(order, formCollection["PromoCode"].FirstOrDefault()); // Save all changes await _db.SaveChangesAsync(HttpContext.RequestAborted); return(RedirectToAction("Complete", new { id = order.OrderId })); } catch { //Invalid - redisplay with errors return(View(order)); } }
public async Task <int> AddAsync(Raincheck raincheck) { var addedRaincheck = _context.RainChecks.Add(raincheck); await _context.SaveChangesAsync(CancellationToken.None); return(addedRaincheck.Entity.RaincheckId); }
public async Task <IActionResult> AddressAndPayment(Order order) { var formCollection = await HttpContext.Request.ReadFormAsync(); try { if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return(View(order)); } else { order.Username = HttpContext.User.Identity.Name; order.OrderDate = DateTime.Now; //Add the Order _db.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(_db, HttpContext); cart.CreateOrder(order); // Save all changes await _db.SaveChangesAsync(HttpContext.RequestAborted); try { string connectionString = Configuration[ConfigurationPath.Combine("ConnectionStrings", "StorageConnectionString")]; QueueClient queueClient = new QueueClient(connectionString, "orders"); queueClient.CreateIfNotExists(); if (queueClient.Exists()) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(order.OrderId.ToString()); queueClient.SendMessage(System.Convert.ToBase64String(plainTextBytes)); } } catch (Exception) { //return View("Error"); } return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }
public async Task <IActionResult> Create(Product product) { if (ModelState.IsValid) { _db.Products.Add(product); await _db.SaveChangesAsync(HttpContext.RequestAborted); _cache.Remove("announcementProduct"); return(RedirectToAction("Index")); } ViewBag.Categories = new SelectList(_db.Categories, "CategoryId", "Name", product.CategoryId); return(View(product)); }
public async Task <IActionResult> AddressAndPayment(Order order, string finalamount) { TempData["FinalPayment"] = finalamount; string temp = Regex.Replace(finalamount, "[$]", ""); HttpContext.Session.SetString("FinalAmount", temp); var formCollection = await HttpContext.Request.ReadFormAsync(); try { if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return(View(order)); } else { order.Username = HttpContext.User.Identity.Name; order.OrderDate = DateTime.Now; //Add the Order _db.Orders.Add(order); //Process the order var cart = ShoppingCart.GetCart(_db, HttpContext); cart.CreateOrder(order); // Save all changes await _db.SaveChangesAsync(HttpContext.RequestAborted); TempData["OrderID"] = order.OrderId; return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }
public async Task <IActionResult> Create(Product product) { if (ModelState.IsValid) { _db.Products.Add(product); await _db.SaveChangesAsync(Context.RequestAborted); _annoucementHub.Clients.All.announcement(new ProductData() { Title = product.Title, Url = Url.Action("Details", "Store", new { id = product.ProductId }) }); _cache.Remove("announcementProduct"); return(RedirectToAction("Index")); } ViewBag.Categories = new SelectList(_db.Categories, "CategoryId", "Name", product.CategoryId); return(View(product)); }