public async Task <IActionResult> AddToCart(int id) { Product product = await _inventory.GetbyID(id); if (product != null) { var user = await _userManager.GetUserAsync(User); var userName = user.UserName; ShoppingCart cart = await _cart.GetCart(userName); ShoppingCartItem cartItem = new ShoppingCartItem(cart.ID, product, 1); await _cartItem.SaveCartItem(cartItem); cart.ShoppingCartItems.Add(cartItem); await _cart.SaveCart(cart); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Register(RegisterViewModel register) { if (ModelState.IsValid) { ApplicationUser user = new ApplicationUser() { UserName = register.Email, Email = register.Email, FirstName = register.FirstName, LastName = register.LastName, Birthday = register.Birthday, State = register.State }; ShoppingCart cart = new ShoppingCart() { UserID = user.UserName }; var result = await _userManager.CreateAsync(user, register.Password); if (result.Succeeded) { Claim fullNameClaim = new Claim("FullName", $"{ user.FirstName } {user.LastName }"); Claim birthDateClaim = new Claim(ClaimTypes.DateOfBirth, new DateTime(user.Birthday.Year, user.Birthday.Month, user.Birthday.Day).ToString("u"), ClaimValueTypes.DateTime); Claim emailClaim = new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.Email); Claim stateClaim = new Claim(ClaimTypes.StateOrProvince, user.State.ToString()); List <Claim> allClaims = new List <Claim> { fullNameClaim, birthDateClaim, emailClaim, stateClaim }; await _userManager.AddClaimsAsync(user, allClaims); //assign user to a role if (user.Email == "*****@*****.**" || user.Email == "*****@*****.**" || user.Email == "*****@*****.**" || user.Email == "*****@*****.**") { await _userManager.AddToRoleAsync(user, ApplicationRoles.Admin); } await _signInManager.SignInAsync(user, isPersistent : false); //email confirmation upon registration StringBuilder sb = new StringBuilder(); sb.Append("<p>Thank you for registering with Maletero. "); sb.AppendLine("Here you will find the best quality travel bags for all occasions</p"); await _emailSender.SendEmailAsync(register.Email, "Thanks for registering", sb.ToString()); var ourUser = await _userManager.FindByEmailAsync(register.Email); string id = ourUser.Id; await _shoppingCartManager.SaveCart(cart); return(RedirectToAction("Index", "Home")); } } return(View(register)); }