public async Task <IActionResult> OnGet()
        {
            var currUser = _userManager.GetUserId(User);

            ShoppingCart = new GetShoppingCart(_context).Do(currUser);
            CartItems    = new GetCartItems(_context).Do(ShoppingCart.CartId);
            Products     = new GetAllProducts(_context).Do()
                           .Where(prod => CartItems.Select(cartItem => cartItem.ProductRefId)
                                  .Contains(prod.ProductId));
            Order = new GetOrder(_context).Do(currUser, "Pending");
            foreach (var cartitem in CartItems.ToList())
            {
                await new CreateProductInOrder(_context).Do(new ProductInOrdersViewModel
                {
                    OrderRefId   = Order.OrderId,
                    ProductRefId = cartitem.ProductRefId,
                    UsedQuantity = cartitem.Quantity,
                });
                await new UpdateProduct(_context, _fileManager).UpdateStockAfterOrder(cartitem.ProductRefId, cartitem.Quantity);
            }
            Order.Status       = "Ordered";
            Order.TotalOrdered = ShoppingCart.TotalInCart;
            await new UpdateOrder(_context).Do(Order);
            ShoppingCart.Status = "Closed";
            await new UpdateShoppingCart(_context).Do(ShoppingCart);
            return(RedirectToPage("/Account/Manage/Orders", new { Area = "Identity" }));
        }
Exemple #2
0
        public async Task <IActionResult> PlaceOrder([FromForm] OrderViewModel vm)
        {
            if (ModelState.IsValid)
            {
                await new UpdateOrder(_context).Do(vm);
                var ShoppingCart = new GetShoppingCart(_context).Do(vm.CustomerId);
                var CartItems    = new GetCartItems(_context).Do(ShoppingCart.CartId);
                var Products     = new GetAllProducts(_context).Do(ShoppingCart.CartId, 0)
                                   .Where(prod => CartItems.Select(cartItem => cartItem.ProductRefId)
                                          .Contains(prod.ProductId));

                foreach (var cartitem in CartItems.ToList())
                {
                    await new CreateProductInOrder(_context).Do(new ProductInOrdersViewModel
                    {
                        OrderRefId   = vm.OrderId,
                        ProductRefId = cartitem.ProductRefId,
                        UsedQuantity = cartitem.Quantity,
                    });
                    await new UpdateProduct(_context, _fileManager).UpdateStockAfterOrder(cartitem.ProductRefId, cartitem.Quantity);
                }
                ShoppingCart.Status = "Closed";
                await new UpdateShoppingCart(_context).Do(ShoppingCart);
                return(Ok());
            }
            return(BadRequest());
        }