Esempio n. 1
0
 public CartController(UserManager <ApplicationUser> manager, UnicornStoreContext context, ApplicationDbContext identityContext, CategoryCache cache)
 {
     userManager   = manager;
     db            = context;
     identityDb    = identityContext;
     categoryCache = cache;
 }
Esempio n. 2
0
        //
        // GET: /Home/
        public async Task <IActionResult> Index(
            [FromServices] UnicornStoreContext dbContext,
            [FromServices] IMemoryCache cache)
        {
            // Get most popular blessings
            var             cacheKey = "topselling";
            List <Blessing> blessings;

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

                if (blessings != null && blessings.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,
                            blessings,
                            new MemoryCacheEntryOptions()
                            .SetAbsoluteExpiration(TimeSpan.FromMinutes(10))
                            .SetPriority(CacheItemPriority.High));
                    }
                }
            }

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

        public async Task <IActionResult> Complete(
            [FromServices] UnicornStoreContext 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"));
            }
        }
Esempio n. 4
0
        private Task <List <Blessing> > GetTopSellingBlessingsAsync(UnicornStoreContext dbContext, int count)
        {
            // Group the order details by blessing and return
            // the blessings with the highest count

            return(dbContext.Blessings
                   .OrderByDescending(a => a.OrderDetails.Count)
                   .Take(count)
                   .ToListAsync());
        }
Esempio n. 5
0
        private Task <List <Album> > GetTopSellingAlbumsAsync(UnicornStoreContext 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] UnicornStoreContext 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
                    // TODO: investigate why intermediary SaveChangesAsync() is necessary.
                    await dbContext.Orders.AddAsync(order);

                    await dbContext.SaveChangesAsync();

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

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

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

                    return(RedirectToAction("Complete", new { id = order.OrderId }));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Checkout failed");
                //Invalid - redisplay with errors
                return(View(order));
            }
        }
Esempio n. 7
0
        public static async Task Initialize(
            UnicornStoreContext context,
            IServiceProvider serviceProvider,
            IConfiguration configuration,
            bool createUsers = true)
        {
            if (await context.Database.EnsureCreatedAsync())
            {
                await InsertTestData(serviceProvider);

                if (createUsers)
                {
                    await CreateAdminUser(serviceProvider, configuration);
                }
            }
        }
Esempio n. 8
0
        public void GetPendingOrders()
        {
            var builder = new DbContextOptionsBuilder <UnicornStoreContext>();

            builder.UseInMemoryStore(persist: true);
            var options = builder.Options;

            using (var context = new UnicornStoreContext(options))
            {
                var orders = new List <Order>
                {
                    new Order {
                        State = OrderState.CheckingOut, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Placed, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Filling, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.ReadyToShip, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Shipped, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Delivered, ShippingDetails = new OrderShippingDetails()
                    },
                    new Order {
                        State = OrderState.Cancelled, ShippingDetails = new OrderShippingDetails()
                    },
                };

                context.AddRange(orders);
                context.AddRange(orders.Select(o => o.ShippingDetails));
                context.SaveChanges();
            }

            using (var context = new UnicornStoreContext(options))
            {
                var controller = new ShippingController(context);
                var orders     = controller.PendingOrders();
                Assert.Equal(1, orders.Count());
            }
        }
Esempio n. 9
0
 public CartController(UnicornStoreContext context, ApplicationDbContext identityContext, CategoryCache cache)
 {
     db            = context;
     identityDb    = identityContext;
     categoryCache = cache;
 }
 public OrdersController(UnicornStoreContext context)
 {
     db = context;
 }
Esempio n. 11
0
 public HomeController(UnicornStoreContext context, CategoryCache cache)
 {
     db            = context;
     categoryCache = cache;
 }
Esempio n. 12
0
 public ShoppingCartController(UnicornStoreContext dbContext, ILogger <ShoppingCartController> logger)
 {
     DbContext = dbContext;
     _logger   = logger;
 }
 public CartSummaryComponent(UnicornStoreContext dbContext)
 {
     DbContext = dbContext;
 }
Esempio n. 14
0
 public RecallsController(UnicornStoreContext context)
 {
     db = context;
 }
Esempio n. 15
0
 public ShippingController(UnicornStoreContext context)
 {
     db = context;
 }
Esempio n. 16
0
 public OrdersController(UserManager <ApplicationUser> manager, UnicornStoreContext context)
 {
     userManager = manager;
     db          = context;
 }
 public GenreMenuComponent(UnicornStoreContext dbContext)
 {
     DbContext = dbContext;
 }
Esempio n. 18
0
 public StoreManagerController(UnicornStoreContext dbContext, IOptions <AppSettings> options)
 {
     DbContext    = dbContext;
     _appSettings = options.Value;
 }
 public StoreController(UnicornStoreContext dbContext, IOptionsSnapshot <AppSettings> options)
 {
     DbContext    = dbContext;
     _appSettings = options.Value;
 }
 public ManageProductsController(UnicornStoreContext context, CategoryCache cache)
 {
     db            = context;
     categoryCache = cache;
 }