public async Task <IResult <Guid> > CreateCustomer(CreateCustomerCommand createCommand)
        {
            if (createCommand == null)
            {
                return(await Result <Guid> .FailAsync("customer fields"));
            }

            var validator = createCommand.Validate();

            if (!validator.IsValid)
            {
                return(await Result <Guid> .FailValidationAsync(validator.Errors));
            }

            var customer = await _service.GetCustomerByEmail(createCommand.Email);

            if (customer != null)
            {
                return(await Result <Guid> .FailAsync("customer already exists"));
            }

            var id = await _service.CreateCustomer(createCommand);

            if (id == Guid.Empty)
            {
                return(await Result <Guid> .FailAsync("unable to create customer"));
            }

            return(await Result <Guid> .CreatedAsync(id));
        }
        public void Deve_retornar_falso_ao_criar_um_createCutomerCAT_com_dados_invalidos()
        {
            var createCustomerCommand = new CreateCustomerCommand("1", "222", "11", Guid.NewGuid());

            createCustomerCommand.Validate();
            Assert.AreEqual(false, createCustomerCommand.IsValid);
        }
        public void Deve_retornar_falso_ao_criar_um_createCutomerCAT_com_id_CAT_invalido()
        {
            var createCustomerCommand = new CreateCustomerCommand("Empresa Teste", "Empresa Teste", "30560346000105", Guid.Empty);

            createCustomerCommand.Validate();
            Assert.AreEqual(false, createCustomerCommand.IsValid);
        }
        public async Task <ActionResult <GenericCommandResult> > Create(
            [FromBody] CreateCustomerCommand command,
            [FromServices] CustomerHandler handler,
            [FromServices] UserHandler userHandler
            )
        {
            var validationResult = command.Validate();

            if (!validationResult.Success)
            {
                return(BadRequest(validationResult));
            }

            var userResult = (GenericCommandResult)userHandler.Handle(new CreateUserCommand(command.Email, command.Password, "customer"));

            if (userResult.Success)
            {
                var user = (User)userResult.Data;
                command.SetUserId(user.Id);


                var result = handler.Handle(command);
                return(Ok(result));
            }

            return(BadRequest(userResult));
        }
        public async Task <CommandResult <CreateCustomerCommandResponse> > Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
        {
            request.Validate();

            if (!request.Valid)
            {
                if (request.Notifications.Any())
                {
                    return(CommandResult <CreateCustomerCommandResponse> .Failed(HttpStatusCode.PreconditionFailed, "Pre condition failed", request.Notifications));
                }
            }

            var entityCreated = new CustomerEntity();

            entityCreated.New(request.Name, request.LastName, request.Email);
            await _customerRepository.CreateAsync(entityCreated);

            return(CommandResult <CreateCustomerCommandResponse> .Succeeded(new CreateCustomerCommandResponse()
            {
                Id = entityCreated.Id.ToString(),
                Name = entityCreated.Name,
                LastName = entityCreated.LastName,
                Email = entityCreated.Email
            }, HttpStatusCode.Created));
        }
示例#6
0
        public void DadoUmClienteComCPFInvalido_Invalid()
        {
            var command = new CreateCustomerCommand("Nome", "1234678");

            command.Validate();

            Assert.AreEqual(command.Invalid, true);
        }
示例#7
0
        public void DadoUmClienteSemNome_Invalid()
        {
            var command = new CreateCustomerCommand("", "12345678901");

            command.Validate();

            Assert.AreEqual(command.Invalid, true);
        }
示例#8
0
        public void DadoUmClienteValido_Valid()
        {
            var command = new CreateCustomerCommand("Nome do Cliente", "12345678901");

            command.Validate();

            Assert.AreEqual(command.Valid, true);
        }
        public void ShouldValidateWhenCommandIsVAlid()
        {
            CreateCustomerCommand command = new CreateCustomerCommand();

            command.FirstName = "Douglas";
            command.LastName  = "Pereira";
            command.Document  = "58303366343";
            command.Email     = "*****@*****.**";
            command.Phone     = "123123123";
            Assert.True(command.Validate());
        }
示例#10
0
        public void ShouldValidateWhenCommandIsValid()
        {
            var command = new CreateCustomerCommand();

            command.FirstName = "George";
            command.LastName  = "Wurthmann";
            command.Document  = "28659170377";
            command.Email     = "*****@*****.**";
            command.Phone     = "11999999997";

            Assert.True(command.Validate());
        }
示例#11
0
        public void ShouldValidateWhenCommandIsValid()
        {
            var command = new CreateCustomerCommand();

            command.FirstName = "Leonardo";
            command.LastName  = "Guarilha";
            command.Document  = "14655803010";
            command.Email     = "*****@*****.**";
            command.Phone     = "123455436";

            command.Validate();

            Assert.AreEqual(true, command.Valid);
        }
        public void ShouldRegisterCustomerWhenCommandIsValid()
        {
            CreateCustomerCommand command = new CreateCustomerCommand();

            command.FirstName = "Douglas";
            command.LastName  = "Pereira";
            command.Document  = "58303366343";
            command.Email     = "*****@*****.**";
            command.Phone     = "123123123";
            Assert.True(command.Validate());

            CreateCustomerCommandHandler handler = new CreateCustomerCommandHandler(new FakeCustomerRepository(), new FakeEmailService());
            var result = handler.Handle(command);

            Assert.False(result == null);
            Assert.True(handler.Valid);
        }
示例#13
0
        public void ShouldRegisterCustomerWhenCommandIsValid()
        {
            var command = new CreateCustomerCommand();

            command.FirstName = "Leonardo";
            command.LastName  = "Guarilha";
            command.Document  = "14655803010";
            command.Email     = "*****@*****.**";
            command.Phone     = "123455436";

            command.Validate();

            var handler = new CustomerHandler(new FakeCustomerRepository(), new FakeEmailService());
            var result  = handler.Handler(command);

            Assert.AreNotEqual(null, result);
            Assert.AreEqual(true, handler.Valid);
            Assert.AreEqual(true, command.Valid);
        }