Пример #1
0
        public async Task <IActionResult> Index()
        {
            var locationRepo = (Repository.ILocation) this._services.GetService(typeof(Repository.ILocation));
            var userRepo     = (Repository.IUser) this._services.GetService(typeof(Repository.IUser));
            var orderRepo    = (Repository.IOrder) this._services.GetService(typeof(Repository.IOrder));
            var productRepo  = (Repository.IProduct) this._services.GetService(typeof(Repository.IProduct));

            var userId = Guid.Parse(HttpContext.User.FindFirst(claim => claim.Type == Auth.Claim.UserId).Value);

            var user = await userRepo.GetUserById(userId);

            var location = await userRepo.GetDefaultLocation(user);

            var currentOrder = await userRepo.GetOpenOrder(user, location);

            var orderLines = orderRepo.GetOrderLines(userId, currentOrder.OrderId);

            var model = new Model.Input.Cart();

            var allStock = locationRepo.GetStock(currentOrder.OrderId);

            foreach (var item in orderLines)
            {
                var stock = allStock
                            .Where(i => i.Item1 == item.Product.ProductId)
                            .Select(s => s.Item2)
                            .FirstOrDefault();

                var cartItem = new Model.Input.CartItem();
                cartItem.Id        = item.Product.ProductId;
                cartItem.Name      = item.Product.Name;
                cartItem.UnitPrice = item.Product.Price;
                cartItem.Quantity  = item.Quantity;
                cartItem.ImageName = item.Product.ImageName;
                cartItem.Stock     = stock;
                model.Items.Add(cartItem);
            }

            return(View("Cart", model));
        }
Пример #2
0
 public IActionResult RedirectCartUpdated(Model.Input.Cart model)
 {
     return(RedirectToAction("Index", "Cart"));
 }
Пример #3
0
        public async Task <IActionResult> Update(Model.Input.Cart model)
        {
            var orderRepo = (Repository.IOrder) this._services.GetService(typeof(Repository.IOrder));
            var userRepo  = (Repository.IUser) this._services.GetService(typeof(Repository.IUser));

            var userId = Guid.Parse(HttpContext.User.FindFirst(claim => claim.Type == Auth.Claim.UserId).Value);
            var user   = await userRepo.GetUserById(userId);

            var location = await userRepo.GetDefaultLocation(user);

            var order = await userRepo.GetOpenOrder(user, location);

            if (ModelState.IsValid)
            {
                var removeIndex = model.RemoveIndex();
                if (removeIndex != null)
                {
                    if ((int)removeIndex < model.Items.Count)
                    {
                        var removed = await orderRepo.DeleteLineItem(userId, order, model.Items[(int)removeIndex].Id);

                        if (!removed)
                        {
                            this.SetFlashError("There was an error removing an item from your order. Please try again.");
                            return(RedirectToAction("Index", "Cart"));
                        }
                    }
                }
                else
                {
                    foreach (var i in model.Items)
                    {
                        var updateStatus = await orderRepo.SetLineItemQuantity(userId, order, i.Id, i.Quantity);

                        switch (updateStatus)
                        {
                        case Repository.SetLineItemQuantityResult.ExceedsStock:
                        {
                            this._logger.LogWarning($"An attempt was made to update the cart with more items than are available. User id: '{userId}'.");
                            this.SetFlashError("Unable to update the quantities in your order: The amount requested exceeds the amount available in stock.");
                            return(RedirectToAction("Index", "Cart"));
                        }

                        case Repository.SetLineItemQuantityResult.ProductMissing:
                        {
                            this._logger.LogWarning($"An attempt was made to update the cart with a non-existing item. User id: '{userId}'.");
                            this.SetFlashError("There was an error updating the item quantities in your order. Please try again.");
                            return(RedirectToAction("Index", "Cart"));
                        }
                        }
                    }
                }
            }
            else
            {
                this.SetFlashError("There was an error updating the item quantities in your order. Please try again.");
                return(RedirectToAction("Index", "Cart"));
            }

            this.SetFlashOk("Items quantities updated successfully.");
            return(RedirectToAction("Index", "Cart"));
        }