public ClientController([FromServices] IEditClient editClient, [FromServices] IRemoveClient removeClient, [FromServices] IGetClient getClient) { _editClient = editClient; _removeClient = removeClient; _getClient = getClient; }
/// <summary> /// Compare two Client and set change in second Client /// if has found changed data /// </summary> /// <param name="updUser">client with new data</param> /// <param name="user">client with old data which will be changed</param> /// <returns></returns> private bool CompareUsers(IEditClient updUser, ref User user) { bool result = false; if (user.UserName != updUser.Name) { user.UserName = updUser.Name; result = true; } if (user.Email != updUser.Email) { user.Email = updUser.Email; result = true; } return(result); }
/// <summary> /// Update User with Client Role /// </summary> /// <param name="updUser">client role object with new data</param> /// <returns></returns> public async Task <(bool isSuccess, IEnumerable <string> errors)> ClientUpdateAsync(IEditClient updUser) { var result = (isSuccess : false, errors : new List <string>()); if (updUser == null) { result.errors.Add("User is empty!"); return(result); } User user = await _userManager.FindByIdAsync(updUser?.Id); if (user == null) { result.errors.Add("User is not exists!"); return(result); } bool isChanged = CompareUsers(updUser, ref user); if (isChanged) { IdentityResult updResult = await _userManager.UpdateAsync(user); if (!updResult.Succeeded) { foreach (var error in updResult.Errors) { result.errors.Add(error.Description); } return(result); } } if ( !string.IsNullOrEmpty(updUser.OldPassword) && !string.IsNullOrEmpty(updUser.NewPassword) && !string.IsNullOrEmpty(updUser.ConfirmNewPassword) ) { bool checkPassResult = await _userManager.CheckPasswordAsync(user, updUser.OldPassword); if (checkPassResult) { IdentityResult changePassResult = await _userManager.ChangePasswordAsync(user, updUser.OldPassword, updUser.NewPassword); if (!changePassResult.Succeeded) { foreach (var error in changePassResult.Errors) { result.errors.Add(error.Description); } return(result); } } else { result.errors.Add("Password was not changed!"); return(result); } } result.isSuccess = true; return(result); }