public async Task <IActionResult> ChangeUserData(string id, [FromBody] UserChangeApiModel user) { try { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage).ToList(); return(BadRequest(errors)); } var _user = await _userManager.FindByIdAsync(id); var confirmPassword = _userManager.PasswordHasher.VerifyHashedPassword(_user, _user.PasswordHash, user.Password); if (confirmPassword == PasswordVerificationResult.Failed) { return(StatusCode(401, "Password doesn`t match")); } await ChangeUserData(_user, user); await ChangeUserRoles(_user, user.RoleNames); return(Ok($"User {user.UserName} was updated")); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
private async Task ChangeUserData(UserModel _user, UserChangeApiModel user) { _user.UserName = user.UserName; _user.FirstName = user.FirstName; _user.LastName = user.LastName; _user.Email = user.Email; _user.PhoneNumber = user.PhoneNumber; _user.WorkDirection = user.WorkDirection; _user.UpdateDate = DateTime.Now; await _userManager.UpdateAsync(_user); }