Ejemplo n.º 1
0
        //
        // GET: /Home/
        public async Task <IActionResult> Index(
            [FromServices] WeLoveVocalEDMContext dbContext,
            [FromServices] IMemoryCache cache)
        {
            // Get most popular albums
            var          cacheKey = "topselling";
            List <Album> albums;

            if (!cache.TryGetValue(cacheKey, out albums))
            {
                albums = await GetTopSellingAlbumsAsync(dbContext, 6);

                if (albums != null && albums.Count > 0)
                {
                    if (_appSettings.CacheDbResults)
                    {
                        // Refresh it every 10 minutes.
                        // Let this be the last item to be removed by cache if cache GC kicks in.
                        cache.Set(
                            cacheKey,
                            albums,
                            new MemoryCacheEntryOptions()
                            .SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
                            .SetPriority(CacheItemPriority.High));
                    }
                }
            }

            return(View(albums));
        }
        //
        // GET: /Checkout/Complete

        public async Task <IActionResult> Complete(
            [FromServices] WeLoveVocalEDMContext dbContext,
            int id)
        {
            var userName = HttpContext.User.Identity.Name;

            // Validate customer owns this order
            bool isValid = await dbContext.Orders.AnyAsync(
                o => o.OrderId == id &&
                o.Username == userName);

            if (isValid)
            {
                _logger.LogInformation("User {userName} completed checkout on order {orderId}.", userName, id);
                return(View(id));
            }
            else
            {
                _logger.LogError(
                    "User {userName} tried to checkout with an order ({orderId}) that doesn't belong to them.",
                    userName,
                    id);
                return(View("Error"));
            }
        }
Ejemplo n.º 3
0
        private Task <List <Album> > GetTopSellingAlbumsAsync(WeLoveVocalEDMContext dbContext, int count)
        {
            // Group the order details by album and return
            // the albums with the highest count

            return(dbContext.Albums
                   .OrderByDescending(a => a.OrderDetails.Count)
                   .Take(count)
                   .ToListAsync());
        }
        public async Task <IActionResult> AddressAndPayment(
            [FromServices] WeLoveVocalEDMContext dbContext,
            [FromForm] Order order,
            CancellationToken requestAborted)
        {
            if (!ModelState.IsValid)
            {
                return(View(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
                    dbContext.Orders.Add(order);

                    //Process the order
                    var cart = ShoppingCart.GetCart(dbContext, HttpContext);
                    await cart.CreateOrder(order);

                    _logger.LogInformation("User {userName} started checkout of {orderId}.", order.Username, order.OrderId);

                    // Save all changes
                    await dbContext.SaveChangesAsync(requestAborted);

                    return(RedirectToAction("Complete", new { id = order.OrderId }));
                }
            }
            catch
            {
                //Invalid - redisplay with errors
                return(View(order));
            }
        }
 public StoreController(WeLoveVocalEDMContext dbContext, IOptions <AppSettings> options)
 {
     DbContext    = dbContext;
     _appSettings = options.Value;
 }
Ejemplo n.º 6
0
 public ShoppingCartController(WeLoveVocalEDMContext dbContext, ILogger <ShoppingCartController> logger)
 {
     DbContext = dbContext;
     _logger   = logger;
 }
 public CartSummaryComponent(WeLoveVocalEDMContext dbContext)
 {
     DbContext = dbContext;
 }
 public GenreMenuComponent(WeLoveVocalEDMContext dbContext)
 {
     DbContext = dbContext;
 }