public async Task UpdateUserAccountAsync(UpdateUserAccountDto userParams, string idUser) { if (userParams == null) { throw new AppException("The data provided is not correct"); } var user = await _userManager.FindByIdAsync(idUser); if (user == null) { throw new AppException("Não existe registo do utilizador!"); } var userEntityUsername = await _userManager.FindByNameAsync(userParams.UserName); if (userEntityUsername != null) { throw new AppException("Username is already assigned to another user!"); } user.UserName = userParams.UserName; user.FirstName = userParams.FirstName; user.LastName = userParams.LastName; var resultUpdateUser = await _userManager.UpdateAsync(user); if (!resultUpdateUser.Succeeded) { throw new AppException("Pedimos desculpa mas ocorreu um problema ao actualizar a sua conta. Tente novamente mais tarde!."); } }
public async Task <IActionResult> UpdateUserProfile(string id, [FromBody] UpdateUserAccountDto updateUserAccountDto) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(modelError => modelError.ErrorMessage).ToList())); } // save await _accountService.UpdateUserAccountAsync(updateUserAccountDto, id); return(Ok(new { message = "Dados do utilizador alterados com sucesso!" })); } catch (AppException ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }