public IActionResult CreateUser([FromBody] DTOCreateUser body)
        {
            try
            {
                var validator        = new CreateUserValidation();
                var rusultValidation = validator.Validate(body);
                if (!rusultValidation.IsValid)
                {
                    return(BadRequest(rusultValidation.Errors));
                }

                try
                {
                    var newUser = _createUserService.Execute(body);

                    if (newUser != null)
                    {
                        var dto = _mapper.Map <DTOUser>(newUser);
                        return(Created($"{ControllerContext.HttpContext.Request.Path.Value}", dto));
                    }

                    return(BadRequest("Não foi possivel realizar o cadastro tente novamente."));
                }
                catch (ValidationOnServiceException ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
            catch
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ErroMessage));
            }
        }
        public DTOResponse <DTOUser> AdminCreate(DTOCreateUser createUserInfo)
        {
            if (string.IsNullOrWhiteSpace(createUserInfo.Email) || string.IsNullOrWhiteSpace(createUserInfo.Password) || string.IsNullOrWhiteSpace(createUserInfo.ConfirmPassword))
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "Email, Password and Confirm Password are required!"
                });
            }

            var getUserResponse = GetUserByEmail(createUserInfo.Email);

            if (getUserResponse.Code == 200)
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "This user email is already registered!"
                });
            }

            if (createUserInfo.Password != createUserInfo.ConfirmPassword)
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "The password and confirm password dont match!"
                });
            }

            createUserInfo.User.Email       = createUserInfo.Email;
            createUserInfo.User.AccessLevel = UserAccessLevel.Admin;

            var userModel = createUserInfo.User.ToModel();

            var passwordHash = SecurePasswordHasher.Hash(createUserInfo.Password);

            userModel.PasswordHash = passwordHash;

            _userCollection.InsertOne(userModel);

            getUserResponse = GetUserByEmail(createUserInfo.Email);

            var existingUser = getUserResponse.Data;

            LoadUserInfo(existingUser);

            return(new DTOResponse <DTOUser>()
            {
                Code = 200,
                Data = existingUser
            });
        }
Beispiel #3
0
        public User Execute(DTOCreateUser createUser)
        {
            if (string.IsNullOrEmpty(createUser.Name) || string.IsNullOrEmpty(createUser.Email) || string.IsNullOrEmpty(createUser.Password))
            {
                throw new ValidationOnServiceException("Um ou mais campos estão invalidos.");
            }

            if (createUser.Password != createUser.CheckedPassword)
            {
                throw new ValidationOnServiceException("Confirmação de senha invalida.");
            }

            var checkIfEmailExists = _userRepository.GetByEmail(createUser.Email);

            if (checkIfEmailExists != null)
            {
                throw new ValidationOnServiceException("Email já está em uso.");
            }

            var encryptedPassword = _encryption.GenerateCryptgraphy(createUser.Password);


            var permission = _permissionRepository.GetByName(nameof(PermissionsEnum.Common));

            var newUser = new User
            {
                Email        = createUser.Email,
                Name         = createUser.Name,
                Password     = encryptedPassword,
                Phone        = createUser.Phone,
                PermissionId = permission.Id
            };

            var createdUser = _userRepository.Insert(newUser);

            _unitOfWork.Save();

            return(createdUser);
        }
Beispiel #4
0
 public DTOResponse <DTOUser> AdminCreate(DTOCreateUser createUserInfo)
 {
     return(_userDomain.AdminCreate(createUserInfo));
 }
        public DTOResponse <DTOUser> Create(DTOCreateUser createUserInfo)
        {
            if (string.IsNullOrWhiteSpace(createUserInfo.Email) || string.IsNullOrWhiteSpace(createUserInfo.Password) || string.IsNullOrWhiteSpace(createUserInfo.ConfirmPassword))
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "Email, Password and Confirm Password are required!"
                });
            }

            var getUserResponse = GetUserByEmail(createUserInfo.Email);

            if (getUserResponse.Code == 200)
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "This user email is already registered!"
                });
            }

            if (createUserInfo.Password != createUserInfo.ConfirmPassword)
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "The password and confirm password dont match!"
                });
            }

            if (!createUserInfo.HasAcceptedTermsAndPrivacyPolice)
            {
                return(new DTOResponse <DTOUser>()
                {
                    Code = 400,
                    Message = "The user must accept Terms and Privacy police!"
                });
            }

            createUserInfo.User.Email       = createUserInfo.Email;
            createUserInfo.User.AccessLevel = UserAccessLevel.Normal;

            var userModel = createUserInfo.User.ToModel();

            var passwordHash = SecurePasswordHasher.Hash(createUserInfo.Password);

            userModel.PasswordHash = passwordHash;

            _userCollection.InsertOne(userModel);

            getUserResponse = GetUserByEmail(createUserInfo.Email);
            var existingUser = getUserResponse.Data;

            createUserInfo.Pet.UserId = existingUser.Id;

            if (createUserInfo.Pet != null)
            {
                _petDomain.Create(createUserInfo.Pet);
            }

            LoadUserInfo(existingUser);

            return(new DTOResponse <DTOUser>()
            {
                Code = 200,
                Data = existingUser
            });
        }