DocSas/PropertyService.Domain/ModelView/Users/UserProvider.cs
/// <summary>
/// save user as an asynchronous operation.
/// </summary>
/// <param name="user">The user.</param>
/// <returns>Task&lt;DSCResponse&gt;.</returns>
save user as an asynchronous operation.
save user as an asynchronous operation .
public async Task<DSCResponse> SaveUserAsync(UserVM user)
        {
            var response = new DSCResponse();
            var msg = new Dictionary<string, string>();
            Enum.TryParse(user.ChangeType, out PSChangeType changeType);
            User userEntity = null;
            if (changeType == PSChangeType.PasswordChange || changeType == PSChangeType.SecurityQuestion)
            {
                var verifyPasswordResponse = await _appCommon.VerifyCurrentPasswordAsync(user);
                if (verifyPasswordResponse.IsSuccess)
                {
                    userEntity = verifyPasswordResponse.ResponseData as User;
                }
                else
                {
                    msg = verifyPasswordResponse.ErrorMessage;
                }
             }
            else
            {
                userEntity = await _appCommon.GetUserAsync(user.Id);
            }
                if (userEntity != null)
                {
                    switch (changeType)
                    {
                        case PSChangeType.SecurityQuestion:
                            userEntity.SecurityQuestionId = user.SecurityQuestionId;
                            userEntity.UserSecurityAns = user.UserSecurityAns;
                            break;
                        case PSChangeType.PersonalInfo:
                        if ((string.IsNullOrWhiteSpace(userEntity.UserName) && !string.IsNullOrWhiteSpace(user.UserName))  || (!userEntity.UserName.Equals(user.UserName, StringComparison.OrdinalIgnoreCase)))
                        {
                            var checkUser = await _userRepo.GetSingleAsync(x => x.UserName != null && x.UserName.Equals(user.UserName, StringComparison.OrdinalIgnoreCase));
                            if (checkUser ==null)
                            {
                                userEntity.UserName = user.UserName;
                            }
                            else
                            {
                                msg.Add("UserNameTaken", "Username Taken");
                            }
                        }
                        if (msg.Count < 1  && !userEntity.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase))
                        {
                            var checkUser = await _userRepo.GetSingleAsync(x => x.Email.Equals(user.Email, StringComparison.OrdinalIgnoreCase));
                            if (checkUser == null)
                            {
                                userEntity.Email = user.Email;
                            }
                            else
                            {
                                msg.Add("EmailTaken", "Email Taken");
                            }
                        }
                        userEntity.FirstName = user.FirstName;
                        userEntity.LastName = user.LastName;
                        userEntity.Lang = user.Lang;
                            break;
                        case PSChangeType.PasswordChange:
                            userEntity.UserSalt = SecurityManager.GenerateSalt();
                            userEntity.Password = SecurityManager.HashPassword(user.NewPassword, userEntity.UserSalt);
                            break;
                        case PSChangeType.UserProfileImage:
                           userEntity.PhotoThumbnail = user.PhotoThumbnail;
                           userEntity.PhotoOriginal = user.PhotoOriginal;
                           break;
                        default:
                            break;
                    }
                if (msg.Count < 1)
                {
                    var success = await _userRepo.ModifyAsync(userEntity);
                    if (changeType == PSChangeType.UserProfileImage)
                    {
                        response.ResponseData = user;
                    }
                    else
                    {
                        response.ResponseData = true;
                    }
                    if (!success)
                    {
                        msg.Add("SomethingWrong", "Something went wrong");
                    }
                }
            }
            response.ErrorMessage = msg;
            return response;
        }
public async Task < DSCResponse > SaveUserAsync ( UserVM user ) { var response = new DSCResponse ( ) ; var msg = new Dictionary < string , string > ( ) ; Enum . TryParse ( user . ChangeType , out PSChangeType changeType ) ; User userEntity = null ; if ( changeType == PSChangeType . PasswordChange || changeType == PSChangeType . SecurityQuestion ) { var verifyPasswordResponse = await _appCommon . VerifyCurrentPasswordAsync ( user ) ; if ( verifyPasswordResponse . IsSuccess ) { userEntity = verifyPasswordResponse . ResponseData as User ; } else { msg = verifyPasswordResponse . ErrorMessage ; } } else { userEntity = await _appCommon . GetUserAsync ( user . Id ) ; } if ( userEntity != null ) { switch ( changeType ) { case PSChangeType . SecurityQuestion : userEntity . SecurityQuestionId = user . SecurityQuestionId ; userEntity . UserSecurityAns = user . UserSecurityAns ; break ; case PSChangeType . PersonalInfo : if ( ( string . IsNullOrWhiteSpace ( userEntity . UserName ) && ! string . IsNullOrWhiteSpace ( user . UserName ) ) || ( ! userEntity . UserName . Equals ( user . UserName , StringComparison . OrdinalIgnoreCase ) ) ) { var checkUser = await _userRepo . GetSingleAsync ( x => x . UserName != null && x . UserName . Equals ( user . UserName , StringComparison . OrdinalIgnoreCase ) ) ; if ( checkUser == null ) { userEntity . UserName = user . UserName ; } else { msg . Add ( " " , " " ) ; } } if ( msg . Count < 1 && ! userEntity . Email . Equals ( user . Email , StringComparison . OrdinalIgnoreCase ) ) { var checkUser = await _userRepo . GetSingleAsync ( x => x . Email . Equals ( user . Email , StringComparison . OrdinalIgnoreCase ) ) ; if ( checkUser == null ) { userEntity . Email = user . Email ; } else { msg . Add ( " " , " " ) ; } } userEntity . FirstName = user . FirstName ; userEntity . LastName = user . LastName ; userEntity . Lang = user . Lang ; break ; case PSChangeType . PasswordChange : userEntity . UserSalt = SecurityManager . GenerateSalt ( ) ; userEntity . Password = SecurityManager . HashPassword ( user . NewPassword , userEntity . UserSalt ) ; break ; case PSChangeType . UserProfileImage : userEntity . PhotoThumbnail = user . PhotoThumbnail ; userEntity . PhotoOriginal = user . PhotoOriginal ; break ; default : break ; } if ( msg . Count < 1 ) { var success = await _userRepo . ModifyAsync ( userEntity ) ; if ( changeType == PSChangeType . UserProfileImage ) { response . ResponseData = user ; } else { response . ResponseData = true ; } if ( ! success ) { msg . Add ( " " , " " ) ; } } } response . ErrorMessage = msg ; return response ; }
save user as an asynchronous operation.
save user as an asynchronous operation .
//generate password salt //hash password
None
user
UserVM
None