public async Task <Photo> Upload(IFormFile photo, PhotoType type) { var photoResults = await _photoService.UploadOriginalsAsync(new IFormFile[] { photo }, type, UserId); if (photoResults.Any()) { var photoResult = photoResults.First(); if (photoResult.Location != null) { photoResult.Location = await _locationService.AddOrUpdateLocationAsync(photoResult.Location); } return(await _photoService.AddOrUpdatePhotoAsync(photoResult)); } return(null); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var userProfile = await _userService.GetUserAsync(user.Id); // Validate uniqueness on display name if (userProfile.DisplayName != Input.DisplayName) { var existingUser = await _userService.GetUserByNameAsync(Input.DisplayName, userProfile); if (existingUser != null) { ModelState.AddModelError("Input.DisplayName", "Sorry, that display name is taken. Please choose another display name."); return(Page()); } } if (userProfile.Location == null) { userProfile.Location = new Location { DateCreated = DateTime.UtcNow }; } userProfile.FirstName = Input.FirstName; userProfile.LastName = Input.LastName; userProfile.Bio = Input.Bio; userProfile.DisplayName = Input.DisplayName; userProfile.Location.AddressLine1 = Input.AddressLine1; userProfile.Location.AddressLine2 = Input.AddressLine2; userProfile.Location.City = Input.City; userProfile.Location.StateOrProvince = Input.StateOrProvince; userProfile.Location.PostalCode = Input.PostalCode; userProfile.Location.Country = Input.Country; userProfile.EmailUpdates = Input.EmailUpdates; userProfile.SocialUpdates = Input.SocialUpdates; userProfile.ProfileVisibility = Input.ProfileVisibility; userProfile.InventoryItemVisibility = Input.InventoryItemVisibility; userProfile.PlantInfoVisibility = Input.PlantInfoVisibility; userProfile.OriginVisibility = Input.OriginVisibility; userProfile.ActivityVisibility = Input.ActivityVisibility; userProfile.DateModified = DateTime.UtcNow; userProfile.Location.DateModified = DateTime.UtcNow; if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } var phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { StatusMessage = "Unexpected error when trying to set phone number."; return(RedirectToPage()); } } if (Input.ProfilePhotoFile != null) { var photoResult = await _photoService.UploadOriginalAsync(Input.ProfilePhotoFile, Data.Shared.PhotoType.User, user.Id, storeLocation : false); photoResult.TypeId = userProfile.Id; photoResult = await _photoService.AddOrUpdatePhotoAsync(photoResult); userProfile.Photo = photoResult; userProfile = await _userService.UpdateUserAsync(userProfile); ProfilePhoto = userProfile.Photo; } else { await _userService.UpdateUserAsync(userProfile); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }