public async Task RegisterAsync(RegisterUserViewModel registerUser) { var command = new RegisterUserCommand() { Entity = _mapper.Map <User>(registerUser) }; if (!command.IsValid()) { await RaiseValidationErrorsAsync(command); return; } var users = await _userRepository.GetAllAsync(); if (users.Any(u => u.Email == command.Entity.Email)) { await _mediatorHandler.RaiseDomainNotificationAsync(new DomainNotification(command.MessageType, "Email is already in use.")); return; } await _userRepository.AddAsync(command.Entity); }
public async Task Handle(RegisterUserCommand notification, CancellationToken cancellationToken) { if (!notification.IsValid()) { NotifyValidationErrors(notification); return; } if (await _userRepository.GetByUsernameAsync(notification.Username) != null) { await _bus.RaiseEvent(new DomainNotification(notification.MessageType, "There's already a user with this username")); return; } var userId = Guid.NewGuid(); var userRoles = new List <UserRole>(); foreach (var role in notification.UserRoles) { userRoles.Add(new UserRole(Guid.NewGuid(), userId, role.RoleId)); } var user = new User(userId, notification.Username, _hashString.Generate(notification.Password), userRoles, DateTime.Now); _userRepository.Add(user); if (await CommitAsync()) { } }
private void btnSave_Click(object sender, EventArgs e) { var name = txtUserName.Text; var email = txtEmail.Text; var phone = txtPhone.Text; var birthDay = dtBirthday.Value; var provence = txtProvince.Text; var city = txtCity.Text; var county = txtCounty.Text; var street = txtStreet.Text; var userViewModel = new UserViewModel() { BirthDate = birthDay, City = city, Street = street, County = county, Email = email, Name = name, Phone = phone, Province = provence }; RegisterUserCommand registerStudentCommand = new RegisterUserCommand(userViewModel.Name, userViewModel.Email, userViewModel.BirthDate, userViewModel.Phone, userViewModel.Province, userViewModel.City, userViewModel.County, userViewModel.Street); if (!registerStudentCommand.IsValid()) { FrmAlert.Alert(this, registerStudentCommand.ValidationResult, sss); } UserService userService = new UserService(); userService.Add(userViewModel); #region MyRegion ////添加命令验证 ////如果命令无效,证明有错误 //if (!registerStudentCommand.IsValid()) //{ // List<string> errorInfo = new List<string>(); // //FrmAlert.Alert(this, registerStudentCommand.ValidationResult,(a)=> { this.ActiveControl = (UCTextBox)a; }); // //获取到错误,请思考这个Result从哪里来的 // foreach (var error in registerStudentCommand.ValidationResult.Errors) // { // //errorInfo.Add(error.ErrorMessage); // //FrmAlert.Alert(this, error.ErrorMessage); // } // //对错误进行记录,还需要抛给前台 // var dsd = errorInfo; //} #endregion }
public void RegisterInvalidPassword() { RegisterUserCommand registerUserCommand = new RegisterUserCommand() { UserName = "******", Password = "", Name = "Name", Surname = "Surname" }; bool isValid = registerUserCommand.IsValid(); Assert.IsFalse(isValid); Assert.IsEmpty(registerUserCommand.Password); }
public IActionResult PostUser([FromBody] RegisterUserCommand command) { if (!command.IsValid()) { return(BadRequest(InvalidCommand)); } try { _commandDispatcher.Execute(command); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }