Ejemplo n.º 1
0
        public async Task <ActionResult <CreateUserDtos> > Add(CreateUserDtos createUserDtos)
        {
            try
            {
                var user = _mapper.Map <User>(createUserDtos);

                var exitUser = await _authTokenRepository.LoginUser(user.Email);

                if (exitUser != null)
                {
                    return(BadRequest("Este Usuario ya existe, Por favor introducir otro."));
                }

                user.Password = _passwordRepository.Hash(user.Password);

                var newUser = await _userRepository.Add(user);

                if (newUser == null)
                {
                    return(BadRequest());
                }

                var newUserDto = _mapper.Map <CreateUserDtos>(newUser);

                return(CreatedAtAction(nameof(Add), new { id = newUserDto.Id }, newUserDto));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <UserDto> > Registrar(RegisterUserDto usuarioDto)
        {
            try
            {
                var user = _mapper.Map <User>(usuarioDto);

                var exitUser = await _authRepository.LoginUser(user.Email);

                if (exitUser != null)
                {
                    return(BadRequest("This User already exists, Please enter another."));
                }

                user.Role = (Roles)Enum.Parse(typeof(Roles), "User");

                user.Password = _passwordRepositorio.Hash(user.Password);

                var NewUser = await _authRepository.RegistrarUser(user);

                if (NewUser == null)
                {
                    return(BadRequest("This user could not be registered."));
                }

                var NewUserDtos = _mapper.Map <UserDto>(NewUser);

                await _emailSenderRepository.SendEmailCreateUserAsync(NewUserDtos);

                return(CreatedAtAction(nameof(Registrar), new { id = NewUserDtos.Id }, NewUserDtos));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }