Exemplo n.º 1
0
        public async Task <IActionResult> UpdateAccount(AppUserViewModel userVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(userVm));
            }
            else
            {
                var error = 0;

                var isValidByPhoneNumber = await _userService.CheckUpdatePhoneNumber(userVm.PhoneNumber, userVm.Id.ToString());

                if (isValidByPhoneNumber == false)
                {
                    ModelState.AddModelError(userVm.PhoneNumber, $"Phone number '{userVm.PhoneNumber}' is already taken.");
                    error++;
                }

                if (!ModelState.IsValid)
                {
                    error++;
                }

                //MM/dd/yyy
                var appUser = await _userManager.FindByIdAsync(userVm.Id.ToString());

                appUser.Email        = userVm.Email;
                appUser.FullName     = userVm.FullName;
                appUser.PhoneNumber  = userVm.PhoneNumber;
                appUser.BirthDay     = userVm.BirthDay;
                appUser.Gender       = userVm.Gender;
                appUser.Address      = userVm.Address;
                appUser.DateModified = DateTime.Now;

                var isUpdate = await _userManager.UpdateAsync(appUser);

                if (!isUpdate.Succeeded || error > 0)
                {
                    AddErrors(isUpdate);
                    return(View(userVm));
                }
                return(RedirectToLocal("/account.html"));
            }
        }