public IActionResult Checkout() { var usercart = HttpContext.Session.GetString("Cart"); if (usercart == null) { return(RedirectToAction("Cart")); } List <Order> cartitems = JsonConvert.DeserializeObject <List <Order> >(usercart); foreach (Order currentOrder in cartitems) { //This is hard-coded. Replace with User from session // currentOrder.UserId = 1; dbContext.Orders.Add(new Order() { Quantity = currentOrder.Quantity, ProductId = currentOrder.ProductId, UserId = currentOrder.UserId }); } dbContext.SaveChanges(); return(RedirectToAction("Summary")); }
// Register as Admin public IActionResult Register(User newUser) { if (ModelState.IsValid) { bool isEmailTaken = dbContext.Users.Any(user => newUser.Email == user.Email); if (isEmailTaken) { ModelState.AddModelError("Email", "Email Taken"); } } if (ModelState.IsValid == false) { return(View("Admin")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); dbContext.Users.Add(newUser); dbContext.SaveChanges(); HttpContext.Session.SetInt32("UserId", newUser.UserId); return(RedirectToAction("Dashboard")); }