Exemplo n.º 1
0
        private async Task <User> CheckUserAsync(string document, string firstName, string lastName, string email, string phone, string address,
                                                 UserType userType)
        {
            var user = await _userHelper.GetUserAsync(email);

            if (user == null)
            {
                user = new User
                {
                    Document    = document,
                    Email       = email,
                    FirstName   = firstName,
                    LastName    = lastName,
                    Address     = address,
                    City        = _context.Cities.FirstOrDefault(),
                    UserType    = userType,
                    UserName    = email,
                    PhoneNumber = phone,
                };

                await _userHelper.AddUserAsync(user, "Eabs123.");

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());

                //Aqui confirmo el usuario creado con token(este por que es el superusuario)
                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            return(user);
        }
Exemplo n.º 2
0
        private async Task <UserEntity> CheckUserAsync(
            string document,
            string firstName,
            string lastName,
            string email,
            string phone,
            UserType userType)
        {
            var user = await _userHelper.GetUserAsync(email);

            if (user == null)
            {
                user = new UserEntity
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Document    = document,
                    UserType    = userType
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            return(user);
        }
Exemplo n.º 3
0
        private async Task <User> CheckUserAsync(
            string document,
            string firstName,
            string lastName,
            string email,
            string phone,
            string address,
            string role)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    Document    = document
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);
            }

            return(user);
        }
Exemplo n.º 4
0
        private async Task <User> AddUser(AddUserViewModel view)
        {
            var user = new User
            {
                Address     = view.Address,
                Document    = view.Document,
                Email       = view.Username,
                FirstName   = view.FirstName,
                LastName    = view.LastName,
                PhoneNumber = view.PhoneNumber,
                UserName    = view.Username
            };

            var result = await _userHelper.AddUserAsync(user, view.Password);

            if (result != IdentityResult.Success)
            {
                return(null);
            }

            var newUser = await _userHelper.GetUserByEmailAsync(view.Username);

            await _userHelper.AddUserToRoleAsync(newUser, "Admin");

            return(newUser);
        }
Exemplo n.º 5
0
        private async Task <User> CheckUserAsync(string firstName, string lastName, string email, string phone, string address, string role, string alias)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName = firstName,
                    LastName  = lastName,
                    Email     = email,
                    UserName  = email,
                    CellPhone = phone,
                    Address   = address,
                    Alias     = alias
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);

                //estas dos linea Auto confirman el email
                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            return(user);
        }
Exemplo n.º 6
0
        private async Task <User> CheckUserAsync(
            string document,
            string firstName,
            string lastName,
            string email,
            string phone,
            string address,
            UserType userType)
        {
            User user = await _userHelper.GetUserAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    Document    = document,
                    City        = _context.Cities.FirstOrDefault(),
                    UserType    = userType
                };

                await _userHelper.AddUserAsync(user, "123456");//clave

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());
            }

            return(user);
        }
Exemplo n.º 7
0
        private async Task <UserEntity> AddUser(AddUserViewModel model)
        {
            var path = string.Empty;

            if (model.ImageFile != null)
            {
                path = await _imageHelper.UploadImageAsync(model.ImageFile, "Users");
            }

            var user = new UserEntity
            {
                Document    = model.Document,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                Address     = model.Address,
                PhoneNumber = model.PhoneNumber,
                PicturePath = path,
                Email       = model.Username,
                UserName    = model.Username,
            };

            var result = await _userHelper.AddUserAsync(user, model.Password);

            if (result != IdentityResult.Success)
            {
                return(null);
            }
            var newUser = await _userHelper.GetUserAsync(model.Username);

            await _userHelper.AddUserToRoleAsync(newUser, "User");

            return(newUser);
        }
Exemplo n.º 8
0
        private async Task <User> AddUserAsync(AddUserViewModel model)
        {
            User user = new User
            {
                Address     = model.Address,
                RFC         = model.RFC,
                Email       = model.Username,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.Username,
                Latitude    = model.Latitude,
                Longitude   = model.Longitude
            };

            IdentityResult result = await _userHelper.AddUserAsync(user, model.Password);

            if (result != IdentityResult.Success)
            {
                return(null);
            }

            User newUser = await _userHelper.GetUserByEmailAsync(model.Username);

            await _userHelper.AddUserToRoleAsync(newUser, "Customer");

            return(newUser);
        }
Exemplo n.º 9
0
        private async Task <User> createUserAsync(AddUserViewModel model)//Debe retornar el usuario
        {
            //2. Creamos el objeto user con los atributos capturados del modelo
            User user = new User
            {
                Address     = model.Address,
                Document    = model.Document,
                Email       = model.Username,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.Username
            };

            //3. Creamos el usuario usando el userHelper y el user que acabamos de crear
            Microsoft.AspNetCore.Identity.IdentityResult result = await _userHelper.AddUserAsync(user, model.Password);

            if (result.Succeeded)//4. Si lo pudo crear lo traemos de nuevo en nuestra variable user
            {
                user = await _userHelper.GetUserByEmailAsync(model.Username);

                //5. Ahora le agregamos el rol a este usuario
                await _userHelper.AddUserToRoleAsync(user, "Owner");

                //6. retornamos el usuario
                return(user);
            }
            //7. si falla retornamos null
            return(null);
        }
Exemplo n.º 10
0
        private async Task <User> CheckUserAsync(string lastName, string firstName, string mail, string phone, string password, string rol)
        {
            var user = await userHelper.GetUserByEmailAsync(mail);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = mail,
                    UserName    = mail,
                    PhoneNumber = phone,
                };

                var result = await userHelper.AddUserAsync(user, password);

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("No se puede crear el usuario en la base de datos");
                }
                await userHelper.AddUserToRoleAsync(user, rol);
            }
            return(user);
        }
Exemplo n.º 11
0
        //implementación de método en el mismo owner pero después de se debe pasar a un helpers.
        private async Task <User> AddUser(AddUserViewModel model)
        {
            var user = new User
            {
                Address     = model.Address,
                Document    = model.Document,
                Email       = model.Username,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.Username,
            };

            var response = await _userHelper.AddUserAsync(user, model.Password);

            if (response != IdentityResult.Success)
            {
                return(null);
            }

            var userInDb = await _userHelper.GetUserByEmailAsync(model.Username);

            await _userHelper.AddUserToRoleAsync(userInDb, "Customer");

            return(userInDb);
        }
Exemplo n.º 12
0
        private async Task <User> CheckUserAsync(string document, string firstName, string lastName, string email, string phone, string address, string role, double latitude, double longitude)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    RFC         = document,
                    Latitude    = latitude,
                    Longitude   = longitude
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);
            }

            var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

            await _userHelper.ConfirmEmailAsync(user, token);

            return(user);
        }
Exemplo n.º 13
0
        private async Task <UserEntity> CheckUserAsync(string document, string firstName, string lastName, string email, string phone,
                                                       string address, UserType userType)
        {
            UserEntity user = await _userHelper.GetUserAsync(email); //busca email

            if (user == null)
            {
                user = new UserEntity //crea el user
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    Document    = document,
                    Team        = _context.Teams.FirstOrDefault(), //coge el primer equipo
                    UserType    = userType
                };

                await _userHelper.AddUserAsync(user, "123456");                  //crea el usuario

                await _userHelper.AddUserToRoleAsync(user, userType.ToString()); //añade el role indicado

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            return(user);
        }
Exemplo n.º 14
0
        private async Task <User> AddUser(AddUserViewModel view)
        {
            var user = new User
            {
                Address     = view.Address,
                Document    = view.Document,
                Email       = view.Username,
                FirstName   = view.FirstName,
                LastName    = view.LastName,
                PhoneNumber = view.PhoneNumber,
                UserName    = view.Username
            };

            var result = await _userHelper.AddUserAsync(user, view.Password);

            if (result != IdentityResult.Success)
            {
                ModelState.AddModelError(string.Empty, result.Errors.FirstOrDefault().Description);
                return(null);
            }

            var newUser = await _userHelper.GetUserByEmailAsync(view.Username);

            await _userHelper.AddUserToRoleAsync(newUser, "Customer");

            return(newUser);
        }
Exemplo n.º 15
0
        public async Task <IActionResult> Create(AddUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    Address     = model.Address,
                    Document    = model.Document,
                    Email       = model.Username,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    UserName    = model.Username
                };

                var response = await _userHelper.AddUserAsync(user, model.Password);

                if (response.Succeeded)
                {
                    var userInDB = await _userHelper.GetUserByEmailAsync(model.Username);

                    await _userHelper.AddUserToRoleAsync(userInDB, "Customer");

                    var owner = new Owner
                    {
                        Agendas = new List <Agenda>(),
                        Pets    = new List <Pet>(),
                        User    = userInDB
                    };

                    _dataContext.Owners.Add(owner);

                    try
                    {
                        await _dataContext.SaveChangesAsync();

                        var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                        var tokenLink = Url.Action("ConfirmEmail", "Account", new
                        {
                            userid = user.Id,
                            token  = myToken
                        }, protocol: HttpContext.Request.Scheme);

                        _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                             $"To allow the user, " +
                                             $"plase click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");

                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(String.Empty, ex.ToString());
                        return(View(model));
                    }
                }
                ModelState.AddModelError(String.Empty, response.Errors.FirstOrDefault().Description);
            }
            return(View(model));
        }
Exemplo n.º 16
0
        private async Task <User> CheckUserAsync(
            string firstName,
            string lastName,
            string email,
            UserType userType)
        {
            User user = await _userHelper.GetUserAsyncByEmail(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName = firstName,
                    LastName  = lastName,
                    IsActive  = true,
                    Email     = email,
                    UserName  = email,
                    UserType  = userType
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());
            }

            return(user);
        }
Exemplo n.º 17
0
        private async Task <User> AddUserAsync(AddUserViewModel model, string userType)
        {
            var user = new User
            {
                Address     = model.Address,
                Document    = model.Document,
                Email       = model.Username,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.Username
            };

            var result = await _userHelper.AddUserAsync(user, model.Password);

            if (result != IdentityResult.Success)
            {
                return(null);
            }

            var newUser = await _userHelper.GetUserByEmailAsync(model.Username);

            await _userHelper.AddUserToRoleAsync(newUser, userType);

            return(newUser);
        }
Exemplo n.º 18
0
        private async Task <Usuario> CheckUserAsync(string Nombres, string Apellidos, string email, string telefono, string rol)
        {
            var usuario = await _userHelper.GetUserByEmailAsync(email);

            if (usuario == null)
            {
                usuario = new Usuario
                {
                    Nombres     = Nombres,
                    Apellidos   = Apellidos,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = telefono
                };

                await _userHelper.AddUserAsync(usuario, "12345678");

                await _userHelper.AddUserToRoleAsync(usuario, rol);
            }
            var token = await _userHelper.GenerateEmailConfirmationTokenAsync(usuario);

            await _userHelper.ConfirmEmailAsync(usuario, token);


            return(usuario);
        }
Exemplo n.º 19
0
        public async Task <IActionResult> Register(RegisterNewUserViewModel model)
        {
            var user = await _userHelper.GetUserByEmailAsync(model.Username);

            if (user == null)
            {
                City city = await _countryRepository.GetCityAsync(model.CityId);

                user = new User
                {
                    FirstName            = model.FirstName,
                    LastName             = model.LastName,
                    Email                = model.Username,
                    PhoneNumber          = model.PhoneNumber,
                    UserName             = model.Username,
                    SocialSecurityNumber = model.SocialSecurityNumber,
                    TaxNumber            = model.TaxNumber,
                    Address              = model.Address,
                    City     = city,
                    isActive = true,
                };


                var result = await _userHelper.AddUserAsync(user, model.Password);


                if (result != IdentityResult.Success)
                {
                    this.ModelState.AddModelError(string.Empty, "The user couldn't be created. Please, contact support!");
                    return(this.View(model));
                }

                var result2 = await _userHelper.AddUserToRoleAsync(user, "Customer");

                var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                // Criar um link que vai levar lá dentro uma acção. Quando o utilizador carregar neste link,
                // vai no controlador Account executar a action "ConfirmEmail"(Ainda será feita)
                // Este ConfirmEmail vai receber um objecto novo que terá um userid e um token.
                var tokenLink = this.Url.Action("ConfirmEmail", "Account", new
                {
                    userid = user.Id,
                    token  = myToken,
                }, protocol: HttpContext.Request.Scheme);

                _mailHelper.SendMail(model.Username, "Email confirmation", $"<h1>Email Confirmation</h1>" +
                                     $"To allow the user, " +
                                     $"plase click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>");

                this.ViewBag.Message = "Go to your email to activate your account";


                return(this.View(model));
            }

            this.ModelState.AddModelError(string.Empty, "The user already exists");


            return(View(model));
        }
Exemplo n.º 20
0
        private async Task <User> CheckUserAsync(
            string document
            , string firstName
            , string lastName
            , string email
            , string Phone
            , string Address
            , string role)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = Phone,
                    Address     = Address,
                    Document    = document
                };
                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);

                var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }
            return(user);
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Roles(TypeUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                string path = model.PicturePath;

                if (model.PictureFile != null)
                {
                    path = await _imageHelper.UploadImageAsync(model.PictureFile, "Users");
                }

                UserEntity user = await _userHelper.GetUserAsync(model.Email);

                string tipo = user.UserType.ToString();
                user.Document    = model.Document;
                user.FirstName   = model.FirstName;
                user.LastName    = model.LastName;
                user.Address     = model.Address;
                user.PhoneNumber = model.PhoneNumber;
                user.PicturePath = path;
                user.Station     = await _context.Stations.FindAsync(model.StationId);

                user.UserType = model.UserType;
                await _userHelper.UpdateUserAsync(user);

                await _userHelper.AddUserToRoleAsync(user, model.UserType.ToString());

                await _userHelper.RemoveUserFromRoleAsync(user, tipo);

                return(RedirectToAction("Index", "Account"));
            }

            model.Stations = _combosHelper.GetComboStations();
            return(View(model));
        }
Exemplo n.º 22
0
        private async Task <User> CheckUserAsync(string firsName, string lastName, string email, string phone, string address, UserType userType)
        {
            User user = await _userHelper.GetUserAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firsName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    UserType    = userType,
                    City        = _context.Cities.FirstOrDefault()
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());

                string token = await _userHelper.GenerateEmailConfirmatioTokenAsync(user);

                await _userHelper.ConfirmEmailAsync(user, token);
            }

            return(user);
        }
Exemplo n.º 23
0
        private async Task <UserEntity> CheckUserAsync(
            string document,
            string firstName,
            string lastName,
            string email,
            string phone,
            string address,
            UserType userType)
        {
            UserEntity user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new UserEntity
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    PhoneNumber = phone,
                    Address     = address,
                    Document    = document,
                    UserType    = userType
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, userType.ToString());
            }
            return(user);
        }
Exemplo n.º 24
0
        private async Task <User> CheckUserAsync(string document,
                                                 string firstName,
                                                 string lastName,
                                                 int age,
                                                 string gender,
                                                 string nationality,
                                                 string email,
                                                 string role)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    Nationality = nationality,
                    Gender      = gender,
                    Age         = age,
                    Document    = document
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);
            }

            return(user);
        }
Exemplo n.º 25
0
        private async Task <User> CreateUserAsync(AddUserViewModel model)
        {
            var user = new User
            {
                Document       = model.Document,
                SiteExpedition = model.SiteExpedition,
                FirstName      = model.FirstName,
                LastName       = model.LastName,
                Email          = model.Username,
                JobTitle       = model.JobTitle,
                SiteBirth      = model.SiteBirth,
                Address        = model.Address,
                Rh             = model.Rh,
                License        = model.License,
                Movil          = model.Movil,
                Arl            = model.Arl
            };
            var result = await _userHelper.AddUserAsync(user, model.Password);

            if (result.Succeeded)
            {
                user = await _userHelper.GetUserByEmailAsync(model.Username);

                await _userHelper.AddUserToRoleAsync(user, "Employe");

                return(user);
            }
            return(null);
        }
Exemplo n.º 26
0
        private async Task CheckUserAsync(string firstName, string lastName, string email, string phone, string pwd, string role)
        {
            var userLogin = await _userHelper.GetUserByEmailAsync(email);

            if (userLogin == null)
            {
                userLogin = new UserLogin {
                    Email = email, PhoneNumber = phone, UserName = email
                };
                var user = new Usuario   {
                    FirstName = firstName, LastName = lastName, UserLogin = userLogin
                };

                await _userHelper.AddUserAsync(userLogin, pwd);

                await _userHelper.AddUserToRoleAsync(userLogin, role);

                _context.Usuarios.Add(user);
                await _context.SaveChangesAsync();

                var defaultToken = await _userHelper.GenerateEmailConfirmationTokenAsync(userLogin);

                await _userHelper.ConfirmEmailAsync(userLogin, defaultToken);
            }
        }
Exemplo n.º 27
0
        private async Task <User> CreateUserAsync(AddUserViewModel model)
        {
            var user = new User
            {
                Address     = model.Address,
                Document    = model.Document,
                Email       = model.Username,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.Username
            };

            var result = await _userHelper.AddUserAsync(user, model.Password);

            if (result.Succeeded)
            {
                user = await _userHelper.GetUserByEmailAsync(model.Username);

                await _userHelper.AddUserToRoleAsync(user, "Owner");

                return(user);
            }

            return(null);
        }
Exemplo n.º 28
0
        private async Task <User> CheckUserAsync(string firstName, string lastName, string document, string email, string role)
        {
            var user = await _userHelper.GetUserByEmailAsync(email);

            if (user == null)
            {
                user = new User
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Email       = email,
                    UserName    = email,
                    Document    = document,
                    PicturePath = $"~/images/Users/alex.png"
                };

                await _userHelper.AddUserAsync(user, "123456");

                await _userHelper.AddUserToRoleAsync(user, role);
            }

            /*var token = await _userHelper.GenerateEmailConfirmationTokenAsync(user);
             * await _userHelper.ConfirmEmailAsync(user, token);*/

            return(user);
        }
Exemplo n.º 29
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();

            await _userHelper.CheckRolesAsync("Admin");

            await _userHelper.CheckRolesAsync("ReSeller");

            await _userHelper.CheckRolesAsync("Client");

            var user = await _userHelper.GetUserByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User
                {
                    FirstNames     = "Ricardo Filipe",
                    LastNames      = "Pinto Lourenço",
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    PhoneNumber    = "912345678",
                    Address        = "Rua da Luz 23, 2dto.",
                    EmailConfirmed = true
                };

                var result = await _userHelper.AddUserAsync(user, "ABab12!?");

                await _userHelper.AddUserToRoleAsync(user, "Admin");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create the user in seeder");
                }
            }

            var category = await _context.Categories.Where(c => c.Name == "Console").FirstOrDefaultAsync();

            if (category == null)
            {
                var entity = new Category
                {
                    Name = "Console"
                };

                await _context.Categories.AddAsync(entity);

                await _context.SaveChangesAsync();

                category = await _context.Categories.Where(c => c.Name == "Console").FirstOrDefaultAsync();
            }

            if (!_context.Products.Any())
            {
                AddProduct("Consola XPTO", 300, 30, category);
                AddProduct("Consola ABC", 300, 30, category);
                AddProduct("Consola DEFG", 300, 30, category);
                await _context.SaveChangesAsync();
            }
        }
Exemplo n.º 30
0
        public async Task <IActionResult> RegisterUserAsync([FromBody] UserRequest userRequest)
        {
            //TODO: hacer la validacion por email y mover a un controlador API Account
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Response <object>
                {
                    IsSuccess = false,
                    Message = "Bad request"
                }));
            }

            var user = await _userHelper.GetUserByEmailAsync(userRequest.Email);

            if (user != null)
            {
                return(BadRequest(new Response <object>
                {
                    IsSuccess = false,
                    Message = "Este Correo ya esta Registrado"
                }));
            }

            Random rnd = new Random();
            // Obtiene un número natural (incluye el 0) aleatorio entre 0 e int.MaxValue
            int alt = rnd.Next(1000);

            User u = new User
            {
                FirstName = userRequest.FirstName,
                LastName  = userRequest.LastName,
                Email     = userRequest.Email,
                UserName  = userRequest.Email,
                CellPhone = userRequest.CellPhone,
                Address   = userRequest.Address,
                Alias     = _userHelper.GenerateAlias(userRequest.FirstName, userRequest.LastName)
            };

            var result = await _userHelper.AddUserAsync(u, userRequest.Password);

            if (result != IdentityResult.Success)
            {
                return(BadRequest(new Response <object>
                {
                    IsSuccess = false,
                    Message = result.Errors.FirstOrDefault().Description
                }));
            }

            var userNew = await _userHelper.GetUserByEmailAsync(userRequest.Email);

            await _userHelper.AddUserToRoleAsync(userNew, "Customer");

            return(Ok(new Response <object>
            {
                IsSuccess = true,
                Message = "Registro Exitoso!"
            }));
        }