private RestoreUser SetupCommand(string userId) { var command = new RestoreUser(_userValidatorMock.Object, _recyclebinControllerMock.Object); var args = new[] { "restore-user", userId }; command.Initialize(args, _portalSettings, null, -1); return(command); }
public ActionResult Index([Bind(Prefix = "UserListViewModel")] IEnumerable <UserListViewModel> model) { if (model == null) { throw new ArgumentNullException("model"); } var users = _userQueryService.GetUsers(); ICommand command; foreach (var userListViewModel in model) { var user = users.FirstOrDefault(x => x.UserId == userListViewModel.UserId); if (user == null) { continue; } if (user.IsDeleted != userListViewModel.IsDeleted) { if (userListViewModel.IsDeleted) { command = new DeleteUser(new UserId(userListViewModel.UserId)); _commandSender.Send(command); } else { command = new RestoreUser(new UserId(userListViewModel.UserId)); _commandSender.Send(command); } } var userRole = new UserRole(); UserRole.TryParse(userListViewModel.UserRole, out userRole); if (!user.Roles.Contains(userRole)) { var roleId = _userQueryService.GetUserRoleId(userRole); command = new AddRoleToUser(new UserId(userListViewModel.UserId), roleId); _commandSender.Send(command); } } return(RedirectToAction("Index")); }
/// <summary> /// Handles the specified <see cref="RestoreUser"/> command. /// </summary> /// <param name="command">The command.</param> /// <exception cref="System.ArgumentNullException">When UserId is null.</exception> /// <exception cref="System.ArgumentException">When user does not exist.</exception> public void Handle(RestoreUser command) { if (command.Id == null) { throw new ArgumentNullException("command", "UserId must be specified."); } var user = _userRepository.Get(command.Id); if (user == null) { throw new ArgumentException(string.Format("User with such id: {0} does not exist.", command.Id)); } user.RestoreUser(); _userRepository.Save(user); }