Exemplo n.º 1
0
 /// <summary>
 /// Creates a new product
 /// </summary>
 /// <param name="product">The product</param>
 /// <returns></returns>
 public async Task CreateProduct(Product product)
 {
     _context.Products.Add(product);
     await _context.SaveChangesAsync();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Adds item to cart
 /// </summary>
 /// <param name="ci">Cart item to add</param>
 public async Task AddToCart(CartItem ci)
 {
     _context.CartItems.Add(ci);
     await _context.SaveChangesAsync();
 }
        public async Task <IActionResult> Register(RegisterViewModel rvm)
        {
            if (ModelState.IsValid)
            {
                CheckUserRoleExists();

                ApplicationUser user = new ApplicationUser()
                {
                    UserName  = rvm.Email,
                    Email     = rvm.Email,
                    FirstName = rvm.FirstName,
                    LastName  = rvm.LastName,
                };

                Cart userCart = new Cart()
                {
                    Username = rvm.Email
                };

                _context.Carts.Add(userCart);
                await _context.SaveChangesAsync();


                var result = await _userManager.CreateAsync(user, rvm.Password);

                if (result.Succeeded)
                {
                    Claim fullNameClaim = new Claim("FullName", $"{user.FirstName} {user.LastName}");

                    Claim emailClaim = new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.Email);

                    List <Claim> myClaims = new List <Claim>()
                    {
                        fullNameClaim,
                        emailClaim
                    };

                    if (rvm.Email == "*****@*****.**" || rvm.Email == "*****@*****.**" || rvm.Email == "*****@*****.**" || rvm.FirstName == "Jeff" ||
                        rvm.Email == "*****@*****.**" || rvm.FirstName == "Richard" || rvm.FirstName == "Amanda")
                    {
                        await _userManager.AddToRoleAsync(user, UserRoles.Admin);
                    }

                    await _userManager.AddToRoleAsync(user, UserRoles.Member);

                    await _userManager.AddClaimsAsync(user, myClaims);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    await _email.SendEmailAsync(rvm.Email, "Registration Complete", "<p> Thanks for registering! </p>");

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError("", item.Description);
                    }
                }
            }

            return(View(rvm));
        }