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());
        }
        public void 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));
            Categ      = new GetAllCategories(_context).Do();
            Order      = new GetOrder(_context).Do(currUser, "Pending");
            OrderInfos = new GetOrderInfo(_context).Do(Order.OrderId);
        }
Exemple #4
0
        public async Task <ShoppingCartViewModel> LoadCart(string currUser)
        {
            var cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["anonymousUsr"];
            var usercart = new GetShoppingCart(_context).Do(currUser);

            if (cookieValueFromContext != null && usercart == null)
            {
                usercart            = new GetShoppingCart(_context).Do(cookieValueFromContext);
                usercart.CustomerId = currUser;
                await new UpdateShoppingCart(_context).Do(usercart);
                _httpContextAccessor.HttpContext.Response.Cookies.Delete("anonymousUsr");
                return(usercart);
            }
            return(usercart);
        }
Exemple #5
0
        public async Task <ShoppingCartViewModel> LoadCart()
        {
            var currUser = _userManager.GetUserId(User);
            var cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["anonymousUsr"];

            if (cookieValueFromContext != null)
            {
                var cookieCart = new GetShoppingCart(_context).Do(cookieValueFromContext);
                if (currUser != null && cookieCart != null)
                {
                    var userCart = new GetShoppingCart(_context).Do(currUser);
                    if (cookieCart.CustomerId != currUser && userCart == null)
                    {
                        cookieCart.CustomerId = currUser;
                        await new UpdateShoppingCart(_context).Do(cookieCart);
                        _httpContextAccessor.HttpContext.Response.Cookies.Delete("anonymousUsr");
                        return(cookieCart);
                    }
                    else
                    {
                        return(userCart);
                    }
                }
                else if (cookieCart == null)
                {
                    await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                    {
                        CustomerId = cookieValueFromContext,
                    });
                    return(new GetShoppingCart(_context).Do(cookieValueFromContext));
                }
                else
                {
                    return(cookieCart);
                }
            }
            else if (currUser != null)
            {
                var usercart = new GetShoppingCart(_context).Do(currUser);
                if (usercart == null)
                {
                    await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                    {
                        CustomerId = currUser,
                    });
                    return(new GetShoppingCart(_context).Do(currUser));
                }
                return(usercart);
            }

            else
            {
                var userId = Guid.NewGuid().ToString();
                var option = new CookieOptions();
                option.Expires = DateTime.Now.AddDays(10);
                Response.Cookies.Append("anonymousUsr", userId, option);
                await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                {
                    CustomerId = userId,
                });
                return(new GetShoppingCart(_context).Do(userId));
            }
        }
Exemple #6
0
        public async Task <IActionResult> AnonymousCart(string customerId)
        {
            var currUser = customerId;
            var cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["anonymousUsr"];

            if (cookieValueFromContext != null)
            {
                var cookieCart = new GetShoppingCart(_context).Do(cookieValueFromContext);
                if (currUser != "undefined" && cookieCart != null)
                {
                    var userCart = new GetShoppingCart(_context).Do(currUser);
                    if (cookieCart.CustomerId != currUser && userCart == null)
                    {
                        cookieCart.CustomerId = currUser;
                        await new UpdateShoppingCart(_context).Do(cookieCart);
                        _httpContextAccessor.HttpContext.Response.Cookies.Delete("anonymousUsr");
                        return(Ok(cookieCart));
                    }
                    else
                    {
                        return(Ok(userCart));
                    }
                }
                else if (cookieCart == null)
                {
                    await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                    {
                        CustomerId = cookieValueFromContext,
                    });
                    return(Ok(new GetShoppingCart(_context).Do(cookieValueFromContext)));
                }
                else
                {
                    return(Ok(cookieCart));
                }
            }
            else if (currUser != "undefined")
            {
                var usercart = new GetShoppingCart(_context).Do(currUser);
                if (usercart == null)
                {
                    await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                    {
                        CustomerId = currUser,
                    });
                    return(Ok(new GetShoppingCart(_context).Do(currUser)));
                }
                return(Ok(usercart));
            }

            else
            {
                var userId = Guid.NewGuid().ToString();
                var option = new CookieOptions();
                option.Expires  = DateTime.Now.AddDays(10);
                option.SameSite = SameSiteMode.Strict;
                option.Secure   = false;
                option.HttpOnly = true;
                Response.Cookies.Append("anonymousUsr", userId, option);
                await new CreateShoppingCart(_context).Do(new ShoppingCartViewModel
                {
                    CustomerId = userId,
                });
                return(Ok(new GetShoppingCart(_context).Do(userId)));
            }
        }