Ejemplo n.º 1
0
        public async Task <IActionResult> Index(IndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            var runnerUser = await _runnerRepo.GetUserAsync(user.Id);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var email = user.Email;

            if (model.Email != email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, model.Email);

                if (!setEmailResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
                }
            }

            var name = runnerUser.Name;

            if (model.Name != name)
            {
                await _runnerRepo.SetNameAsync(runnerUser.Id, model.Name);
            }

            var lastName = runnerUser.LastName;

            if (model.LastName != lastName)
            {
                await _runnerRepo.SetLastNameAsync(runnerUser.Id, model.LastName);
            }

            var sex = runnerUser.Sex;

            if (model.Sex != sex)
            {
                await _runnerRepo.SetSexAsync(runnerUser.Id, model.Sex);
            }

            var birthday = runnerUser.Birthday;

            if (model.Birthday != birthday)
            {
                await _runnerRepo.SetBirthdayAsync(runnerUser.Id, model.Birthday);
            }

            var phoneNumber = user.PhoneNumber;

            if (model.PhoneNumber != phoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, model.PhoneNumber);

                if (!setPhoneResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
                }
            }

            if (model.Photo != null)
            {
                var blobUri = await _cloudStorageService.UploadProfilePhoto(model.Photo);

                await _runnerRepo.SetPhotoUriAsync(runnerUser.Id, blobUri);
            }

            StatusMessage = "Your profile has been updated";
            return(RedirectToAction(nameof(Index)));
        }