public ActionResult Settings(UserSettingsViewModel userSettingsViewModel) { if(!this.ValidateAndAppendMessages(userSettingsViewModel)) { return View(userSettingsViewModel); } // Check Email Address if (this.UserService.EmailAddressIsInUse(userSettingsViewModel.EmailAddress, this.ActiveUser.UserId)) { this.AppendMessage(new ErrorMessage { Text = "The email address you entered is already in use." }); return View(userSettingsViewModel); } // Check Username Uniqueness if (this.UserService.UsernameIsInUse(this.ActiveUser.UserId, userSettingsViewModel.Username)) { this.AppendMessage(new ErrorMessage { Text = "The requested username is already in use" }); return View(userSettingsViewModel); } using(var unitOfWork = this.UnitOfWorkFactory.NewUnitOfWork()) { try { var user = this.UserService.GetUserById(this.ActiveUser.UserId); var originalUsername = user.Username; user = Mapper.Map(userSettingsViewModel, user); // Cust Username Flag if (!user.HasCustomUsername && originalUsername != user.Username) { user.HasCustomUsername = true; } user.EmailAddress = userSettingsViewModel.EmailAddress.Trim(); user.DateModified = DateTime.Now; unitOfWork.Commit(); this.UserResolver.Update(Mapper.Map(user, new UserSummary())); return new EmptyResult(); } catch (Exception ex) { unitOfWork.Rollback(); this.LogHandledException(ex); return this.Issue500(); } } }
public ActionResult SetNotifications(UserSettingsViewModel userSettingsViewModel) { if (userSettingsViewModel.UserId != this.ActiveUser.UserId) { return this.Issue404(); } using (var unitOfWork = this.UnitOfWorkFactory.NewUnitOfWork()) { try { var user = this.UserService.GetUserById(userSettingsViewModel.UserId); #region Sub/Un-SUB Types // RecipeComments: Handle Unsubscribe if (!userSettingsViewModel.RecipeCommentNotifications && user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int) NotificationType.RecipeComment)) { this.UserService.UnsubscribeUserFromNotificationType(user, NotificationType.RecipeComment); } // RecipeComments: Handle Subscribe if (userSettingsViewModel.RecipeCommentNotifications && !user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int) NotificationType.RecipeComment)) { this.UserService.SubscribeUserToNotificationType(user, NotificationType.RecipeComment); } // BrewSessionComments: Handle Unsubscribe if (!userSettingsViewModel.BrewSessionCommentNotifications && user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.BrewSessionComment)) { this.UserService.UnsubscribeUserFromNotificationType(user, NotificationType.BrewSessionComment); } // BrewSessionComments: Handle Subscribe if (userSettingsViewModel.BrewSessionCommentNotifications && !user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.BrewSessionComment)) { this.UserService.SubscribeUserToNotificationType(user, NotificationType.BrewSessionComment); } // BrewerFollow: Handle Unsubscribe if (!userSettingsViewModel.BrewerFollowNotifications && user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.BrewerFollowed)) { this.UserService.UnsubscribeUserFromNotificationType(user, NotificationType.BrewerFollowed); } // BrewerFollow: Handle Subscribe if (userSettingsViewModel.BrewerFollowNotifications && !user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.BrewerFollowed)) { this.UserService.SubscribeUserToNotificationType(user, NotificationType.BrewerFollowed); } // SiteFeatures: Handle Unsubscribe if (!userSettingsViewModel.SiteFeatureNotifications && user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.SiteFeatures)) { this.UserService.UnsubscribeUserFromNotificationType(user, NotificationType.SiteFeatures); } // SiteFeatures: Handle Subscribe if (userSettingsViewModel.SiteFeatureNotifications && !user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.SiteFeatures)) { this.UserService.SubscribeUserToNotificationType(user, NotificationType.SiteFeatures); } // SiteOutages: Handle Unsubscribe if (!userSettingsViewModel.SiteOutageNotifications && user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.SiteOutages)) { this.UserService.UnsubscribeUserFromNotificationType(user, NotificationType.SiteOutages); } // SiteOutages: Handle Subscribe if (userSettingsViewModel.SiteOutageNotifications && !user.UserNotificationTypes.Any(x => x.NotificationTypeId == (int)NotificationType.SiteOutages)) { this.UserService.SubscribeUserToNotificationType(user, NotificationType.SiteOutages); } #endregion unitOfWork.Commit(); return new EmptyResult(); } catch (Exception ex) { unitOfWork.Rollback(); this.LogHandledException(ex); return this.Issue500(); } } }