public async Task <IActionResult> Login(LoginRegisterViewModel vm) { ModelState.Remove("PasswordConfirm"); if (ModelState.IsValid) { var biendekiemtracotrongcsdlhaykhong = await _signInManager.PasswordSignInAsync(vm.UserName, vm.Password, vm.RememberMe, lockoutOnFailure : false); if (biendekiemtracotrongcsdlhaykhong.Succeeded) { if (_cookieServices.ReadCookie(HttpContext, "CART_INFORMATION") != _userManager.FindByNameAsync(vm.UserName).Result.Id || _cookieServices.ReadCookie(HttpContext, "CART_INFORMATION") == null) { if (vm.RememberMe) { _cookieServices.AddCookie(HttpContext, "CART_INFORMATION", _userManager.FindByNameAsync(vm.UserName).Result.Id, isPersistent: true); } else { _cookieServices.AddCookie(HttpContext, "CART_INFORMATION", _userManager.FindByNameAsync(vm.UserName).Result.Id); } } return(RedirectToAction("Index", "Home")); } } return(StatusCode(69)); }
internal static bool AddToCart(this DoAnTMDT_Entities _context, HttpContext httpContext, int itemID, string size, byte quantity = 1) { Product bienkiemtraxemcosanphamtrongdb = _context.ProductTable.Find(itemID); if (bienkiemtraxemcosanphamtrongdb != null) { string cookieKey = _cookieServices.ReadCookie(httpContext, "CART_INFORMATION"); if (string.IsNullOrEmpty(cookieKey)) { cookieKey = httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); _cookieServices.AddCookie(httpContext, "CART_INFORMATION", cookieKey); } var bienkiemtraxemcodonhangchuathanhtoancocungmadonhang = _context.CartDetailTable.Include(x => x.Cart).Where(x => x.Cart.UserID == cookieKey && x.Cart.IsDisplay); if (bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Count() > 0) { var bienkiemtraxemsanphamdodacotronggiohangchua = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.Where(x => x.ProductID == itemID && x.Size == size).FirstOrDefault(); if (bienkiemtraxemsanphamdodacotronggiohangchua != null) { byte calculatedQuantity = 0; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { bienkiemtraxemsanphamdodacotronggiohangchua.Quantity += calculatedQuantity; } else { bienkiemtraxemsanphamdodacotronggiohangchua.Quantity += quantity; } } else { byte calculatedQuantity = 0; bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart.OderDate = DateTime.Now; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { _context.CartDetailTable.Add(new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = calculatedQuantity, Cart = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart, Size = size, CartID = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().CartID }); } else { _context.CartDetailTable.Add(new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = quantity, Cart = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().Cart, Size = size, CartID = bienkiemtraxemcodonhangchuathanhtoancocungmadonhang.First().CartID }); } } _context.SaveChanges(); return(true); } else { string guidKey = Guid.NewGuid().ToString(); Cart cartDetail = new Cart { CartID = guidKey, UserID = cookieKey, OderDate = DateTime.Now }; CartDetail detail; byte calculatedQuantity = 0; if (size == "small") { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity; bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.SmallSizeQuantity -= quantity; } } else if (size == "large") { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity; bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.LargeSizeQuantity -= quantity; } } else { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity - quantity < 0) { if (bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity > 0) { calculatedQuantity = bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity; bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity = 0; } } else { bienkiemtraxemcosanphamtrongdb.MediumSizeQuantity -= quantity; } } if (calculatedQuantity > 0) { detail = new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = calculatedQuantity, Size = size, CartID = guidKey, Cart = cartDetail }; } else { detail = new CartDetail { Product = bienkiemtraxemcosanphamtrongdb, Quantity = quantity, Size = size, CartID = guidKey, Cart = cartDetail }; } _context.CartDetailTable.Add(detail); _context.SaveChanges(); return(true); } } return(false); }