public ICommandResult Handle(RegisterCustomerCommand command) { // Passo 1. Verificar se o CPF já existe if (_customerRepository.DocumentExists(command.Document)) { AddNotification("Document", "Este CPF já está cadastrado"); return(null); } // Passo 2. Gerar o novo cliente var name = new Name(command.FirstName, command.LastName); var email = new Email(command.Email); var user = new User(command.Username, command.Password, command.ConfirmPassword); var document = new Document(command.Document); var customer = new Customer(name, email, document, user); // Passo 3. Adicionar as notificações AddNotifications(name.Notifications); AddNotifications(email.Notifications); AddNotifications(document.Notifications); AddNotifications(user.Notifications); AddNotifications(customer.Notifications); // Passo 4. Inserir no banco if (IsValid()) { _customerRepository.Save(customer); } // Passo 5. Enviar E-mail de boas vindas _emailService.Send(customer.Name.ToString(), customer.Email.ToString(), string.Format(EmailTemplates.WelcomeEmailTitle, customer.Name.ToString()), string.Format(EmailTemplates.WelcomeEmailBody, customer.Name)); // Passo 6. Retornar algo return(new RegisterCustomerCommandResult(customer.Id, customer.Name.ToString())); }
public ObjectRequest NewCustomer(RegisterCustomerCommand cmd) { var user = _repUser.GetUserEmail(cmd.Email); if (user == null) { return(new ObjectRequest().CreateObjectRequest($"Usuario nao encontrado", false)); } Customer _customer = new Customer(cmd.Document, cmd.DateBirthday, cmd.Phone, cmd.Gender, user); Address _address = new Address(cmd.Street, cmd.Number, cmd.Complement, cmd.District, cmd.City, cmd.State, cmd.CEP, _customer.ID); _repCustomer.AddEntity(_customer); _repAddress.AddEntity(_address); _address.ListErrors().ForEach(x => _customer.ListErrors().Add(x)); if (Commit(_customer)) { return(new ObjectRequest().CreateObjectRequest($"Cliente {_customer.Document} Registrado com Sucesso ", true)); } return(new ObjectRequest().CreateErrorNotification(_customer.ListErrors())); }
public void CustomerRegistration_ShouldNot_CreateCustomer_WithNullValues(RegisterCustomerCommand cmd) { Should.Throw <ArgumentNullException>(() => { var c = Customer.Registration(null, cmd.FirstName, cmd.LastName, cmd.Email); }); }
public async Task <IActionResult> PostCustomer([FromBody] dynamic body) { try { var cmd = new RegisterCustomerCommand { CEP = (string)body.cep, City = (string)body.city, Complement = (string)body.complement, District = (string)body.district, Number = (int)body.number, Street = (string)body.street, State = (string)body.state, DateBirthday = (DateTime)body.dateBirthday, Document = (string)body.document, // Email = User.Identity.Name, Email = (string)body.email, Gender = (bool)body.gender, Phone = (string)body.phone, UserId = (Guid)body.userId }; var result = _service.NewCustomer(cmd); return(await CreateResponse(result)); } catch (Exception ex) { return(await ServerErroApp(ex)); } }
public async Task Register_Customer_Should_Return_CustomerId() { // Arrange using var httpClient = new TestClientProvider().HttpClient; var tokenResponse = await httpClient.PostAsync($"/api/auth/token", new StringContent("{}", Encoding.UTF8, "application/json")); var token = JsonConvert.DeserializeObject <HttpServiceResponseBase <string> >(await tokenResponse.Content.ReadAsStringAsync()).Data; var command = new RegisterCustomerCommand { Name = "Ugur", Address = "Merter - Istanbul", Phone = "506 533 53 53", Email = "*****@*****.**" }; var json = JsonConvert.SerializeObject(command); var data = new StringContent(json, Encoding.UTF8, "application/json"); // Act httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); var response = await httpClient.PostAsync($"/api/customers", data); var customer = JsonConvert.DeserializeObject <HttpServiceResponseBase <string> >(await response.Content.ReadAsStringAsync()).Data; // Assert customer.Should().NotBeNullOrEmpty(); response.StatusCode.Should().Be(HttpStatusCode.Created); }
public async Task <IActionResult> Index() { var command = new RegisterCustomerCommand(Guid.NewGuid(), "Guilherme", "*****@*****.**"); var result = await _mediator.SendCommand(command); return(GenerateResponse(result)); }
public async Task <IActionResult> Post([FromBody] RegisterCustomerCommand command) { var result = await _mediator.Send(command); return(Created(string.Empty, new HttpServiceResponseBase <string> { Data = result, Code = HttpStatusCode.Created })); }
public async Task <IActionResult> Post([FromBody] RegisterCustomerCommand command) { var result = _handler.Handle(command); if (!_handler.IsValid()) { return(BadRequest(new { success = false, errors = _handler.Notifications })); } return(Ok(result)); }
public CustomerHandlerTests() { _command = new RegisterCustomerCommand(); _command.Id = new Guid("74d96684-817d-4b5a-8edc-1a20aca2228c"); _command.FirstName = "Enrique"; _command.LastName = "Souza"; _command.Email = "*****@*****.**"; _command.UserId = new Guid("96352cd9-f793-42b1-bcb8-2f9c8698b330"); _command.Password = "******"; _command.ConfirmPassword = "******"; }
public void SampleCommand_ShouldRunWithNoException(string id, string name, string accountno) { var handler = CommandHandlerFactory.GetCommandHandler <RegisterCustomerCommand>(); var command = new RegisterCustomerCommand { Id = id, Name = name, AccountNo = accountno }; Should.NotThrow(() => handler.Handle(command)); }
public string Post(RegisterCustomerCommand command) { #region Move To CustomerService // Guid customerId = Guid.Empty; // eventBus.Subscribe(new ActionHandler<CustomerCreatedEvent>(p => id = p.Id)); // commandbus.Dispatch(command); // #endregion customerService.RegisterCustomer(command); return("OK"); }