public async Task <IActionResult> Register([FromBody] User userInfo)
        {
            if (userInfo == null || (userInfo != null && await accountsRepository.UserExistAsync(userInfo.Username)))
            {
                return(BadRequest());
            }

            User result = null;

            userInfo.Password = HashGenerator.GenerateHash(userInfo.Password);
            await accountsRepository.AddUserAsync(userInfo);

            try
            {
                result = await accountsRepository.GetUserAsync(userInfo.Username);
            }
            catch (Exception e)
            {
                var x = e.Message;
            }

            if (result == null)
            {
                return(BadRequest());
            }
            return(Ok(result));
        }
示例#2
0
        public async Task <ResultData <RetrospectorUser> > CreateUserAsync(string email, string roleName)
        {
            if (string.IsNullOrEmpty(email))
            {
                return(new ResultData <RetrospectorUser>(EmailNullMessage, false));
            }

            if (string.IsNullOrEmpty(roleName))
            {
                return(new ResultData <RetrospectorUser>(RoleNameEmptyMessage, false));
            }

            RetrospectorUser user = await _accountsRepository.GetUserByEmailAsync(email);

            if (user != null)
            {
                string userFoundMessage = string.Format(UserIsFoundMessage, email);
                return(new ResultData <RetrospectorUser>(userFoundMessage, true, user));
            }

            user = new RetrospectorUser
            {
                Email      = email,
                UserName   = email,
                Notes      = new List <Note>(),
                TeamUsers  = new List <TeamUser>(),
                OwnedTeams = new List <Team>()
            };

            IdentityResult identityResult = await _accountsRepository.AddUserAsync(user);

            if (!identityResult.Succeeded)
            {
                return(new ResultData <RetrospectorUser>(UserCreateFailMessage, false));
            }

            Result roleResult = await AssginToRoleUserAsync(user, roleName);

            if (!roleResult.Success)
            {
                string roleMessage = string.Format(AssignToUserRoleFailMessage, email);

                return(new ResultData <RetrospectorUser>(roleMessage, false));
            }

            string message = string.Format(UserCreateSuccessMessage, user.Email);

            return(new ResultData <RetrospectorUser>(message, true, user));
        }