コード例 #1
0
        public async Task <IActionResult> NotificationsAndLayout()
        {
            var user = await _userManager.GetUserAsync(User);

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

            var model = new NotificationsAndLayoutViewModel
            {
                EmailOnFeedUpdate = user.EmailOnFeedUpdate,
                EmailOnMessage    = user.EmailOnMessage,
                TextOnFeedUpdate  = user.TextOnFeedUpdate,
                TextOnMessage     = user.TextOnMessage,
                HomeScreen        = user.HomeScreen,
                StatusMessage     = StatusMessage
            };

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> NotificationsAndLayout(NotificationsAndLayoutViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

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

            var emailOnFeedUpdate = user.EmailOnFeedUpdate;

            if (model.EmailOnFeedUpdate != emailOnFeedUpdate)
            {
                user.EmailOnFeedUpdate = model.EmailOnFeedUpdate;
                var setResult = await _userManager.UpdateAsync(user);

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

            var emailOnMessage = user.EmailOnMessage;

            if (model.EmailOnMessage != emailOnMessage)
            {
                user.EmailOnMessage = model.EmailOnMessage;
                var setResult = await _userManager.UpdateAsync(user);

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

            var textOnFeedUpdate = user.TextOnFeedUpdate;

            if (model.TextOnFeedUpdate != textOnFeedUpdate)
            {
                user.TextOnFeedUpdate = model.TextOnFeedUpdate;
                var setResult = await _userManager.UpdateAsync(user);

                if (!setResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting text on feed update for user with ID '{user.Id}'.");
                }
            }

            var textOnMessage = user.TextOnMessage;

            if (model.TextOnMessage != textOnMessage)
            {
                user.TextOnMessage = model.TextOnMessage;
                var setResult = await _userManager.UpdateAsync(user);

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

            var homeScreen = user.HomeScreen;

            if (model.HomeScreen != homeScreen)
            {
                user.HomeScreen = model.HomeScreen;
                var setResult = await _userManager.UpdateAsync(user);

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

            StatusMessage = "Your settings have been updated";
            return(RedirectToAction(nameof(NotificationsAndLayout)));
        }