public async Task <IActionResult> OnPostAsync() { IdentityUser user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } Models.EntityModels.UserProfile userProfile = await _userProfileService.GetByUserId(user.Id); if (userProfile != null) { userProfile.EnableEmailNotifications = Input.EnableEmailNotifications; userProfile.DaysBetweenEmailNotifications = Input.EmailFrequency switch { "Daily" => 1, "Weekly" => 7, "Monthly" => 30, _ => Input.DaysBetweenEmailNotifications, }; userProfile.SendEmailWhenNoProductHasBeenChanged = Input.SendEmailWhenNoProductHasBeenChanged; await _userProfileService.UpdateAsync(userProfile); } StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }
private async Task LoadAsync(IdentityUser user) { Models.EntityModels.UserProfile userProfile = await _userProfileService.GetByUserId(user.Id); Input = new InputModel { EnableEmailNotifications = userProfile.EnableEmailNotifications, DaysBetweenEmailNotifications = userProfile.DaysBetweenEmailNotifications, SendEmailWhenNoProductHasBeenChanged = userProfile.SendEmailWhenNoProductHasBeenChanged, EmailFrequency = "Custom" }; if (userProfile.DaysBetweenEmailNotifications == 1) { Input.EmailFrequency = "Daily"; } else if (userProfile.DaysBetweenEmailNotifications == 7) { Input.EmailFrequency = "Weekly"; } else if (userProfile.DaysBetweenEmailNotifications == 30) { Input.EmailFrequency = "Monthly"; } }
private async Task LoadAsync(IdentityUser user) { string userName = await _userManager.GetUserNameAsync(user); string phoneNumber = await _userManager.GetPhoneNumberAsync(user); Models.EntityModels.UserProfile userProfile = await _userProfileService.GetByUserId(user.Id); Username = userName; Input = new InputModel { PhoneNumber = phoneNumber, FirstName = userProfile.FirstName, LastName = userProfile.LastName }; }
public async Task<IActionResult> OnPostAsync() { IdentityUser user = await _userManager.GetUserAsync(User); if (user == null) { return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } if (!ModelState.IsValid) { await LoadAsync(user); return Page(); } string phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { IdentityResult setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { string userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'."); } } Models.EntityModels.UserProfile userProfile = await _userProfileService.GetByUserId(user.Id); if (userProfile != null) { userProfile.FirstName = Input.FirstName; userProfile.LastName = Input.LastName; await _userProfileService.UpdateAsync(userProfile); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return RedirectToPage(); }