//
        // 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 }
            };

            telemetry.TrackEvent("Cart/Server/Add", null, measurements);

            // Go back to the main store page for more shopping
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        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 <int> AddAsync(Raincheck raincheck)
        {
            var addedRaincheck = context.RainChecks.Add(raincheck);

            await context.SaveChangesAsync(CancellationToken.None);

            //return addedRaincheck.Entity.RaincheckId;
            return(addedRaincheck.RaincheckId);
        }
Ejemplo n.º 4
0
        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));
        }