Exemple #1
0
        public async Task <UpdateCustomerCommandResponse> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            var response = new UpdateCustomerCommandResponse();

            var validator = new UpdateCustomerCommandValidator();
            var result    = await validator.ValidateAsync(request);

            if (result.Errors.Count > 0)
            {
                response.Success          = false;
                response.ValidationErrors = new List <string>();
                foreach (var error in result.Errors)
                {
                    response.ValidationErrors.Add(error.ErrorMessage);
                }
            }

            if (result.Errors.Count > 0)
            {
                throw new ValidationException(result);
            }

            if (response.Success)
            {
                var customer = _mapper.Map <Customer>(request);
                await _customerRepository.UpdateAsync(customer);

                response.Customer = _mapper.Map <UpdateCustomerCommandViewModel>(customer);
            }
            return(response);
        }
        public async Task ValidateAsync_InvalidCommand_ValidationResultIsNotValid()
        {
            // Arrange
            var validator = new UpdateCustomerCommandValidator();
            var command   = new UpdateCustomerCommand
            {
                PhoneNumber = 123456789
            };

            // Act
            var result = await validator.ValidateAsync(command);

            // Assert
            result.Should().NotBeNull();
            result.IsValid.Should().BeFalse();
            result.Errors.Should().NotBeEmpty();
        }
        public async Task ValidateAsync_ValidCommand_ValidationResultIsValid()
        {
            // Arrange
            var validator = new UpdateCustomerCommandValidator();
            var command   = new UpdateCustomerCommand
            {
                Id          = Guid.Parse("2DEE6F31-67F4-43E1-A944-9672B03B257C"),
                PhoneNumber = 123456789,
                Website     = "https://www.example.com"
            };

            // Act
            var result = await validator.ValidateAsync(command);

            // Assert
            result.Should().NotBeNull();
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }