public async Task <IActionResult> Post([FromBody] CreateCustomerRequest request)
        {
            var validator = new CreateCustomerValidator();
            var result    = validator.Validate(request);

            if (!result.IsValid)
            {
                return(BadRequest(result.Errors));
            }

            // convert request DTO to command DTO
            var createCustomerCommand = Mapper.Map <CreateCustomerCommand1>(request);

            // command handler returns response that wraps domain model
            var createCustomerCommandResponse = await _mediator.Send(createCustomerCommand);

            if (createCustomerCommandResponse.Result == OperationStatus.ValidationFailure)
            {
                return(BadRequest(createCustomerCommandResponse.ErrorMessages));
            }

            if (createCustomerCommandResponse.Result == OperationStatus.Conflict)
            {
                return(Conflict(createCustomerCommandResponse));
            }

            return(Ok(createCustomerCommandResponse.CustomerId));
        }
        public async Task <IActionResult> Post([FromBody] CreateCustomerRequest request)
        {
            var validator = new CreateCustomerValidator();
            var result    = validator.Validate(request);

            if (!result.IsValid)
            {
                return(BadRequest(result.Errors));
            }

            var addressResult = Address3.Create(request.Address.HouseNoOrName,
                                                request.Address.Street,
                                                request.Address.City,
                                                request.Address.County,
                                                request.Address.PostCode
                                                );

            if (addressResult.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(addressResult.ErrorMessages));
            }

            var customerResult = Customer3.Create(request.CustomerId,
                                                  request.FirstName,
                                                  request.LastName,
                                                  request.MiddleName,
                                                  request.Title,
                                                  addressResult.Value,
                                                  request.DateOfBirth,
                                                  request.CountryOfBirth,
                                                  request.IdDocumentType,
                                                  request.IdDocumentNumber,
                                                  request.VatNumber,
                                                  request.VatCountry);

            if (customerResult.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(customerResult.ErrorMessages));
            }


            // command handler returns response that wraps domain model
            var response = await _mediator.Send(new CreateCustomerCommand3(
                                                    customerResult.Value));

            if (response.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(response.ErrorMessages));
            }

            if (response.Status == OperationStatus.Conflict)
            {
                return(Conflict(response));
            }

            return(Ok(response.Value));
        }
Exemplo n.º 3
0
        public async Task <CreateCustomerResponse> Add(CreateCustomerRequest command)
        {
            var validators = new CreateCustomerValidator(_Repository, command);

            if (!validators.isValid())
            {
                throw new ValidationRuleException(validators.Errors.ToArray());
            }

            return(await _Mediator.Send(command));
        }
        public void ShouldValidateValidCustomer()
        {
            var customer = new CreateCustomerRequest()
            {
                ID = Guid.NewGuid(), FirstName = "Bob", Surname = "Bobby", EMail = "*****@*****.**", CustomerPassword = "******"
            };
            var validator = new CreateCustomerValidator();

            var result = validator.Validate(customer);

            Assert.True(result.IsValid);
        }
        public void ShouldCatchIfSurnameIsTooLong()
        {
            var customer = new CreateCustomerRequest()
            {
                ID = Guid.NewGuid(), FirstName = "Bob", Surname = "abcdefghijkmlpqjfkidsjfkdsjfkdsjfkdsjfkdsjfkdsjfkdsjfdksjfdskfjdskfdjkfdsjkfdsjdsfkjfdskjfdskfdjkfdsjfdksjfdskjfdskfdjkdfjkdfsjdfsk", EMail = "*****@*****.**", CustomerPassword = "******"
            };
            var validator = new CreateCustomerValidator();

            var result = validator.Validate(customer);

            Assert.False(result.IsValid);
            Assert.Equal(1, result.Errors.Count());
            Assert.Equal("Surname", result.Errors.FirstOrDefault().PropertyName);
        }
        public void ShouldCatchIfSurnameIsEmpty()
        {
            var customer = new CreateCustomerRequest()
            {
                ID = Guid.NewGuid(), FirstName = "Bob", Surname = "", EMail = "*****@*****.**", CustomerPassword = "******"
            };
            var validator = new CreateCustomerValidator();

            var result = validator.Validate(customer);

            Assert.False(result.IsValid);
            Assert.Equal(1, result.Errors.Count());
            Assert.Equal("Surname", result.Errors.FirstOrDefault().PropertyName);
        }
        public void ShouldCatchIfEMailIsNotAnEmail()
        {
            var customer = new CreateCustomerRequest()
            {
                ID = Guid.NewGuid(), FirstName = "Bob", Surname = "something", EMail = "Random String", CustomerPassword = "******"
            };
            var validator = new CreateCustomerValidator();

            var result = validator.Validate(customer);

            Assert.False(result.IsValid);
            Assert.Equal(1, result.Errors.Count());
            Assert.Equal("EMail", result.Errors.FirstOrDefault().PropertyName);
        }
Exemplo n.º 8
0
        public void NameTooLong()
        {
            // Arrange
            CreateCustomerRequest request = new CreateCustomerRequest()
            {
                Name = "123456789012345678901234567890123456789012345678901234567890"
            };
            CreateCustomerValidator sut = new CreateCustomerValidator();

            // Act
            ValidationResult result = sut.Validate(request);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Errors.Count);
            Assert.AreEqual("MaximumLengthValidator", result.Errors[0].ErrorCode);
            Assert.AreEqual("Name", result.Errors[0].PropertyName);
        }
Exemplo n.º 9
0
            public void Customer_IsValidated_IsValid(
                string fistName,
                string lastName,
                string emailAddress,
                string password,
                bool validCustomer)
            {
                var customer = new Customer
                {
                    FirstName    = fistName,
                    LastName     = lastName,
                    EmailAddress = emailAddress,
                    Password     = password
                };

                var validator = new CreateCustomerValidator();
                var results   = validator.Validate(customer);

                results.IsValid.Should().Be(validCustomer);
            }
 public CreateCustomerHandler(CustomerService customerService, ILogger <CreateCustomerHandler> logger)
 {
     _customerService = customerService ?? throw new ArgumentNullException(nameof(customerService));
     _logger          = logger ?? throw new ArgumentNullException(nameof(logger));
     _validator       = new CreateCustomerValidator();
 }
Exemplo n.º 11
0
 public EfCreateCustomerCommand(MovingContext context, CreateCustomerValidator validator)
 {
     _context   = context;
     _validator = validator;
 }