예제 #1
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.FindByEmailAsync(Input.Username);

            if (user == null)
            {
                ModelState.AddModelError("Input.Username", "Incorrect username or password.");
                return(Page());
            }

            var result = await _signInManager.PasswordSignInAsync(user, Input.Password, false, false);

            IdentityUser usr = new IdentityUser();

            if (result.Succeeded)
            {
                // Add user id to current session cart
                // Remove session id from cart at the same time
                var sessionId = GetSessionId();
                var cart      = _ctx.Carts.Include(c => c.Products).FirstOrDefault(x => x.SessionId == sessionId);

                if (cart != null)
                {
                    var existingCart = _ctx.Carts.Include(c => c.Products).FirstOrDefault(x => x.UserId == user.Id);
                    // if user already has a cart, add products to the existing cart
                    if (existingCart != null)
                    {
                        foreach (var product in cart.Products.ToList())
                        {
                            // the product should not already exist in the cart
                            if (!existingCart.Products.Any(cp => cp.ProductId == product.ProductId))
                            {
                                await _addToCart.Do(new AddToCart.Request()
                                {
                                    ProductId = product.ProductId,
                                    Qty       = product.Qty,
                                    UserMark  = (user.Id, string.Empty)
                                });